Modestus Moon OS  R4
CS 450 project
arg_list.h
Go to the documentation of this file.
1 #ifndef ARG_LIST__
2 #define ARG_LIST__
3 
4 //added matt-g 1/15/17
5 
6 //because the i386 architecture only passes arguments by stack, we can leverage
7  //the fact we have a pointer to the beginning of the function's stack in the form
8  //of the preceeding required argument for the ellipses.
9 
10 //this method only works if we know what should be on the stack, the only info
11  //we have of the stack would be a format string passed into a function like printf
12 
13 //arguments on i386 are pushed right to left in the call, meaning the left-most
14  //argument is at the bottom of the functions stack.
15 
19 #define arg_list unsigned char*
20 
25 #define init_arg_list(argList, argBeforeEllipses) { \
26  argList = (unsigned char*)&argBeforeEllipses; \
27  } \
28 
29 
35 static inline unsigned int arg_size_on_stack(const char *formatType, int isLong)
36 {
37  if(!isLong)
38  {
39  switch(*formatType)
40  {
41  case 'c': return sizeof(int); //minimum size of char on stack
42  case 's': return sizeof(char*); //s is a pointer, pointer size is system size
43  case 'd': case 'i': return sizeof(int);
44  case 'u': case 'o': case 'x': case 'X': return sizeof(unsigned int);
45  case 'f': case 'F': case 'e': case 'E': case 'a': case'A': case 'g': case 'G':
46  return sizeof(double); //float and double are pushed as double precision, 8 bytes
47  case 'p': return sizeof(void*);
48  default: return 0;
49  }
50  }
51  return 0;
52 }
53 
59 static inline unsigned int arg_size_on_stack_from_size(const unsigned int size)
60 {
61  return (size < 4) ? sizeof(int) : size;
62 }
63 
71 static inline void* next_arg_in_list(arg_list* list, const char *formatType, int isLong)
72 {
73  *list+=arg_size_on_stack(formatType, isLong);
74  return (void*)(*list);
75 }
76 
77 #endif //ARG_LIST__
#define arg_list
Definition: arg_list.h:19