va_list.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* va_list.c - tinycc support for va_list on X86_64 */
  2. #if defined __x86_64__
  3. /* Avoid include files, they may not be available when cross compiling */
  4. extern void *memset(void *s, int c, __SIZE_TYPE__ n);
  5. extern void abort(void);
  6. /* This should be in sync with our include/stdarg.h */
  7. enum __va_arg_type {
  8. __va_gen_reg, __va_float_reg, __va_stack
  9. };
  10. /* GCC compatible definition of va_list. */
  11. typedef struct {
  12. unsigned int gp_offset;
  13. unsigned int fp_offset;
  14. union {
  15. unsigned int overflow_offset;
  16. char *overflow_arg_area;
  17. };
  18. char *reg_save_area;
  19. } __va_list_struct;
  20. void __va_start(__va_list_struct *ap, void *fp)
  21. {
  22. memset(ap, 0, sizeof(__va_list_struct));
  23. *ap = *(__va_list_struct *)((char *)fp - 16);
  24. ap->overflow_arg_area = (char *)fp + ap->overflow_offset;
  25. ap->reg_save_area = (char *)fp - 176 - 16;
  26. }
  27. void *__va_arg(__va_list_struct *ap,
  28. enum __va_arg_type arg_type,
  29. int size, int align)
  30. {
  31. size = (size + 7) & ~7;
  32. align = (align + 7) & ~7;
  33. switch (arg_type) {
  34. case __va_gen_reg:
  35. if (ap->gp_offset + size <= 48) {
  36. ap->gp_offset += size;
  37. return ap->reg_save_area + ap->gp_offset - size;
  38. }
  39. goto use_overflow_area;
  40. case __va_float_reg:
  41. if (ap->fp_offset < 128 + 48) {
  42. ap->fp_offset += 16;
  43. return ap->reg_save_area + ap->fp_offset - 16;
  44. }
  45. size = 8;
  46. goto use_overflow_area;
  47. case __va_stack:
  48. use_overflow_area:
  49. ap->overflow_arg_area += size;
  50. ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -align);
  51. return ap->overflow_arg_area - size;
  52. default: /* should never happen */
  53. abort();
  54. }
  55. }
  56. #endif