codegen.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #include "ssa.cpp"
  2. #include "print_llvm.cpp"
  3. struct ssaGen {
  4. ssaModule module;
  5. gbFile output_file;
  6. };
  7. b32 ssa_gen_init(ssaGen *s, Checker *c) {
  8. if (global_error_collector.count != 0) {
  9. return false;
  10. }
  11. isize tc = c->parser->total_token_count;
  12. if (tc < 2) {
  13. return false;
  14. }
  15. ssa_init_module(&s->module, c);
  16. s->module.generate_debug_info = false;
  17. // TODO(bill): generate appropriate output name
  18. int pos = cast(int)string_extension_position(c->parser->init_fullpath);
  19. gbFileError err = gb_file_create(&s->output_file, gb_bprintf("%.*s.ll", pos, c->parser->init_fullpath.text));
  20. if (err != gbFileError_None) {
  21. return false;
  22. }
  23. return true;
  24. }
  25. void ssa_gen_destroy(ssaGen *s) {
  26. ssa_destroy_module(&s->module);
  27. gb_file_close(&s->output_file);
  28. }
  29. String ssa_mangle_name(ssaGen *s, String path, String name) {
  30. // NOTE(bill): prefix names not in the init scope
  31. // TODO(bill): make robust and not just rely on the file's name
  32. ssaModule *m = &s->module;
  33. CheckerInfo *info = m->info;
  34. gbAllocator a = m->allocator;
  35. AstFile *file = *map_get(&info->files, hash_string(path));
  36. char *str = gb_alloc_array(a, char, path.len+1);
  37. gb_memmove(str, path.text, path.len);
  38. str[path.len] = 0;
  39. for (isize i = 0; i < path.len; i++) {
  40. if (str[i] == '\\') {
  41. str[i] = '/';
  42. }
  43. }
  44. char const *base = gb_path_base_name(str);
  45. char const *ext = gb_path_extension(base);
  46. isize base_len = ext-1-base;
  47. isize max_len = base_len + 1 + 10 + 1 + name.len;
  48. u8 *new_name = gb_alloc_array(a, u8, max_len);
  49. isize new_name_len = gb_snprintf(
  50. cast(char *)new_name, max_len,
  51. "%.*s-%u.%.*s",
  52. cast(int)base_len, base,
  53. file->id,
  54. LIT(name));
  55. return make_string(new_name, new_name_len-1);
  56. }
  57. void ssa_gen_tree(ssaGen *s) {
  58. ssaModule *m = &s->module;
  59. CheckerInfo *info = m->info;
  60. gbAllocator a = m->allocator;
  61. if (v_zero == NULL) {
  62. v_zero = ssa_make_const_int (m->allocator, 0);
  63. v_one = ssa_make_const_int (m->allocator, 1);
  64. v_zero32 = ssa_make_const_i32 (m->allocator, 0);
  65. v_one32 = ssa_make_const_i32 (m->allocator, 1);
  66. v_two32 = ssa_make_const_i32 (m->allocator, 2);
  67. v_false = ssa_make_const_bool(m->allocator, false);
  68. v_true = ssa_make_const_bool(m->allocator, true);
  69. }
  70. isize global_variable_max_count = 0;
  71. Entity *entry_point = NULL;
  72. for_array(i, info->entities.entries) {
  73. auto *entry = &info->entities.entries[i];
  74. Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
  75. String name = e->token.string;
  76. if (e->kind == Entity_Variable) {
  77. global_variable_max_count++;
  78. } else if (e->kind == Entity_Procedure) {
  79. if (e->scope->is_init && name == "main") {
  80. entry_point = e;
  81. }
  82. }
  83. }
  84. struct ssaGlobalVariable {
  85. ssaValue *var, *init;
  86. DeclInfo *decl;
  87. };
  88. Array<ssaGlobalVariable> global_variables;
  89. array_init(&global_variables, m->tmp_allocator, global_variable_max_count);
  90. auto min_dep_map = generate_minimum_dependency_map(info, entry_point);
  91. defer (map_destroy(&min_dep_map));
  92. for_array(i, info->entities.entries) {
  93. auto *entry = &info->entities.entries[i];
  94. Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
  95. String name = e->token.string;
  96. DeclInfo *decl = entry->value;
  97. Scope *scope = e->scope;
  98. if (!scope->is_file) {
  99. continue;
  100. }
  101. if (map_get(&min_dep_map, hash_pointer(e)) == NULL) {
  102. // NOTE(bill): Nothing depends upon it so doesn't need to be built
  103. continue;
  104. }
  105. if (!scope->is_global && !scope->is_init) {
  106. name = ssa_mangle_name(s, e->token.pos.file, name);
  107. }
  108. switch (e->kind) {
  109. case Entity_TypeName:
  110. GB_ASSERT(e->type->kind == Type_Named);
  111. map_set(&m->type_names, hash_pointer(e->type), name);
  112. ssa_gen_global_type_name(m, e, name);
  113. break;
  114. case Entity_Variable: {
  115. ssaValue *g = ssa_make_value_global(a, e, NULL);
  116. if (decl->var_decl_tags & VarDeclTag_thread_local) {
  117. g->Global.is_thread_local = true;
  118. }
  119. ssaGlobalVariable var = {};
  120. var.var = g;
  121. var.decl = decl;
  122. if (decl->init_expr != NULL) {
  123. TypeAndValue *tav = map_get(&info->types, hash_pointer(decl->init_expr));
  124. if (tav != NULL) {
  125. if (tav->value.kind != ExactValue_Invalid) {
  126. ExactValue v = tav->value;
  127. if (v.kind != ExactValue_String) {
  128. g->Global.value = ssa_add_module_constant(m, tav->type, v);
  129. }
  130. }
  131. }
  132. }
  133. if (g->Global.value == NULL) {
  134. array_add(&global_variables, var);
  135. }
  136. map_set(&m->values, hash_pointer(e), g);
  137. map_set(&m->members, hash_string(name), g);
  138. } break;
  139. case Entity_Procedure: {
  140. auto *pd = &decl->proc_decl->ProcDecl;
  141. String original_name = name;
  142. AstNode *body = pd->body;
  143. if (pd->tags & ProcTag_foreign) {
  144. name = pd->name->Ident.string;
  145. }
  146. if (pd->foreign_name.len > 0) {
  147. name = pd->foreign_name;
  148. } else if (pd->link_name.len > 0) {
  149. name = pd->link_name;
  150. }
  151. ssaValue *p = ssa_make_value_procedure(a, m, e, e->type, decl->type_expr, body, name);
  152. p->Proc.tags = pd->tags;
  153. map_set(&m->values, hash_pointer(e), p);
  154. HashKey hash_name = hash_string(name);
  155. if (map_get(&m->members, hash_name) == NULL) {
  156. map_set(&m->members, hash_name, p);
  157. }
  158. } break;
  159. }
  160. }
  161. for_array(i, m->members.entries) {
  162. auto *entry = &m->members.entries[i];
  163. ssaValue *v = entry->value;
  164. if (v->kind == ssaValue_Proc)
  165. ssa_build_proc(v, NULL);
  166. }
  167. ssaDebugInfo *compile_unit = m->debug_info.entries[0].value;
  168. GB_ASSERT(compile_unit->kind == ssaDebugInfo_CompileUnit);
  169. ssaDebugInfo *all_procs = ssa_alloc_debug_info(m->allocator, ssaDebugInfo_AllProcs);
  170. isize all_proc_max_count = 0;
  171. for_array(i, m->debug_info.entries) {
  172. auto *entry = &m->debug_info.entries[i];
  173. ssaDebugInfo *di = entry->value;
  174. di->id = i;
  175. if (di->kind == ssaDebugInfo_Proc) {
  176. all_proc_max_count++;
  177. }
  178. }
  179. array_init(&all_procs->AllProcs.procs, m->allocator, all_proc_max_count);
  180. map_set(&m->debug_info, hash_pointer(all_procs), all_procs); // NOTE(bill): This doesn't need to be mapped
  181. compile_unit->CompileUnit.all_procs = all_procs;
  182. for_array(i, m->debug_info.entries) {
  183. auto *entry = &m->debug_info.entries[i];
  184. ssaDebugInfo *di = entry->value;
  185. di->id = i;
  186. if (di->kind == ssaDebugInfo_Proc) {
  187. array_add(&all_procs->AllProcs.procs, di);
  188. }
  189. }
  190. { // Startup Runtime
  191. // Cleanup(bill): probably better way of doing code insertion
  192. String name = make_string(SSA_STARTUP_RUNTIME_PROC_NAME);
  193. Type *proc_type = make_type_proc(a, gb_alloc_item(a, Scope),
  194. NULL, 0,
  195. NULL, 0, false);
  196. AstNode *body = gb_alloc_item(a, AstNode);
  197. ssaValue *p = ssa_make_value_procedure(a, m, NULL, proc_type, NULL, body, name);
  198. Token token = {};
  199. token.string = name;
  200. Entity *e = make_entity_procedure(a, NULL, token, proc_type);
  201. map_set(&m->values, hash_pointer(e), p);
  202. map_set(&m->members, hash_string(name), p);
  203. ssaProcedure *proc = &p->Proc;
  204. proc->tags = ProcTag_no_inline; // TODO(bill): is no_inline a good idea?
  205. ssa_begin_procedure_body(proc);
  206. // TODO(bill): Should do a dependency graph do check which order to initialize them in?
  207. for_array(i, global_variables) {
  208. ssaGlobalVariable *var = &global_variables[i];
  209. if (var->decl->init_expr != NULL) {
  210. var->init = ssa_build_expr(proc, var->decl->init_expr);
  211. }
  212. }
  213. // NOTE(bill): Initialize constants first
  214. for_array(i, global_variables) {
  215. ssaGlobalVariable *var = &global_variables[i];
  216. if (var->init != NULL) {
  217. if (var->init->kind == ssaValue_Constant) {
  218. ssa_emit_store(proc, var->var, var->init);
  219. }
  220. }
  221. }
  222. for_array(i, global_variables) {
  223. ssaGlobalVariable *var = &global_variables[i];
  224. if (var->init != NULL) {
  225. if (var->init->kind != ssaValue_Constant) {
  226. ssa_emit_store(proc, var->var, var->init);
  227. }
  228. }
  229. }
  230. { // NOTE(bill): Setup type_info data
  231. // TODO(bill): Try and make a lot of this constant aggregate literals in LLVM IR
  232. ssaValue *type_info_data = NULL;
  233. ssaValue *type_info_member_data = NULL;
  234. ssaValue **found = NULL;
  235. found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_NAME)));
  236. GB_ASSERT(found != NULL);
  237. type_info_data = *found;
  238. found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_MEMBER_NAME)));
  239. GB_ASSERT(found != NULL);
  240. type_info_member_data = *found;
  241. CheckerInfo *info = proc->module->info;
  242. // Useful types
  243. Type *t_int_ptr = make_type_pointer(a, t_int);
  244. Type *t_i64_ptr = make_type_pointer(a, t_i64);
  245. Type *t_bool_ptr = make_type_pointer(a, t_bool);
  246. Type *t_string_ptr = make_type_pointer(a, t_string);
  247. Type *t_type_info_ptr_ptr = make_type_pointer(a, t_type_info_ptr);
  248. Type *t_i64_slice_ptr = make_type_pointer(a, make_type_slice(a, t_i64));
  249. Type *t_string_slice_ptr = make_type_pointer(a, make_type_slice(a, t_string));
  250. auto get_type_info_ptr = [](ssaProcedure *proc, ssaValue *type_info_data, Type *type) -> ssaValue * {
  251. return ssa_emit_array_gep(proc, type_info_data,
  252. ssa_type_info_index(proc->module->info, type));
  253. };
  254. isize type_info_member_index = 0;
  255. auto type_info_member_offset = [](ssaProcedure *proc, ssaValue *data, isize count, isize *index) -> ssaValue * {
  256. ssaValue *offset = ssa_emit_array_gep(proc, data, *index);
  257. *index += count;
  258. return offset;
  259. };
  260. for_array(type_info_map_index, info->type_info_map.entries) {
  261. auto *entry = &info->type_info_map.entries[type_info_map_index];
  262. Type *t = cast(Type *)cast(uintptr)entry->key.key;
  263. t = default_type(t);
  264. isize entry_index = entry->value;
  265. ssaValue *tag = NULL;
  266. switch (t->kind) {
  267. case Type_Named: {
  268. tag = ssa_add_local_generated(proc, t_type_info_named);
  269. // TODO(bill): Which is better? The mangled name or actual name?
  270. // ssaValue *gsa = ssa_add_global_string_array(proc, make_exact_value_string(t->Named.name));
  271. ssaValue *gsa = ssa_add_global_string_array(m, t->Named.type_name->token.string);
  272. ssaValue *elem = ssa_array_elem(proc, gsa);
  273. ssaValue *len = ssa_array_len(proc, ssa_emit_load(proc, gsa));
  274. ssaValue *name = ssa_emit_string(proc, elem, len);
  275. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Named.base);
  276. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), name);
  277. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 1), gep);
  278. } break;
  279. case Type_Basic:
  280. switch (t->Basic.kind) {
  281. case Basic_bool:
  282. tag = ssa_add_local_generated(proc, t_type_info_boolean);
  283. break;
  284. case Basic_i8:
  285. case Basic_i16:
  286. case Basic_i32:
  287. case Basic_i64:
  288. case Basic_u8:
  289. case Basic_u16:
  290. case Basic_u32:
  291. case Basic_u64:
  292. case Basic_int:
  293. case Basic_uint: {
  294. tag = ssa_add_local_generated(proc, t_type_info_integer);
  295. b32 is_unsigned = (t->Basic.flags & BasicFlag_Unsigned) != 0;
  296. ssaValue *bits = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
  297. ssaValue *is_signed = ssa_make_const_bool(a, !is_unsigned);
  298. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), bits);
  299. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 1), is_signed);
  300. } break;
  301. case Basic_f32:
  302. case Basic_f64: {
  303. tag = ssa_add_local_generated(proc, t_type_info_float);
  304. ssaValue *bits = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
  305. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), bits);
  306. } break;
  307. case Basic_rawptr:
  308. tag = ssa_add_local_generated(proc, t_type_info_pointer);
  309. break;
  310. case Basic_string:
  311. tag = ssa_add_local_generated(proc, t_type_info_string);
  312. break;
  313. case Basic_any:
  314. tag = ssa_add_local_generated(proc, t_type_info_any);
  315. break;
  316. }
  317. break;
  318. case Type_Pointer: {
  319. tag = ssa_add_local_generated(proc, t_type_info_pointer);
  320. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Pointer.elem);
  321. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), gep);
  322. } break;
  323. case Type_Maybe: {
  324. tag = ssa_add_local_generated(proc, t_type_info_maybe);
  325. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Maybe.elem);
  326. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), gep);
  327. } break;
  328. case Type_Array: {
  329. tag = ssa_add_local_generated(proc, t_type_info_array);
  330. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Array.elem);
  331. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), gep);
  332. isize ez = type_size_of(m->sizes, a, t->Array.elem);
  333. ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, 1);
  334. ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
  335. ssaValue *count = ssa_emit_struct_gep(proc, tag, 2);
  336. ssa_emit_store(proc, count, ssa_make_const_int(a, t->Array.count));
  337. } break;
  338. case Type_Slice: {
  339. tag = ssa_add_local_generated(proc, t_type_info_slice);
  340. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Slice.elem);
  341. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), gep);
  342. isize ez = type_size_of(m->sizes, a, t->Slice.elem);
  343. ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, 1);
  344. ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
  345. } break;
  346. case Type_Vector: {
  347. tag = ssa_add_local_generated(proc, t_type_info_vector);
  348. ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Vector.elem);
  349. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 0), gep);
  350. isize ez = type_size_of(m->sizes, a, t->Vector.elem);
  351. ssaValue *elem_size = ssa_emit_struct_gep(proc, tag, 1);
  352. ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
  353. ssaValue *count = ssa_emit_struct_gep(proc, tag, 2);
  354. ssa_emit_store(proc, count, ssa_make_const_int(a, t->Vector.count));
  355. } break;
  356. case Type_Record: {
  357. switch (t->Record.kind) {
  358. case TypeRecord_Struct: {
  359. tag = ssa_add_local_generated(proc, t_type_info_struct);
  360. {
  361. ssaValue *packed = ssa_make_const_bool(a, t->Record.struct_is_packed);
  362. ssaValue *ordered = ssa_make_const_bool(a, t->Record.struct_is_ordered);
  363. ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
  364. ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
  365. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 1), size);
  366. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 2), align);
  367. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 3), packed);
  368. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 4), ordered);
  369. }
  370. ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
  371. type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet
  372. for (isize source_index = 0; source_index < t->Record.field_count; source_index++) {
  373. // TODO(bill): Order fields in source order not layout order
  374. Entity *f = t->Record.fields_in_src_order[source_index];
  375. ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
  376. i64 foffset = t->Record.struct_offsets[f->Variable.field_index];
  377. GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
  378. ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, source_index));
  379. ssaValue *name = ssa_emit_struct_gep(proc, field, 0);
  380. ssaValue *type_info = ssa_emit_struct_gep(proc, field, 1);
  381. ssaValue *offset = ssa_emit_struct_gep(proc, field, 2);
  382. if (f->token.string.len > 0) {
  383. ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
  384. }
  385. ssa_emit_store(proc, type_info, tip);
  386. ssa_emit_store(proc, offset, ssa_make_const_int(a, foffset));
  387. }
  388. Type *slice_type = make_type_slice(a, t_type_info_member);
  389. Type *slice_type_ptr = make_type_pointer(a, slice_type);
  390. ssaValue *slice = ssa_emit_struct_gep(proc, tag, 0);
  391. ssaValue *field_count = ssa_make_const_int(a, t->Record.field_count);
  392. ssaValue *elem = ssa_emit_struct_gep(proc, slice, 0);
  393. ssaValue *len = ssa_emit_struct_gep(proc, slice, 1);
  394. ssaValue *cap = ssa_emit_struct_gep(proc, slice, 2);
  395. ssa_emit_store(proc, elem, memory);
  396. ssa_emit_store(proc, len, field_count);
  397. ssa_emit_store(proc, cap, field_count);
  398. } break;
  399. case TypeRecord_Union:
  400. tag = ssa_add_local_generated(proc, t_type_info_union);
  401. {
  402. ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
  403. ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
  404. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 1), size);
  405. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 2), align);
  406. }
  407. break;
  408. case TypeRecord_RawUnion: {
  409. tag = ssa_add_local_generated(proc, t_type_info_raw_union);
  410. {
  411. ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
  412. ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
  413. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 1), size);
  414. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 2), align);
  415. }
  416. ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
  417. for (isize i = 0; i < t->Record.field_count; i++) {
  418. ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
  419. ssaValue *name = ssa_emit_struct_gep(proc, field, 0);
  420. ssaValue *type_info = ssa_emit_struct_gep(proc, field, 1);
  421. ssaValue *offset = ssa_emit_struct_gep(proc, field, 2);
  422. Entity *f = t->Record.fields[i];
  423. ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
  424. if (f->token.string.len > 0) {
  425. ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
  426. }
  427. ssa_emit_store(proc, type_info, tip);
  428. ssa_emit_store(proc, offset, ssa_make_const_int(a, 0));
  429. }
  430. Type *slice_type = make_type_slice(a, t_type_info_member);
  431. Type *slice_type_ptr = make_type_pointer(a, slice_type);
  432. ssaValue *slice = ssa_emit_struct_gep(proc, tag, 0);
  433. ssaValue *field_count = ssa_make_const_int(a, t->Record.field_count);
  434. ssaValue *elem = ssa_emit_struct_gep(proc, slice, 0);
  435. ssaValue *len = ssa_emit_struct_gep(proc, slice, 1);
  436. ssaValue *cap = ssa_emit_struct_gep(proc, slice, 2);
  437. ssa_emit_store(proc, elem, memory);
  438. ssa_emit_store(proc, len, field_count);
  439. ssa_emit_store(proc, cap, field_count);
  440. } break;
  441. case TypeRecord_Enum: {
  442. tag = ssa_add_local_generated(proc, t_type_info_enum);
  443. Type *enum_base = t->Record.enum_base;
  444. if (enum_base == NULL) {
  445. enum_base = t_int;
  446. }
  447. ssaValue *base = ssa_emit_struct_gep(proc, tag, 0);
  448. ssa_emit_store(proc, base, get_type_info_ptr(proc, type_info_data, enum_base));
  449. if (t->Record.other_field_count > 0) {
  450. Entity **fields = t->Record.other_fields;
  451. isize count = t->Record.other_field_count;
  452. ssaValue *value_array = NULL;
  453. ssaValue *name_array = NULL;
  454. {
  455. Token token = {Token_Identifier};
  456. i32 id = cast(i32)entry_index;
  457. char name_base[] = "__$enum_values";
  458. isize name_len = gb_size_of(name_base) + 10;
  459. token.string.text = gb_alloc_array(a, u8, name_len);
  460. token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
  461. "%s-%d", name_base, id)-1;
  462. Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_i64, count));
  463. value_array = ssa_make_value_global(a, e, NULL);
  464. value_array->Global.is_private = true;
  465. ssa_module_add_value(m, e, value_array);
  466. map_set(&m->members, hash_string(token.string), value_array);
  467. }
  468. {
  469. Token token = {Token_Identifier};
  470. i32 id = cast(i32)entry_index;
  471. char name_base[] = "__$enum_names";
  472. isize name_len = gb_size_of(name_base) + 10;
  473. token.string.text = gb_alloc_array(a, u8, name_len);
  474. token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
  475. "%s-%d", name_base, id)-1;
  476. Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count));
  477. name_array = ssa_make_value_global(a, e, NULL);
  478. name_array->Global.is_private = true;
  479. ssa_module_add_value(m, e, name_array);
  480. map_set(&m->members, hash_string(token.string), name_array);
  481. }
  482. for (isize i = 0; i < count; i++) {
  483. ssaValue *value_gep = ssa_emit_struct_gep(proc, value_array, i);
  484. ssaValue *name_gep = ssa_emit_struct_gep(proc, name_array, i);
  485. ssa_emit_store(proc, value_gep, ssa_make_const_i64(a, fields[i]->Constant.value.value_integer));
  486. ssa_emit_store(proc, name_gep, ssa_emit_global_string(proc, fields[i]->token.string));
  487. }
  488. ssaValue *v_count = ssa_make_const_int(a, count);
  489. ssaValue *values = ssa_emit_struct_gep(proc, tag, 1);
  490. ssaValue *names = ssa_emit_struct_gep(proc, tag, 2);
  491. ssaValue *value_slice = ssa_add_local_generated(proc, type_deref(t_i64_slice_ptr));
  492. ssaValue *name_slice = ssa_add_local_generated(proc, type_deref(t_string_slice_ptr));
  493. ssa_emit_store(proc, ssa_emit_struct_gep(proc, value_slice, 0), ssa_array_elem(proc, value_array));
  494. ssa_emit_store(proc, ssa_emit_struct_gep(proc, value_slice, 1), v_count);
  495. ssa_emit_store(proc, ssa_emit_struct_gep(proc, value_slice, 2), v_count);
  496. ssa_emit_store(proc, ssa_emit_struct_gep(proc, name_slice, 0), ssa_array_elem(proc, name_array));
  497. ssa_emit_store(proc, ssa_emit_struct_gep(proc, name_slice, 1), v_count);
  498. ssa_emit_store(proc, ssa_emit_struct_gep(proc, name_slice, 2), v_count);
  499. ssa_emit_store(proc, values, ssa_emit_load(proc, value_slice));
  500. ssa_emit_store(proc, names, ssa_emit_load(proc, name_slice));
  501. }
  502. } break;
  503. }
  504. } break;
  505. case Type_Tuple: {
  506. tag = ssa_add_local_generated(proc, t_type_info_tuple);
  507. {
  508. ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
  509. ssa_emit_store(proc, ssa_emit_struct_gep(proc, tag, 2), align);
  510. }
  511. ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Tuple.variable_count, &type_info_member_index);
  512. for (isize i = 0; i < t->Tuple.variable_count; i++) {
  513. ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
  514. ssaValue *name = ssa_emit_struct_gep(proc, field, 0);
  515. ssaValue *type_info = ssa_emit_struct_gep(proc, field, 1);
  516. // NOTE(bill): offset is not used for tuples
  517. Entity *f = t->Tuple.variables[i];
  518. ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
  519. if (f->token.string.len > 0) {
  520. ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
  521. }
  522. ssa_emit_store(proc, type_info, tip);
  523. }
  524. Type *slice_type = make_type_slice(a, t_type_info_member);
  525. Type *slice_type_ptr = make_type_pointer(a, slice_type);
  526. ssaValue *slice = ssa_emit_struct_gep(proc, tag, 0);
  527. ssaValue *variable_count = ssa_make_const_int(a, t->Tuple.variable_count);
  528. ssaValue *elem = ssa_emit_struct_gep(proc, slice, 0);
  529. ssaValue *len = ssa_emit_struct_gep(proc, slice, 1);
  530. ssaValue *cap = ssa_emit_struct_gep(proc, slice, 2);
  531. ssa_emit_store(proc, elem, memory);
  532. ssa_emit_store(proc, len, variable_count);
  533. ssa_emit_store(proc, cap, variable_count);
  534. } break;
  535. case Type_Proc: {
  536. tag = ssa_add_local_generated(proc, t_type_info_procedure);
  537. ssaValue *params = ssa_emit_struct_gep(proc, tag, 0);
  538. ssaValue *results = ssa_emit_struct_gep(proc, tag, 1);
  539. ssaValue *variadic = ssa_emit_struct_gep(proc, tag, 2);
  540. if (t->Proc.params) {
  541. ssa_emit_store(proc, params, get_type_info_ptr(proc, type_info_data, t->Proc.params));
  542. }
  543. if (t->Proc.results) {
  544. ssa_emit_store(proc, results, get_type_info_ptr(proc, type_info_data, t->Proc.results));
  545. }
  546. ssa_emit_store(proc, variadic, ssa_make_const_bool(a, t->Proc.variadic));
  547. // TODO(bill): Type_Info for procedures
  548. } break;
  549. }
  550. if (tag != NULL) {
  551. ssaValue *gep = ssa_emit_array_gep(proc, type_info_data, entry_index);
  552. ssaValue *val = ssa_emit_conv(proc, ssa_emit_load(proc, tag), t_type_info);
  553. ssa_emit_store(proc, gep, val);
  554. }
  555. }
  556. }
  557. ssa_end_procedure_body(proc);
  558. }
  559. for_array(i, m->procs) {
  560. ssa_build_proc(m->procs[i], m->procs[i]->Proc.parent);
  561. }
  562. // m->layout = make_string("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64");
  563. }
  564. void ssa_gen_ir(ssaGen *s) {
  565. ssaFileBuffer buf = {};
  566. ssa_file_buffer_init(&buf, &s->output_file);
  567. defer (ssa_file_buffer_destroy(&buf));
  568. ssa_print_llvm_ir(&buf, &s->module);
  569. }