cc_types.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 FUNCTION */
  105. hold = new_primitive("FUNCTION", "FUNCTION*", "FUNCTION**", register_size, FALSE);
  106. prim_types = add_primitive(hold);
  107. if(BOOTSTRAP_MODE)
  108. {
  109. /* Define FILE */
  110. hold = new_primitive("FILE", "FILE*", "FILE**", register_size, TRUE);
  111. prim_types = add_primitive(hold);
  112. /* Primitives mes.c wanted */
  113. hold = new_primitive("size_t", "size_t*", "size_t**", register_size, FALSE);
  114. prim_types = add_primitive(hold);
  115. hold = new_primitive("ssize_t", "ssize_t*", "ssize_t**", register_size, FALSE);
  116. prim_types = add_primitive(hold);
  117. }
  118. global_types = prim_types;
  119. }
  120. struct type* lookup_type(char* s, struct type* start)
  121. {
  122. struct type* i;
  123. for(i = start; NULL != i; i = i->next)
  124. {
  125. if(match(i->name, s))
  126. {
  127. return i;
  128. }
  129. }
  130. return NULL;
  131. }
  132. struct type* lookup_member(struct type* parent, char* name)
  133. {
  134. struct type* i;
  135. require(NULL != parent, "Not a valid struct type\n");
  136. for(i = parent->members; NULL != i; i = i->members)
  137. {
  138. if(match(i->name, name)) return i;
  139. }
  140. fputs("ERROR in lookup_member ", stderr);
  141. fputs(parent->name, stderr);
  142. fputs("->", stderr);
  143. fputs(global_token->s, stderr);
  144. fputs(" does not exist\n", stderr);
  145. line_error();
  146. fputs("\n", stderr);
  147. exit(EXIT_FAILURE);
  148. }
  149. struct type* type_name(void);
  150. void require_match(char* message, char* required);
  151. int member_size;
  152. struct type* build_member(struct type* last, int offset)
  153. {
  154. struct type* i = calloc(1, sizeof(struct type));
  155. require(NULL != i, "Exhausted memory while building a struct member\n");
  156. i->members = last;
  157. i->offset = offset;
  158. struct type* member_type = type_name();
  159. require(NULL != member_type, "struct member type can not be invalid\n");
  160. i->type = member_type;
  161. i->name = global_token->s;
  162. global_token = global_token->next;
  163. require(NULL != global_token, "struct member can not be EOF terminated\n");
  164. /* Check to see if array */
  165. if(match( "[", global_token->s))
  166. {
  167. global_token = global_token->next;
  168. require(NULL != global_token, "struct member arrays can not be EOF sized\n");
  169. i->size = member_type->type->size * strtoint(global_token->s);
  170. if(0 == i->size)
  171. {
  172. fputs("Struct only supports [num] form\n", stderr);
  173. exit(EXIT_FAILURE);
  174. }
  175. global_token = global_token->next;
  176. require_match("Struct only supports [num] form\n", "]");
  177. }
  178. else
  179. {
  180. i->size = member_type->size;
  181. }
  182. member_size = i->size;
  183. return i;
  184. }
  185. struct type* build_union(struct type* last, int offset)
  186. {
  187. int size = 0;
  188. global_token = global_token->next;
  189. require_match("ERROR in build_union\nMissing {\n", "{");
  190. while('}' != global_token->s[0])
  191. {
  192. last = build_member(last, offset);
  193. if(member_size > size)
  194. {
  195. size = member_size;
  196. }
  197. require_match("ERROR in build_union\nMissing ;\n", ";");
  198. require(NULL != global_token, "Unterminated union\n");
  199. }
  200. member_size = size;
  201. global_token = global_token->next;
  202. return last;
  203. }
  204. void create_struct(void)
  205. {
  206. int offset = 0;
  207. member_size = 0;
  208. struct type* head = calloc(1, sizeof(struct type));
  209. require(NULL != head, "Exhausted memory while creating a struct\n");
  210. struct type* i = calloc(1, sizeof(struct type));
  211. require(NULL != i, "Exhausted memory while creating a struct indirection\n");
  212. struct type* ii = calloc(1, sizeof(struct type));
  213. require(NULL != ii, "Exhausted memory while creating a struct double indirection\n");
  214. head->name = global_token->s;
  215. head->type = head;
  216. head->indirect = i;
  217. head->next = global_types;
  218. i->name = global_token->s;
  219. i->type = head;
  220. i->indirect = ii;
  221. i->size = register_size;
  222. ii->name = global_token->s;
  223. ii->type = i;
  224. ii->indirect = ii;
  225. ii->size = register_size;
  226. global_types = head;
  227. global_token = global_token->next;
  228. require_match("ERROR in create_struct\n Missing {\n", "{");
  229. struct type* last = NULL;
  230. require(NULL != global_token, "Incomplete struct definition at end of file\n");
  231. while('}' != global_token->s[0])
  232. {
  233. if(match(global_token->s, "union"))
  234. {
  235. last = build_union(last, offset);
  236. }
  237. else
  238. {
  239. last = build_member(last, offset);
  240. }
  241. offset = offset + member_size;
  242. require_match("ERROR in create_struct\n Missing ;\n", ";");
  243. require(NULL != global_token, "Unterminated struct\n");
  244. }
  245. global_token = global_token->next;
  246. require_match("ERROR in create_struct\n Missing ;\n", ";");
  247. head->size = offset;
  248. head->members = last;
  249. i->members = last;
  250. }
  251. struct type* type_name(void)
  252. {
  253. struct type* ret;
  254. require(NULL != global_token, "Received EOF instead of type name\n");
  255. if(match("extern", global_token->s))
  256. {
  257. global_token = global_token->next;
  258. require(NULL != global_token, "unfinished type definition in extern\n");
  259. }
  260. if(match("struct", global_token->s))
  261. {
  262. global_token = global_token->next;
  263. require(NULL != global_token, "structs can not have a EOF type name\n");
  264. ret = lookup_type(global_token->s, global_types);
  265. if(NULL == ret)
  266. {
  267. create_struct();
  268. return NULL;
  269. }
  270. }
  271. else
  272. {
  273. ret = lookup_type(global_token->s, global_types);
  274. if(NULL == ret)
  275. {
  276. fputs("Unknown type ", stderr);
  277. fputs(global_token->s, stderr);
  278. fputs("\n", stderr);
  279. line_error();
  280. fputs("\n", stderr);
  281. exit(EXIT_FAILURE);
  282. }
  283. }
  284. global_token = global_token->next;
  285. require(NULL != global_token, "unfinished type definition\n");
  286. if(match("const", global_token->s))
  287. {
  288. global_token = global_token->next;
  289. require(NULL != global_token, "unfinished type definition in const\n");
  290. }
  291. while(global_token->s[0] == '*')
  292. {
  293. ret = ret->indirect;
  294. global_token = global_token->next;
  295. require(NULL != global_token, "unfinished type definition in indirection\n");
  296. }
  297. return ret;
  298. }
  299. struct type* mirror_type(struct type* source, char* name)
  300. {
  301. struct type* head = calloc(1, sizeof(struct type));
  302. require(NULL != head, "Exhausted memory while creating a struct\n");
  303. struct type* i = calloc(1, sizeof(struct type));
  304. require(NULL != i, "Exhausted memory while creating a struct indirection\n");
  305. head->name = name;
  306. i->name = name;
  307. head->size = source->size;
  308. i->size = source->indirect->size;
  309. head->offset = source->offset;
  310. i->offset = source->indirect->offset;
  311. head->is_signed = source->is_signed;
  312. i->is_signed = source->indirect->is_signed;
  313. head->indirect = i;
  314. i->indirect = head;
  315. head->members = source->members;
  316. i->members = source->indirect->members;
  317. head->type = head;
  318. i->type = i;
  319. return head;
  320. }