cc_types.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2020 deesix <deesix@tuta.io>
  3. * This file is part of M2-Planet.
  4. *
  5. * M2-Planet is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * M2-Planet is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "cc.h"
  19. /* Imported functions */
  20. int numerate_string(char *a);
  21. void line_error();
  22. void require(int bool, char* error);
  23. /* enable easy primitive extension */
  24. struct type* add_primitive(struct type* a)
  25. {
  26. if(NULL == prim_types) return a;
  27. struct type* i = prim_types;
  28. while(NULL != i->next)
  29. {
  30. i = i->next;
  31. }
  32. i->next = a;
  33. return prim_types;
  34. }
  35. /* enable easy primitive creation */
  36. struct type* new_primitive(char* name0, char* name1, char* name2, int size, int sign)
  37. {
  38. /* Create type** */
  39. struct type* a = calloc(1, sizeof(struct type));
  40. require(NULL != a, "Exhusted memory while declaring new primitive**\n");
  41. a->name = name2;
  42. a->size = register_size;
  43. a->indirect = a;
  44. a->is_signed = sign;
  45. /* Create type* */
  46. struct type* b = calloc(1, sizeof(struct type));
  47. require(NULL != b, "Exhusted memory while declaring new primitive*\n");
  48. b->name = name1;
  49. b->size = register_size;
  50. b->is_signed = sign;
  51. b->indirect = a;
  52. b->type = b;
  53. a->type = b;
  54. struct type* r = calloc(1, sizeof(struct type));
  55. require(NULL != r, "Exhusted memory while declaring new primitive\n");
  56. r->name = name0;
  57. r->size = size;
  58. r->is_signed = sign;
  59. r->indirect = b;
  60. r->type = r;
  61. return r;
  62. }
  63. /* Initialize default types */
  64. void initialize_types()
  65. {
  66. if(AMD64 == Architecture || AARCH64 == Architecture) register_size = 8;
  67. else register_size = 4;
  68. /* Define void */
  69. struct type* hold = new_primitive("void", "void*", "void**", register_size, FALSE);
  70. prim_types = add_primitive(hold);
  71. /* Define unsigned LONG */
  72. hold = new_primitive("SCM","SCM*", "SCM**", register_size, FALSE);
  73. prim_types = add_primitive(hold);
  74. /* Define LONG */
  75. hold = new_primitive("long", "long*", "long**", register_size, TRUE);
  76. prim_types = add_primitive(hold);
  77. /* Define UNSIGNED */
  78. hold = new_primitive("unsigned", "unsigned*", "unsigned**",register_size, FALSE);
  79. prim_types = add_primitive(hold);
  80. /* Define int */
  81. integer = new_primitive("int", "int*", "int**", register_size, TRUE);
  82. prim_types = add_primitive(integer);
  83. /* Define char */
  84. hold = new_primitive("char", "char*", "char**", 1, TRUE);
  85. prim_types = add_primitive(hold);
  86. /* Define FUNCTION */
  87. hold = new_primitive("FUNCTION", "FUNCTION*", "FUNCTION**", register_size, FALSE);
  88. prim_types = add_primitive(hold);
  89. if(BOOTSTRAP_MODE)
  90. {
  91. /* Define FILE */
  92. hold = new_primitive("FILE", "FILE*", "FILE**", register_size, TRUE);
  93. prim_types = add_primitive(hold);
  94. /* Primitives mes.c wanted */
  95. hold = new_primitive("size_t", "size_t*", "size_t**", register_size, FALSE);
  96. prim_types = add_primitive(hold);
  97. hold = new_primitive("ssize_t", "ssize_t*", "ssize_t**", register_size, FALSE);
  98. prim_types = add_primitive(hold);
  99. }
  100. global_types = prim_types;
  101. }
  102. struct type* lookup_type(char* s, struct type* start)
  103. {
  104. struct type* i;
  105. for(i = start; NULL != i; i = i->next)
  106. {
  107. if(match(i->name, s))
  108. {
  109. return i;
  110. }
  111. }
  112. return NULL;
  113. }
  114. struct type* lookup_member(struct type* parent, char* name)
  115. {
  116. struct type* i;
  117. require(NULL != parent, "Not a valid struct type\n");
  118. for(i = parent->members; NULL != i; i = i->members)
  119. {
  120. if(match(i->name, name)) return i;
  121. }
  122. file_print("ERROR in lookup_member ", stderr);
  123. file_print(parent->name, stderr);
  124. file_print("->", stderr);
  125. file_print(global_token->s, stderr);
  126. file_print(" does not exist\n", stderr);
  127. line_error();
  128. file_print("\n", stderr);
  129. exit(EXIT_FAILURE);
  130. }
  131. struct type* type_name();
  132. void require_match(char* message, char* required);
  133. int member_size;
  134. struct type* build_member(struct type* last, int offset)
  135. {
  136. struct type* i = calloc(1, sizeof(struct type));
  137. require(NULL != i, "Exhusted memory while building a struct member\n");
  138. i->members = last;
  139. i->offset = offset;
  140. struct type* member_type = type_name();
  141. require(NULL != member_type, "struct member type can not be invalid\n");
  142. i->type = member_type;
  143. i->name = global_token->s;
  144. global_token = global_token->next;
  145. require(NULL != global_token, "struct member can not be EOF terminated\n");
  146. /* Check to see if array */
  147. if(match( "[", global_token->s))
  148. {
  149. global_token = global_token->next;
  150. require(NULL != global_token, "struct member arrays can not be EOF sized\n");
  151. i->size = member_type->type->size * numerate_string(global_token->s);
  152. if(0 == i->size)
  153. {
  154. file_print("Struct only supports [num] form\n", stderr);
  155. exit(EXIT_FAILURE);
  156. }
  157. global_token = global_token->next;
  158. require_match("Struct only supports [num] form\n", "]");
  159. }
  160. else
  161. {
  162. i->size = member_type->size;
  163. }
  164. member_size = i->size;
  165. return i;
  166. }
  167. struct type* build_union(struct type* last, int offset)
  168. {
  169. int size = 0;
  170. global_token = global_token->next;
  171. require_match("ERROR in build_union\nMissing {\n", "{");
  172. while('}' != global_token->s[0])
  173. {
  174. last = build_member(last, offset);
  175. if(member_size > size)
  176. {
  177. size = member_size;
  178. }
  179. require_match("ERROR in build_union\nMissing ;\n", ";");
  180. require(NULL != global_token, "Unterminated union\n");
  181. }
  182. member_size = size;
  183. global_token = global_token->next;
  184. return last;
  185. }
  186. void create_struct()
  187. {
  188. int offset = 0;
  189. member_size = 0;
  190. struct type* head = calloc(1, sizeof(struct type));
  191. require(NULL != head, "Exhusted memory while creating a struct\n");
  192. struct type* i = calloc(1, sizeof(struct type));
  193. require(NULL != i, "Exhusted memory while creating a struct indirection\n");
  194. head->name = global_token->s;
  195. head->type = head;
  196. i->name = global_token->s;
  197. i->type = i;
  198. head->indirect = i;
  199. i->indirect = head;
  200. head->next = global_types;
  201. global_types = head;
  202. global_token = global_token->next;
  203. i->size = register_size;
  204. require_match("ERROR in create_struct\n Missing {\n", "{");
  205. struct type* last = NULL;
  206. while('}' != global_token->s[0])
  207. {
  208. if(match(global_token->s, "union"))
  209. {
  210. last = build_union(last, offset);
  211. }
  212. else
  213. {
  214. last = build_member(last, offset);
  215. }
  216. offset = offset + member_size;
  217. require_match("ERROR in create_struct\n Missing ;\n", ";");
  218. require(NULL != global_token, "Unterminated struct\n");
  219. }
  220. global_token = global_token->next;
  221. require_match("ERROR in create_struct\n Missing ;\n", ";");
  222. head->size = offset;
  223. head->members = last;
  224. i->members = last;
  225. }
  226. struct type* type_name()
  227. {
  228. struct type* ret;
  229. require(NULL != global_token, "Recieved EOF instead of type name\n");
  230. if(match("struct", global_token->s))
  231. {
  232. global_token = global_token->next;
  233. require(NULL != global_token, "structs can not have a EOF type name\n");
  234. ret = lookup_type(global_token->s, global_types);
  235. if(NULL == ret)
  236. {
  237. create_struct();
  238. return NULL;
  239. }
  240. }
  241. else
  242. {
  243. ret = lookup_type(global_token->s, global_types);
  244. if(NULL == ret)
  245. {
  246. file_print("Unknown type ", stderr);
  247. file_print(global_token->s, stderr);
  248. file_print("\n", stderr);
  249. line_error();
  250. exit(EXIT_FAILURE);
  251. }
  252. }
  253. global_token = global_token->next;
  254. require(NULL != global_token, "unfinished type definition\n");
  255. if(match("const", global_token->s))
  256. {
  257. global_token = global_token->next;
  258. require(NULL != global_token, "unfinished type definition in const\n");
  259. }
  260. while(global_token->s[0] == '*')
  261. {
  262. ret = ret->indirect;
  263. global_token = global_token->next;
  264. require(NULL != global_token, "unfinished type definition in indirection\n");
  265. }
  266. return ret;
  267. }
  268. struct type* mirror_type(struct type* source, char* name)
  269. {
  270. struct type* head = calloc(1, sizeof(struct type));
  271. require(NULL != head, "Exhusted memory while creating a struct\n");
  272. struct type* i = calloc(1, sizeof(struct type));
  273. require(NULL != i, "Exhusted memory while creating a struct indirection\n");
  274. head->name = name;
  275. i->name = name;
  276. head->size = source->size;
  277. i->size = source->indirect->size;
  278. head->offset = source->offset;
  279. i->offset = source->indirect->offset;
  280. head->is_signed = source->is_signed;
  281. i->is_signed = source->indirect->is_signed;
  282. head->indirect = i;
  283. i->indirect = head;
  284. head->members = source->members;
  285. i->members = source->indirect->members;
  286. head->type = head;
  287. i->type = i;
  288. return head;
  289. }