cc_types.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 strtoint(char *a);
  21. void line_error(void);
  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, "Exhausted 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, "Exhausted 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. a->type = b;
  53. struct type* r = calloc(1, sizeof(struct type));
  54. require(NULL != r, "Exhausted memory while declaring new primitive\n");
  55. r->name = name0;
  56. r->size = size;
  57. r->is_signed = sign;
  58. r->indirect = b;
  59. r->type = r;
  60. b->type = r;
  61. return r;
  62. }
  63. /* Initialize default types */
  64. void initialize_types(void)
  65. {
  66. if(AMD64 == Architecture || AARCH64 == Architecture || RISCV64 == 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 uint32_t */
  84. hold = new_primitive("uint32_t", "uint32_t*", "uint32_t**", 4, FALSE);
  85. prim_types = add_primitive(hold);
  86. /* Define int32_t */
  87. hold = new_primitive("int32_t", "int32_t*", "int32_t**", 4, TRUE);
  88. prim_types = add_primitive(hold);
  89. /* Define uint16_t */
  90. hold = new_primitive("uint16_t", "uint16_t*", "uint16_t**", 2, FALSE);
  91. prim_types = add_primitive(hold);
  92. /* Define int16_t */
  93. hold = new_primitive("int16_t", "int16_t*", "int16_t**", 2, TRUE);
  94. prim_types = add_primitive(hold);
  95. /* Define uint8_t */
  96. hold = new_primitive("uint8_t", "uint8_t*", "uint8_t**", 1, FALSE);
  97. prim_types = add_primitive(hold);
  98. /* Define int8_t */
  99. hold = new_primitive("int8_t", "int8_t*", "int8_t**", 1, TRUE);
  100. prim_types = add_primitive(hold);
  101. /* Define char */
  102. hold = new_primitive("char", "char*", "char**", 1, TRUE);
  103. prim_types = add_primitive(hold);
  104. /* Define _Bool */
  105. hold = new_primitive("_Bool", "_Bool*", "_Bool**", 1, TRUE);
  106. prim_types = add_primitive(hold);
  107. /* Define FUNCTION */
  108. hold = new_primitive("FUNCTION", "FUNCTION*", "FUNCTION**", register_size, FALSE);
  109. prim_types = add_primitive(hold);
  110. if(BOOTSTRAP_MODE)
  111. {
  112. /* Define FILE */
  113. hold = new_primitive("FILE", "FILE*", "FILE**", register_size, TRUE);
  114. prim_types = add_primitive(hold);
  115. /* Primitives mes.c wanted */
  116. hold = new_primitive("size_t", "size_t*", "size_t**", register_size, FALSE);
  117. prim_types = add_primitive(hold);
  118. hold = new_primitive("ssize_t", "ssize_t*", "ssize_t**", register_size, FALSE);
  119. prim_types = add_primitive(hold);
  120. }
  121. global_types = prim_types;
  122. }
  123. struct type* lookup_type(char* s, struct type* start)
  124. {
  125. struct type* i;
  126. for(i = start; NULL != i; i = i->next)
  127. {
  128. if(match(i->name, s))
  129. {
  130. return i;
  131. }
  132. }
  133. return NULL;
  134. }
  135. struct type* lookup_member(struct type* parent, char* name)
  136. {
  137. struct type* i;
  138. require(NULL != parent, "Not a valid struct type\n");
  139. for(i = parent->members; NULL != i; i = i->members)
  140. {
  141. if(match(i->name, name)) return i;
  142. }
  143. fputs("ERROR in lookup_member ", stderr);
  144. fputs(parent->name, stderr);
  145. fputs("->", stderr);
  146. fputs(global_token->s, stderr);
  147. fputs(" does not exist\n", stderr);
  148. line_error();
  149. fputs("\n", stderr);
  150. exit(EXIT_FAILURE);
  151. }
  152. struct type* type_name(void);
  153. void require_match(char* message, char* required);
  154. int member_size;
  155. struct type* build_member(struct type* last, int offset)
  156. {
  157. struct type* i = calloc(1, sizeof(struct type));
  158. require(NULL != i, "Exhausted memory while building a struct member\n");
  159. i->members = last;
  160. i->offset = offset;
  161. struct type* member_type = type_name();
  162. require(NULL != member_type, "struct member type can not be invalid\n");
  163. i->type = member_type;
  164. i->name = global_token->s;
  165. global_token = global_token->next;
  166. require(NULL != global_token, "struct member can not be EOF terminated\n");
  167. /* Check to see if array */
  168. if(match( "[", global_token->s))
  169. {
  170. global_token = global_token->next;
  171. require(NULL != global_token, "struct member arrays can not be EOF sized\n");
  172. i->size = member_type->type->size * strtoint(global_token->s);
  173. if(0 == i->size)
  174. {
  175. fputs("Struct only supports [num] form\n", stderr);
  176. exit(EXIT_FAILURE);
  177. }
  178. global_token = global_token->next;
  179. require_match("Struct only supports [num] form\n", "]");
  180. }
  181. else
  182. {
  183. i->size = member_type->size;
  184. }
  185. member_size = i->size;
  186. return i;
  187. }
  188. struct type* build_union(struct type* last, int offset)
  189. {
  190. int size = 0;
  191. global_token = global_token->next;
  192. require_match("ERROR in build_union\nMissing {\n", "{");
  193. while('}' != global_token->s[0])
  194. {
  195. last = build_member(last, offset);
  196. if(member_size > size)
  197. {
  198. size = member_size;
  199. }
  200. require_match("ERROR in build_union\nMissing ;\n", ";");
  201. require(NULL != global_token, "Unterminated union\n");
  202. }
  203. member_size = size;
  204. global_token = global_token->next;
  205. return last;
  206. }
  207. void create_struct(void)
  208. {
  209. int offset = 0;
  210. member_size = 0;
  211. struct type* head = calloc(1, sizeof(struct type));
  212. require(NULL != head, "Exhausted memory while creating a struct\n");
  213. struct type* i = calloc(1, sizeof(struct type));
  214. require(NULL != i, "Exhausted memory while creating a struct indirection\n");
  215. struct type* ii = calloc(1, sizeof(struct type));
  216. require(NULL != ii, "Exhausted memory while creating a struct double indirection\n");
  217. head->name = global_token->s;
  218. head->type = head;
  219. head->indirect = i;
  220. head->next = global_types;
  221. i->name = global_token->s;
  222. i->type = head;
  223. i->indirect = ii;
  224. i->size = register_size;
  225. ii->name = global_token->s;
  226. ii->type = i;
  227. ii->indirect = ii;
  228. ii->size = register_size;
  229. global_types = head;
  230. global_token = global_token->next;
  231. require_match("ERROR in create_struct\n Missing {\n", "{");
  232. struct type* last = NULL;
  233. require(NULL != global_token, "Incomplete struct definition at end of file\n");
  234. while('}' != global_token->s[0])
  235. {
  236. if(match(global_token->s, "union"))
  237. {
  238. last = build_union(last, offset);
  239. }
  240. else
  241. {
  242. last = build_member(last, offset);
  243. }
  244. offset = offset + member_size;
  245. require_match("ERROR in create_struct\n Missing ;\n", ";");
  246. require(NULL != global_token, "Unterminated struct\n");
  247. }
  248. global_token = global_token->next;
  249. require_match("ERROR in create_struct\n Missing ;\n", ";");
  250. head->size = offset;
  251. head->members = last;
  252. i->members = last;
  253. }
  254. void create_enum(void)
  255. {
  256. maybe_bootstrap_error("enum statement");
  257. struct type* head = calloc(1, sizeof(struct type));
  258. require(NULL != head, "Exhausted memory while creating an enum\n");
  259. struct type* i = calloc(1, sizeof(struct type));
  260. require(NULL != i, "Exhausted memory while creating a enum indirection\n");
  261. struct type* ii = calloc(1, sizeof(struct type));
  262. require(NULL != ii, "Exhausted memory while creating a enum double indirection\n");
  263. /* Anonymous enums */
  264. if(!match("{", global_token->s))
  265. {
  266. head->name = global_token->s;
  267. head->type = head;
  268. head->indirect = i;
  269. head->next = global_types;
  270. head->size = register_size; /* We treat enums as always being ints. */
  271. head->is_signed = TRUE;
  272. i->name = global_token->s;
  273. i->type = head;
  274. i->indirect = ii;
  275. i->size = register_size;
  276. ii->name = global_token->s;
  277. ii->type = i;
  278. ii->indirect = ii;
  279. ii->size = register_size;
  280. global_types = head;
  281. global_token = global_token->next;
  282. }
  283. require_match("ERROR in create_enum\n Missing {\n", "{");
  284. require(NULL != global_token, "Incomplete enum definition at end of file\n");
  285. int next_enum_value = 0;
  286. while('}' != global_token->s[0])
  287. {
  288. global_constant_list = sym_declare(global_token->s, NULL, global_constant_list);
  289. global_token = global_token->next;
  290. require(NULL != global_token, "Incomplete enumerator definition at end of file\n");
  291. if(match("=", global_token->s))
  292. {
  293. global_token = global_token->next;
  294. require(NULL != global_token, "Incomplete enumerator value at end of file\n");
  295. next_enum_value = strtoint(global_token->s) + 1;
  296. }
  297. else
  298. {
  299. global_token->s = int2str(next_enum_value, 10, TRUE);
  300. next_enum_value = next_enum_value + 1;
  301. }
  302. global_constant_list->arguments = global_token;
  303. global_token = global_token->next;
  304. require(NULL != global_token, "Incomplete enumerator termination at end of file\n");
  305. if(match(";", global_token->s))
  306. {
  307. /* The last enumerator did not have an enumerator-expression nor a closing comma
  308. * so we overwrote the closing curly brace with the int2str value. */
  309. global_token = global_token->next;
  310. return;
  311. }
  312. else if(match(",", global_token->s))
  313. {
  314. global_token = global_token->next;
  315. require(NULL != global_token, "Incomplete enumerator comma at end of file\n");
  316. }
  317. require(NULL != global_token, "Unterminated enum\n");
  318. }
  319. global_token = global_token->next;
  320. require_match("ERROR in create_enum\n Missing ;\n", ";");
  321. }
  322. struct type* type_name(void)
  323. {
  324. struct type* ret;
  325. require(NULL != global_token, "Received EOF instead of type name\n");
  326. if(match("extern", global_token->s))
  327. {
  328. global_token = global_token->next;
  329. require(NULL != global_token, "unfinished type definition in extern\n");
  330. }
  331. if(match("const", global_token->s))
  332. {
  333. global_token = global_token->next;
  334. require(NULL != global_token, "unfinished type definition in const\n");
  335. }
  336. if(match("struct", global_token->s))
  337. {
  338. global_token = global_token->next;
  339. require(NULL != global_token, "structs can not have a EOF type name\n");
  340. ret = lookup_type(global_token->s, global_types);
  341. if(NULL == ret)
  342. {
  343. create_struct();
  344. return NULL;
  345. }
  346. }
  347. else if(match("enum", global_token->s))
  348. {
  349. maybe_bootstrap_error("enum statements");
  350. global_token = global_token->next;
  351. require(NULL != global_token, "enums can not have a EOF type name\n");
  352. ret = lookup_type(global_token->s, global_types);
  353. if(NULL == ret)
  354. {
  355. create_enum();
  356. return NULL;
  357. }
  358. }
  359. else
  360. {
  361. ret = lookup_type(global_token->s, global_types);
  362. if(NULL == ret)
  363. {
  364. fputs("Unknown type ", stderr);
  365. fputs(global_token->s, stderr);
  366. fputs("\n", stderr);
  367. line_error();
  368. fputs("\n", stderr);
  369. exit(EXIT_FAILURE);
  370. }
  371. }
  372. global_token = global_token->next;
  373. require(NULL != global_token, "unfinished type definition\n");
  374. if(match("const", global_token->s))
  375. {
  376. global_token = global_token->next;
  377. require(NULL != global_token, "unfinished type definition in const\n");
  378. }
  379. while(global_token->s[0] == '*')
  380. {
  381. ret = ret->indirect;
  382. global_token = global_token->next;
  383. require(NULL != global_token, "unfinished type definition in indirection\n");
  384. }
  385. return ret;
  386. }
  387. struct type* mirror_type(struct type* source, char* name)
  388. {
  389. struct type* head = calloc(1, sizeof(struct type));
  390. require(NULL != head, "Exhausted memory while creating a struct\n");
  391. struct type* i = calloc(1, sizeof(struct type));
  392. require(NULL != i, "Exhausted memory while creating a struct indirection\n");
  393. head->name = name;
  394. i->name = name;
  395. head->size = source->size;
  396. i->size = source->indirect->size;
  397. head->offset = source->offset;
  398. i->offset = source->indirect->offset;
  399. head->is_signed = source->is_signed;
  400. i->is_signed = source->indirect->is_signed;
  401. head->indirect = i;
  402. i->indirect = head;
  403. head->members = source->members;
  404. i->members = source->indirect->members;
  405. head->type = head;
  406. i->type = i;
  407. return head;
  408. }