cc_types.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "cc.h"
  18. #include <stdint.h>
  19. void line_error();
  20. int numerate_string(char *a);
  21. /* Initialize default types */
  22. void initialize_types()
  23. {
  24. /* Define void */
  25. global_types = calloc(1, sizeof(struct type));
  26. global_types->name = "void";
  27. global_types->size = 4;
  28. global_types->type = global_types;
  29. /* void* has the same properties as void */
  30. global_types->indirect = global_types;
  31. /* Define int */
  32. struct type* a = calloc(1, sizeof(struct type));
  33. a->name = "int";
  34. a->size = 4;
  35. /* int* has the same properties as int */
  36. a->indirect = a;
  37. a->type = a;
  38. /* Define char* */
  39. struct type* b = calloc(1, sizeof(struct type));
  40. b->name = "char*";
  41. b->size = 4;
  42. b->type = b;
  43. /* Define char */
  44. struct type* c = calloc(1, sizeof(struct type));
  45. c->name = "char";
  46. c->size = 1;
  47. c->type = c;
  48. /* Define char** */
  49. struct type* d = calloc(1, sizeof(struct type));
  50. d->name = "char**";
  51. d->size = 4;
  52. d->type = b;
  53. d->indirect = d;
  54. /*fix up indrects for chars */
  55. c->indirect = b;
  56. b->indirect = d;
  57. /* Define FILE */
  58. struct type* e = calloc(1, sizeof(struct type));
  59. e->name = "FILE";
  60. e->size = 4;
  61. e->type = e;
  62. /* FILE* has the same properties as FILE */
  63. e->indirect = e;
  64. /* Define FUNCTION */
  65. struct type* f = calloc(1, sizeof(struct type));
  66. f->name = "FUNCTION";
  67. f->size = 4;
  68. f->type = f;
  69. /* FUNCTION* has the same properties as FUNCTION */
  70. f->indirect = f;
  71. /* Define UNSIGNED */
  72. struct type* g = calloc(1, sizeof(struct type));
  73. g->name = "unsigned";
  74. g->size = 4;
  75. g->type = g;
  76. /* unsigned* has the same properties as unsigned */
  77. g->indirect = g;
  78. /* Custom type for mescc*/
  79. struct type* h = calloc(1, sizeof(struct type));
  80. h->name = "SCM";
  81. h->size = 4;
  82. h->indirect = h;
  83. /* Finalize type list */
  84. g->next = h;
  85. f->next = g;
  86. e->next = f;
  87. d->next = e;
  88. c->next = e;
  89. a->next = c;
  90. global_types->next = a;
  91. prim_types = global_types;
  92. }
  93. struct type* lookup_type(char* s, struct type* start)
  94. {
  95. struct type* i;
  96. for(i = start; NULL != i; i = i->next)
  97. {
  98. if(match(i->name, s))
  99. {
  100. return i;
  101. }
  102. }
  103. return NULL;
  104. }
  105. struct type* lookup_member(struct type* parent, char* name)
  106. {
  107. struct type* i;
  108. for(i = parent->members; NULL != i; i = i->members)
  109. {
  110. if(match(i->name, name)) return i;
  111. }
  112. file_print("ERROR in lookup_member ", stderr);
  113. file_print(parent->name, stderr);
  114. file_print("->", stderr);
  115. file_print(global_token->s, stderr);
  116. file_print(" does not exist\n", stderr);
  117. line_error();
  118. file_print("\n", stderr);
  119. exit(EXIT_FAILURE);
  120. }
  121. struct type* type_name();
  122. void require_match(char* message, char* required);
  123. int member_size;
  124. struct type* build_member(struct type* last, int offset)
  125. {
  126. struct type* member_type = type_name();
  127. struct type* i = calloc(1, sizeof(struct type));
  128. i->name = global_token->s;
  129. global_token = global_token->next;
  130. i->members = last;
  131. /* Check to see if array */
  132. if(match( "[", global_token->s))
  133. {
  134. global_token = global_token->next;
  135. i->size = member_type->type->size * numerate_string(global_token->s);
  136. global_token = global_token->next;
  137. require_match("Struct only supports [num] form\n", "]");
  138. }
  139. else
  140. {
  141. i->size = member_type->size;
  142. }
  143. member_size = i->size;
  144. i->type = member_type;
  145. i->offset = offset;
  146. return i;
  147. }
  148. struct type* build_union(struct type* last, int offset)
  149. {
  150. int size = 0;
  151. global_token = global_token->next;
  152. require_match("ERROR in build_union\nMissing {\n", "{");
  153. while('}' != global_token->s[0])
  154. {
  155. last = build_member(last, offset);
  156. if(member_size > size)
  157. {
  158. size = member_size;
  159. }
  160. require_match("ERROR in build_union\nMissing ;\n", ";");
  161. }
  162. member_size = size;
  163. global_token = global_token->next;
  164. return last;
  165. }
  166. void create_struct()
  167. {
  168. int offset = 0;
  169. member_size = 0;
  170. struct type* head = calloc(1, sizeof(struct type));
  171. struct type* i = calloc(1, sizeof(struct type));
  172. head->name = global_token->s;
  173. i->name = global_token->s;
  174. head->indirect = i;
  175. i->indirect = head;
  176. head->next = global_types;
  177. global_types = head;
  178. global_token = global_token->next;
  179. i->size = 4;
  180. require_match("ERROR in create_struct\n Missing {\n", "{");
  181. struct type* last = NULL;
  182. while('}' != global_token->s[0])
  183. {
  184. if(match(global_token->s, "union"))
  185. {
  186. last = build_union(last, offset);
  187. }
  188. else
  189. {
  190. last = build_member(last, offset);
  191. }
  192. offset = offset + member_size;
  193. require_match("ERROR in create_struct\n Missing ;\n", ";");
  194. }
  195. global_token = global_token->next;
  196. require_match("ERROR in create_struct\n Missing ;\n", ";");
  197. head->size = offset;
  198. head->members = last;
  199. i->members = last;
  200. }
  201. /*
  202. * type-name:
  203. * char *
  204. * int
  205. * struct
  206. * FILE
  207. * void
  208. */
  209. struct type* type_name()
  210. {
  211. int structure = match("struct", global_token->s);
  212. if(structure)
  213. {
  214. global_token = global_token->next;
  215. }
  216. struct type* ret = lookup_type(global_token->s, global_types);
  217. if(NULL == ret && !structure)
  218. {
  219. file_print("Unknown type ", stderr);
  220. file_print(global_token->s, stderr);
  221. file_print("\n", stderr);
  222. line_error();
  223. exit(EXIT_FAILURE);
  224. }
  225. else if(NULL == ret)
  226. {
  227. create_struct();
  228. return NULL;
  229. }
  230. global_token = global_token->next;
  231. while(global_token->s[0] == '*')
  232. {
  233. ret = ret->indirect;
  234. global_token = global_token->next;
  235. }
  236. return ret;
  237. }