ir_print.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. struct irFileBuffer {
  2. gbVirtualMemory vm;
  3. isize offset;
  4. gbFile * output;
  5. };
  6. void ir_file_buffer_init(irFileBuffer *f, gbFile *output) {
  7. isize size = 8*gb_virtual_memory_page_size(nullptr);
  8. f->vm = gb_vm_alloc(nullptr, size);
  9. f->offset = 0;
  10. f->output = output;
  11. }
  12. void ir_file_buffer_destroy(irFileBuffer *f) {
  13. if (f->offset > 0) {
  14. // NOTE(bill): finish writing buffered data
  15. gb_file_write(f->output, f->vm.data, f->offset);
  16. }
  17. gb_vm_free(f->vm);
  18. }
  19. void ir_file_buffer_write(irFileBuffer *f, void *data, isize len) {
  20. if (len > f->vm.size) {
  21. gb_file_write(f->output, data, len);
  22. return;
  23. }
  24. if ((f->vm.size - f->offset) < len) {
  25. gb_file_write(f->output, f->vm.data, f->offset);
  26. f->offset = 0;
  27. }
  28. u8 *cursor = cast(u8 *)f->vm.data + f->offset;
  29. gb_memmove(cursor, data, len);
  30. f->offset += len;
  31. }
  32. void ir_fprintf(irFileBuffer *f, char *fmt, ...) {
  33. va_list va;
  34. va_start(va, fmt);
  35. char buf[4096] = {};
  36. isize len = gb_snprintf_va(buf, gb_size_of(buf), fmt, va);
  37. ir_file_buffer_write(f, buf, len-1);
  38. va_end(va);
  39. }
  40. void ir_fprint_string(irFileBuffer *f, String s) {
  41. ir_file_buffer_write(f, s.text, s.len);
  42. }
  43. void ir_fprint_i128(irFileBuffer *f, i128 i) {
  44. char buf[200] = {};
  45. String str = i128_to_string(i, buf, gb_size_of(buf)-1);
  46. ir_fprint_string(f, str);
  47. }
  48. void ir_file_write(irFileBuffer *f, void *data, isize len) {
  49. ir_file_buffer_write(f, data, len);
  50. }
  51. bool ir_valid_char(u8 c) {
  52. if (c >= 0x80) {
  53. return false;
  54. }
  55. if (gb_char_is_alphanumeric(c)) {
  56. return true;
  57. }
  58. switch (c) {
  59. case '$':
  60. case '-':
  61. case '.':
  62. case '_':
  63. return true;
  64. }
  65. return false;
  66. }
  67. void ir_print_escape_string(irFileBuffer *f, String name, bool print_quotes, bool prefix_with_dot) {
  68. isize extra = 0;
  69. for (isize i = 0; i < name.len; i++) {
  70. u8 c = name[i];
  71. if (!ir_valid_char(c)) {
  72. extra += 2;
  73. }
  74. }
  75. if (extra == 0) {
  76. ir_fprintf(f, "%.*s", LIT(name));
  77. return;
  78. }
  79. char hex_table[] = "0123456789ABCDEF";
  80. isize buf_len = name.len + extra + 2 + 1;
  81. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
  82. u8 *buf = gb_alloc_array(string_buffer_allocator, u8, buf_len);
  83. isize j = 0;
  84. if (print_quotes) {
  85. buf[j++] = '"';
  86. }
  87. if (prefix_with_dot) {
  88. buf[j++] = '.';
  89. }
  90. for (isize i = 0; i < name.len; i++) {
  91. u8 c = name[i];
  92. if (ir_valid_char(c)) {
  93. buf[j++] = c;
  94. } else {
  95. buf[j] = '\\';
  96. buf[j+1] = hex_table[c >> 4];
  97. buf[j+2] = hex_table[c & 0x0f];
  98. j += 3;
  99. }
  100. }
  101. if (print_quotes) {
  102. buf[j++] = '"';
  103. }
  104. ir_file_write(f, buf, j);
  105. gb_temp_arena_memory_end(tmp);
  106. }
  107. void ir_print_encoded_local(irFileBuffer *f, String name) {
  108. ir_fprintf(f, "%%");
  109. ir_print_escape_string(f, name, true, false);
  110. }
  111. void ir_print_encoded_global(irFileBuffer *f, String name, bool remove_prefix) {
  112. ir_fprintf(f, "@");
  113. ir_print_escape_string(f, name, true, !remove_prefix);
  114. }
  115. void ir_print_type(irFileBuffer *f, irModule *m, Type *t);
  116. void ir_print_proc_results(irFileBuffer *f, irModule *m, Type *t) {
  117. GB_ASSERT(is_type_proc(t));
  118. t = base_type(t);
  119. isize result_count = t->Proc.result_count;
  120. if (result_count == 0 || t->Proc.return_by_pointer) {
  121. ir_fprintf(f, "void");
  122. } else {
  123. Type *rt = t->Proc.abi_compat_result_type;
  124. if (!is_type_tuple(rt)) {
  125. ir_print_type(f, m, rt);
  126. } else if (rt->Tuple.variable_count == 1) {
  127. ir_print_type(f, m, rt->Tuple.variables[0]->type);
  128. } else {
  129. isize count = rt->Tuple.variable_count;
  130. ir_fprintf(f, "{");
  131. for (isize i = 0; i < count; i++) {
  132. Entity *e = rt->Tuple.variables[i];
  133. if (i > 0) {
  134. ir_fprintf(f, ", ");
  135. }
  136. ir_print_type(f, m, e->type);
  137. }
  138. ir_fprintf(f, "}");
  139. }
  140. }
  141. }
  142. void ir_print_proc_type_without_pointer(irFileBuffer *f, irModule *m, Type *t) {
  143. i64 word_bits = 8*build_context.word_size;
  144. t = base_type(t);
  145. GB_ASSERT(is_type_proc(t));
  146. isize param_count = t->Proc.param_count;
  147. isize result_count = t->Proc.result_count;
  148. ir_print_proc_results(f, m, t);
  149. ir_fprintf(f, " (");
  150. if (t->Proc.return_by_pointer) {
  151. ir_print_type(f, m, reduce_tuple_to_single_type(t->Proc.results));
  152. ir_fprintf(f, "* sret noalias ");
  153. if (param_count > 0) {
  154. ir_fprintf(f, ", ");
  155. }
  156. }
  157. isize param_index = 0;
  158. for (isize i = 0; i < param_count; i++) {
  159. Entity *e = t->Proc.params->Tuple.variables[i];
  160. if (e->kind != Entity_Variable) continue;
  161. if (param_index > 0) ir_fprintf(f, ", ");
  162. if (i+1 == param_count && t->Proc.c_vararg) {
  163. ir_fprintf(f, "...");
  164. } else {
  165. ir_print_type(f, m, t->Proc.abi_compat_params[i]);
  166. }
  167. param_index++;
  168. }
  169. if (t->Proc.calling_convention == ProcCC_Odin) {
  170. if (param_index > 0) ir_fprintf(f, ", ");
  171. ir_print_type(f, m, t_context_ptr);
  172. }
  173. ir_fprintf(f, ")");
  174. }
  175. void ir_print_type(irFileBuffer *f, irModule *m, Type *t) {
  176. i64 word_bits = 8*build_context.word_size;
  177. GB_ASSERT_NOT_NULL(t);
  178. t = default_type(t);
  179. GB_ASSERT(is_type_typed(t));
  180. switch (t->kind) {
  181. case Type_Basic:
  182. switch (t->Basic.kind) {
  183. case Basic_bool: ir_fprintf(f, "i1"); return;
  184. case Basic_i8: ir_fprintf(f, "i8"); return;
  185. case Basic_u8: ir_fprintf(f, "i8"); return;
  186. case Basic_i16: ir_fprintf(f, "i16"); return;
  187. case Basic_u16: ir_fprintf(f, "i16"); return;
  188. case Basic_i32: ir_fprintf(f, "i32"); return;
  189. case Basic_u32: ir_fprintf(f, "i32"); return;
  190. case Basic_i64: ir_fprintf(f, "i64"); return;
  191. case Basic_u64: ir_fprintf(f, "i64"); return;
  192. case Basic_i128: ir_fprintf(f, "i128"); return;
  193. case Basic_u128: ir_fprintf(f, "i128"); return;
  194. case Basic_rune: ir_fprintf(f, "i32"); return;
  195. // case Basic_f16: ir_fprintf(f, "half"); return;
  196. case Basic_f32: ir_fprintf(f, "float"); return;
  197. case Basic_f64: ir_fprintf(f, "double"); return;
  198. // case Basic_complex32: ir_fprintf(f, "%%..complex32"); return;
  199. case Basic_complex64: ir_fprintf(f, "%%..complex64"); return;
  200. case Basic_complex128: ir_fprintf(f, "%%..complex128"); return;
  201. case Basic_rawptr: ir_fprintf(f, "%%..rawptr"); return;
  202. case Basic_string: ir_fprintf(f, "%%..string"); return;
  203. case Basic_uint: ir_fprintf(f, "i%lld", word_bits); return;
  204. case Basic_int: ir_fprintf(f, "i%lld", word_bits); return;
  205. case Basic_any: ir_fprintf(f, "%%..any"); return;
  206. }
  207. break;
  208. case Type_Pointer:
  209. ir_print_type(f, m, t->Pointer.elem);
  210. ir_fprintf(f, "*");
  211. return;
  212. case Type_Atomic:
  213. ir_print_type(f, m, t->Atomic.elem);
  214. return;
  215. case Type_Array:
  216. ir_fprintf(f, "[%lld x ", t->Array.count);
  217. ir_print_type(f, m, t->Array.elem);
  218. ir_fprintf(f, "]");
  219. return;
  220. case Type_Vector: {
  221. i64 align = type_align_of(heap_allocator(), t);
  222. i64 count = t->Vector.count;
  223. ir_fprintf(f, "{[0 x <%lld x i8>], [%lld x ", align, count);
  224. ir_print_type(f, m, t->Vector.elem);
  225. ir_fprintf(f, "]}");
  226. return;
  227. }
  228. case Type_Slice:
  229. ir_fprintf(f, "{");
  230. ir_print_type(f, m, t->Slice.elem);
  231. ir_fprintf(f, "*, i%lld, i%lld}", word_bits, word_bits);
  232. return;
  233. case Type_DynamicArray:
  234. ir_fprintf(f, "{");
  235. ir_print_type(f, m, t->DynamicArray.elem);
  236. ir_fprintf(f, "*, i%lld, i%lld,", word_bits, word_bits);
  237. ir_print_type(f, m, t_allocator);
  238. ir_fprintf(f, "}");
  239. return;
  240. case Type_Record: {
  241. switch (t->Record.kind) {
  242. case TypeRecord_Struct:
  243. if (t->Record.is_packed) {
  244. ir_fprintf(f, "<");
  245. }
  246. ir_fprintf(f, "{");
  247. if (t->Record.custom_align > 0) {
  248. ir_fprintf(f, "[0 x <%lld x i8>]", t->Record.custom_align);
  249. if (t->Record.field_count > 0) {
  250. ir_fprintf(f, ", ");
  251. }
  252. }
  253. for (isize i = 0; i < t->Record.field_count; i++) {
  254. if (i > 0) {
  255. ir_fprintf(f, ", ");
  256. }
  257. ir_print_type(f, m, t->Record.fields[i]->type);
  258. }
  259. ir_fprintf(f, "}");
  260. if (t->Record.is_packed) {
  261. ir_fprintf(f, ">");
  262. }
  263. return;
  264. case TypeRecord_Union: {
  265. // NOTE(bill): The zero size array is used to fix the alignment used in a structure as
  266. // LLVM takes the first element's alignment as the entire alignment (like C)
  267. i64 align = type_align_of(heap_allocator(), t);
  268. i64 total_size = type_size_of(heap_allocator(), t);
  269. #if 1
  270. i64 block_size = t->Record.variant_block_size;
  271. ir_fprintf(f, "{[0 x <%lld x i8>], ", align);
  272. for (isize i = 0; i < t->Record.field_count; i++) {
  273. ir_print_type(f, m, t->Record.fields[i]->type);
  274. ir_fprintf(f, ", ");
  275. }
  276. ir_fprintf(f, "[%lld x i8], ", block_size);
  277. ir_fprintf(f, "i%lld}", word_bits);
  278. #else
  279. i64 block_size = total_size - build_context.word_size;
  280. ir_fprintf(f, "{[0 x <%lld x i8>], [%lld x i8], i%lld}", align, block_size, word_bits);
  281. #endif
  282. } return;
  283. case TypeRecord_RawUnion: {
  284. // NOTE(bill): The zero size array is used to fix the alignment used in a structure as
  285. // LLVM takes the first element's alignment as the entire alignment (like C)
  286. i64 size_of_union = type_size_of(heap_allocator(), t);
  287. i64 align_of_union = type_align_of(heap_allocator(), t);
  288. ir_fprintf(f, "{[0 x <%lld x i8>], [%lld x i8]}", align_of_union, size_of_union);
  289. } return;
  290. case TypeRecord_Enum:
  291. ir_print_type(f, m, base_enum_type(t));
  292. return;
  293. }
  294. } break;
  295. case Type_Named:
  296. if (is_type_struct(t) || is_type_union(t)) {
  297. String *name = map_get(&m->entity_names, hash_pointer(t->Named.type_name));
  298. GB_ASSERT_MSG(name != nullptr, "%.*s", LIT(t->Named.name));
  299. ir_print_encoded_local(f, *name);
  300. } else {
  301. ir_print_type(f, m, base_type(t));
  302. }
  303. return;
  304. case Type_Tuple:
  305. if (t->Tuple.variable_count == 1) {
  306. ir_print_type(f, m, t->Tuple.variables[0]->type);
  307. } else {
  308. ir_fprintf(f, "{");
  309. isize index = 0;
  310. for (isize i = 0; i < t->Tuple.variable_count; i++) {
  311. if (index > 0) {
  312. ir_fprintf(f, ", ");
  313. }
  314. Entity *e = t->Tuple.variables[i];
  315. if (e->kind == Entity_Variable) {
  316. ir_print_type(f, m, e->type);
  317. index++;
  318. }
  319. }
  320. ir_fprintf(f, "}");
  321. }
  322. return;
  323. case Type_Proc: {
  324. ir_print_proc_type_without_pointer(f, m, t);
  325. ir_fprintf(f, "*");
  326. } return;
  327. case Type_Map: {
  328. GB_ASSERT(t->Map.generated_struct_type != nullptr);
  329. ir_print_type(f, m, t->Map.generated_struct_type);
  330. } break;
  331. case Type_BitField: {
  332. i64 align = type_align_of(heap_allocator(), t);
  333. i64 size = type_size_of(heap_allocator(), t);
  334. ir_fprintf(f, "{[0 x <%lld x i8>], [%lld x i8]}", align, size);
  335. } break;
  336. }
  337. }
  338. void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *type);
  339. void ir_print_compound_element(irFileBuffer *f, irModule *m, ExactValue v, Type *elem_type) {
  340. ir_print_type(f, m, elem_type);
  341. ir_fprintf(f, " ");
  342. if (v.kind == ExactValue_Invalid || base_type(elem_type) == t_any) {
  343. ir_fprintf(f, "zeroinitializer");
  344. } else {
  345. ir_print_exact_value(f, m, v, elem_type);
  346. }
  347. }
  348. void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *type) {
  349. type = core_type(type);
  350. value = convert_exact_value_for_type(value, type);
  351. switch (value.kind) {
  352. case ExactValue_Bool:
  353. ir_fprintf(f, "%s", (value.value_bool ? "true" : "false"));
  354. break;
  355. case ExactValue_String: {
  356. String str = value.value_string;
  357. if (str.len == 0) {
  358. ir_fprintf(f, "zeroinitializer");
  359. break;
  360. }
  361. if (!is_type_string(type)) {
  362. GB_ASSERT(is_type_array(type));
  363. ir_fprintf(f, "c\"");
  364. ir_print_escape_string(f, str, false, false);
  365. ir_fprintf(f, "\\00\"");
  366. } else {
  367. // HACK NOTE(bill): This is a hack but it works because strings are created at the very end
  368. // of the .ll file
  369. irValue *str_array = ir_add_global_string_array(m, str);
  370. ir_fprintf(f, "{i8* getelementptr inbounds (");
  371. ir_print_type(f, m, str_array->Global.entity->type);
  372. ir_fprintf(f, ", ");
  373. ir_print_type(f, m, str_array->Global.entity->type);
  374. ir_fprintf(f, "* ");
  375. ir_print_encoded_global(f, str_array->Global.entity->token.string, false);
  376. ir_fprintf(f, ", ");
  377. ir_print_type(f, m, t_int);
  378. ir_fprintf(f, " 0, i32 0), ");
  379. ir_print_type(f, m, t_int);
  380. ir_fprintf(f, " %lld}", cast(i64)str.len);
  381. }
  382. } break;
  383. case ExactValue_Integer: {
  384. if (is_type_pointer(type)) {
  385. if (i128_eq(value.value_integer, I128_ZERO)) {
  386. ir_fprintf(f, "null");
  387. } else {
  388. ir_fprintf(f, "inttoptr (");
  389. ir_print_type(f, m, t_int);
  390. ir_fprintf(f, " ");
  391. ir_fprint_i128(f, value.value_integer);
  392. ir_fprintf(f, " to ");
  393. ir_print_type(f, m, t_rawptr);
  394. ir_fprintf(f, ")");
  395. }
  396. } else {
  397. ir_fprint_i128(f, value.value_integer);
  398. }
  399. } break;
  400. case ExactValue_Float: {
  401. GB_ASSERT_MSG(is_type_float(type), "%s", type_to_string(type));
  402. type = core_type(type);
  403. u64 u = *cast(u64*)&value.value_float;
  404. switch (type->Basic.kind) {
  405. case Basic_f32:
  406. // IMPORTANT NOTE(bill): LLVM requires all floating point constants to be
  407. // a 64 bit number if bits_of(float type) <= 64.
  408. // https://groups.google.com/forum/#!topic/llvm-dev/IlqV3TbSk6M
  409. // 64 bit mantissa: 52 bits
  410. // 32 bit mantissa: 23 bits
  411. // 16 bit mantissa: 10 bits
  412. // 29 == 52-23
  413. u >>= 29;
  414. u <<= 29;
  415. break;
  416. // case Basic_f16:
  417. // u >>= 42;
  418. // u <<= 42;
  419. // break;
  420. }
  421. switch (type->Basic.kind) {
  422. case 0: break;
  423. default:
  424. ir_fprintf(f, "0x%016llx", u);
  425. break;
  426. }
  427. } break;
  428. case ExactValue_Complex: {
  429. type = core_type(type);
  430. GB_ASSERT_MSG(is_type_complex(type), "%s", type_to_string(type));
  431. Type *ft = base_complex_elem_type(type);
  432. ir_fprintf(f, " {"); ir_print_type(f, m, ft); ir_fprintf(f, " ");
  433. ir_print_exact_value(f, m, exact_value_float(value.value_complex.real), ft);
  434. ir_fprintf(f, ", "); ir_print_type(f, m, ft); ir_fprintf(f, " ");
  435. ir_print_exact_value(f, m, exact_value_float(value.value_complex.imag), ft);
  436. ir_fprintf(f, "}");
  437. } break;
  438. case ExactValue_Pointer:
  439. if (value.value_pointer == 0) {
  440. ir_fprintf(f, "null");
  441. } else {
  442. ir_fprintf(f, "inttoptr (");
  443. ir_print_type(f, m, t_int);
  444. ir_fprintf(f, " %llu to ", cast(u64)cast(uintptr)value.value_pointer);
  445. ir_print_type(f, m, t_rawptr);
  446. ir_fprintf(f, ")");
  447. }
  448. break;
  449. case ExactValue_Compound: {
  450. type = base_type(type);
  451. if (is_type_array(type)) {
  452. ast_node(cl, CompoundLit, value.value_compound);
  453. isize elem_count = cl->elems.count;
  454. if (elem_count == 0) {
  455. ir_fprintf(f, "zeroinitializer");
  456. break;
  457. }
  458. ir_fprintf(f, "[");
  459. Type *elem_type = type->Array.elem;
  460. for (isize i = 0; i < elem_count; i++) {
  461. if (i > 0) {
  462. ir_fprintf(f, ", ");
  463. }
  464. TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
  465. GB_ASSERT(tav.mode != Addressing_Invalid);
  466. ir_print_compound_element(f, m, tav.value, elem_type);
  467. }
  468. for (isize i = elem_count; i < type->Array.count; i++) {
  469. if (i >= elem_count) {
  470. ir_fprintf(f, ", ");
  471. }
  472. ir_print_type(f, m, elem_type);
  473. ir_fprintf(f, " zeroinitializer");
  474. }
  475. ir_fprintf(f, "]");
  476. } else if (is_type_vector(type)) {
  477. ast_node(cl, CompoundLit, value.value_compound);
  478. isize elem_count = cl->elems.count;
  479. if (elem_count == 0) {
  480. ir_fprintf(f, "zeroinitializer");
  481. break;
  482. }
  483. i64 align = type_align_of(m->allocator, type);
  484. i64 count = type->Vector.count;
  485. Type *elem_type = type->Vector.elem;
  486. ir_fprintf(f, "{[0 x <%lld x i8>] zeroinitializer, [%lld x ", align, count);
  487. ir_print_type(f, m, elem_type);
  488. ir_fprintf(f, "][");
  489. if (elem_count == 1 && type->Vector.count > 1) {
  490. TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[0]);
  491. GB_ASSERT(tav.mode != Addressing_Invalid);
  492. for (isize i = 0; i < type->Vector.count; i++) {
  493. if (i > 0) {
  494. ir_fprintf(f, ", ");
  495. }
  496. ir_print_compound_element(f, m, tav.value, elem_type);
  497. }
  498. } else {
  499. for (isize i = 0; i < elem_count; i++) {
  500. if (i > 0) {
  501. ir_fprintf(f, ", ");
  502. }
  503. TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
  504. GB_ASSERT(tav.mode != Addressing_Invalid);
  505. ir_print_compound_element(f, m, tav.value, elem_type);
  506. }
  507. }
  508. ir_fprintf(f, "]}");
  509. } else if (is_type_struct(type)) {
  510. gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
  511. ast_node(cl, CompoundLit, value.value_compound);
  512. if (cl->elems.count == 0) {
  513. ir_fprintf(f, "zeroinitializer");
  514. break;
  515. }
  516. isize value_count = type->Record.field_count;
  517. ExactValue *values = gb_alloc_array(m->tmp_allocator, ExactValue, value_count);
  518. if (cl->elems[0]->kind == AstNode_FieldValue) {
  519. isize elem_count = cl->elems.count;
  520. for (isize i = 0; i < elem_count; i++) {
  521. ast_node(fv, FieldValue, cl->elems[i]);
  522. String name = fv->field->Ident.token.string;
  523. TypeAndValue tav = type_and_value_of_expr(m->info, fv->value);
  524. GB_ASSERT(tav.mode != Addressing_Invalid);
  525. Selection sel = lookup_field(m->allocator, type, name, false);
  526. Entity *f = type->Record.fields[sel.index[0]];
  527. values[f->Variable.field_index] = tav.value;
  528. }
  529. } else {
  530. for (isize i = 0; i < value_count; i++) {
  531. Entity *f = type->Record.fields_in_src_order[i];
  532. TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
  533. ExactValue val = {};
  534. if (tav.mode != Addressing_Invalid) {
  535. val = tav.value;
  536. }
  537. values[f->Variable.field_index] = val;
  538. }
  539. }
  540. if (type->Record.is_packed) {
  541. ir_fprintf(f, "<");
  542. }
  543. ir_fprintf(f, "{");
  544. if (type->Record.custom_align > 0) {
  545. ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
  546. if (value_count > 0) {
  547. ir_fprintf(f, ", ");
  548. }
  549. }
  550. for (isize i = 0; i < value_count; i++) {
  551. if (i > 0) {
  552. ir_fprintf(f, ", ");
  553. }
  554. Type *elem_type = type->Record.fields[i]->type;
  555. ir_print_compound_element(f, m, values[i], elem_type);
  556. }
  557. ir_fprintf(f, "}");
  558. if (type->Record.is_packed) {
  559. ir_fprintf(f, ">");
  560. }
  561. gb_temp_arena_memory_end(tmp);
  562. } else {
  563. ir_fprintf(f, "zeroinitializer");
  564. }
  565. } break;
  566. default:
  567. ir_fprintf(f, "zeroinitializer");
  568. // GB_PANIC("Invalid ExactValue: %d", value.kind);
  569. break;
  570. }
  571. }
  572. void ir_print_block_name(irFileBuffer *f, irBlock *b) {
  573. if (b != nullptr) {
  574. ir_print_escape_string(f, b->label, false, false);
  575. ir_fprintf(f, "-%td", b->index);
  576. } else {
  577. ir_fprintf(f, "<INVALID-BLOCK>");
  578. }
  579. }
  580. bool ir_print_is_proc_global(irModule *m, irProcedure *proc) {
  581. if (proc->entity != nullptr &&
  582. proc->entity->kind == Entity_Procedure) {
  583. if (m->entry_point_entity == proc->entity) {
  584. // gb_printf("%.*s\n", LIT(proc->entity->token.string));
  585. // TODO(bill): This may not be needed during windows
  586. return true;
  587. }
  588. if (proc->entity->Procedure.link_name.len > 0) {
  589. return true;
  590. }
  591. }
  592. return (proc->tags & (ProcTag_foreign|ProcTag_export)) != 0;
  593. }
  594. void ir_print_value(irFileBuffer *f, irModule *m, irValue *value, Type *type_hint) {
  595. if (value == nullptr) {
  596. ir_fprintf(f, "!!!nullptr_VALUE");
  597. return;
  598. }
  599. switch (value->kind) {
  600. default: GB_PANIC("Unknown irValue kind"); break;
  601. case irValue_Constant:
  602. ir_print_exact_value(f, m, value->Constant.value, type_hint);
  603. break;
  604. case irValue_ConstantSlice: {
  605. irValueConstantSlice *cs = &value->ConstantSlice;
  606. if (cs->backing_array == nullptr || cs->count == 0) {
  607. ir_fprintf(f, "zeroinitializer");
  608. } else {
  609. Type *at = base_type(type_deref(ir_type(cs->backing_array)));
  610. Type *et = at->Array.elem;
  611. ir_fprintf(f, "{");
  612. ir_print_type(f, m, et);
  613. ir_fprintf(f, "* getelementptr inbounds (");
  614. ir_print_type(f, m, at);
  615. ir_fprintf(f, ", ");
  616. ir_print_type(f, m, at);
  617. ir_fprintf(f, "* ");
  618. ir_print_value(f, m, cs->backing_array, at);
  619. ir_fprintf(f, ", ");
  620. ir_print_type(f, m, t_int);
  621. ir_fprintf(f, " 0, i32 0), ");
  622. ir_print_type(f, m, t_int);
  623. ir_fprintf(f, " %lld, ", cs->count);
  624. ir_print_type(f, m, t_int);
  625. ir_fprintf(f, " %lld}", cs->count);
  626. }
  627. } break;
  628. case irValue_Nil:
  629. ir_fprintf(f, "zeroinitializer");
  630. break;
  631. case irValue_Undef:
  632. ir_fprintf(f, "undef");
  633. break;
  634. case irValue_TypeName:
  635. ir_print_encoded_local(f, value->TypeName.name);
  636. break;
  637. case irValue_Global: {
  638. Entity *e = value->Global.entity;
  639. Scope *scope = e->scope;
  640. bool in_global_scope = false;
  641. if (scope != nullptr) {
  642. // TODO(bill): Fix this rule. What should it be?
  643. in_global_scope = scope->is_global || scope->is_init;
  644. }
  645. ir_print_encoded_global(f, ir_get_global_name(m, value), in_global_scope);
  646. } break;
  647. case irValue_Param:
  648. ir_print_encoded_local(f, value->Param.entity->token.string);
  649. break;
  650. case irValue_Proc:
  651. ir_print_encoded_global(f, value->Proc.name, ir_print_is_proc_global(m, &value->Proc));
  652. break;
  653. case irValue_Instr:
  654. ir_fprintf(f, "%%%d", value->index);
  655. break;
  656. }
  657. }
  658. void ir_print_calling_convention(irFileBuffer *f, irModule *m, ProcCallingConvention cc) {
  659. switch (cc) {
  660. case ProcCC_Odin: ir_fprintf(f, ""); break;
  661. case ProcCC_Contextless: ir_fprintf(f, ""); break;
  662. case ProcCC_C: ir_fprintf(f, "ccc "); break;
  663. case ProcCC_Std: ir_fprintf(f, "cc 64 "); break;
  664. case ProcCC_Fast: ir_fprintf(f, "cc 65 "); break;
  665. default: GB_PANIC("unknown calling convention: %d", cc);
  666. }
  667. }
  668. void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
  669. GB_ASSERT(value->kind == irValue_Instr);
  670. irInstr *instr = &value->Instr;
  671. ir_fprintf(f, "\t");
  672. switch (instr->kind) {
  673. default: {
  674. GB_PANIC("<unknown instr> %d\n", instr->kind);
  675. ir_fprintf(f, "; <unknown instr> %d\n", instr->kind);
  676. } break;
  677. case irInstr_StartupRuntime: {
  678. ir_fprintf(f, "call void ");
  679. ir_print_encoded_global(f, str_lit(IR_STARTUP_RUNTIME_PROC_NAME), false);
  680. ir_fprintf(f, "()\n");
  681. } break;
  682. case irInstr_Comment:
  683. ir_fprintf(f, "; %.*s\n", LIT(instr->Comment.text));
  684. break;
  685. case irInstr_Local: {
  686. Type *type = instr->Local.entity->type;
  687. i64 align = instr->Local.alignment;
  688. if (align <= 0) {
  689. align = type_align_of(m->allocator, type);
  690. }
  691. ir_fprintf(f, "%%%d = alloca ", value->index);
  692. ir_print_type(f, m, type);
  693. ir_fprintf(f, ", align %lld\n", align);
  694. } break;
  695. case irInstr_ZeroInit: {
  696. Type *type = type_deref(ir_type(instr->ZeroInit.address));
  697. ir_fprintf(f, "store ");
  698. ir_print_type(f, m, type);
  699. ir_fprintf(f, " zeroinitializer, ");
  700. ir_print_type(f, m, type);
  701. ir_fprintf(f, "* %%%d\n", instr->ZeroInit.address->index);
  702. } break;
  703. case irInstr_Store: {
  704. Type *type = type_deref(ir_type(instr->Store.address));
  705. ir_fprintf(f, "store ");
  706. if (instr->Store.atomic) {
  707. ir_fprintf(f, "atomic ");
  708. }
  709. ir_print_type(f, m, type);
  710. ir_fprintf(f, " ");
  711. ir_print_value(f, m, instr->Store.value, type);
  712. ir_fprintf(f, ", ");
  713. ir_print_type(f, m, type);
  714. ir_fprintf(f, "* ");
  715. ir_print_value(f, m, instr->Store.address, type);
  716. if (is_type_atomic(type)) {
  717. // TODO(bill): Do ordering
  718. ir_fprintf(f, " unordered");
  719. ir_fprintf(f, ", align %lld\n", type_align_of(m->allocator, type));
  720. }
  721. ir_fprintf(f, "\n");
  722. } break;
  723. case irInstr_Load: {
  724. Type *type = instr->Load.type;
  725. ir_fprintf(f, "%%%d = load ", value->index);
  726. if (is_type_atomic(type)) {
  727. ir_fprintf(f, "atomic ");
  728. }
  729. ir_print_type(f, m, type);
  730. ir_fprintf(f, ", ");
  731. ir_print_type(f, m, type);
  732. ir_fprintf(f, "* ");
  733. ir_print_value(f, m, instr->Load.address, type);
  734. if (is_type_atomic(type)) {
  735. // TODO(bill): Do ordering
  736. ir_fprintf(f, " unordered");
  737. }
  738. ir_fprintf(f, ", align %lld\n", type_align_of(m->allocator, type));
  739. } break;
  740. case irInstr_ArrayElementPtr: {
  741. Type *et = ir_type(instr->ArrayElementPtr.address);
  742. ir_fprintf(f, "%%%d = getelementptr inbounds ", value->index);
  743. ir_print_type(f, m, type_deref(et));
  744. ir_fprintf(f, ", ");
  745. ir_print_type(f, m, et);
  746. ir_fprintf(f, " ");
  747. ir_print_value(f, m, instr->ArrayElementPtr.address, et);
  748. ir_fprintf(f, ", ");
  749. ir_print_type(f, m, t_int);
  750. ir_fprintf(f, " 0, ");
  751. if (is_type_vector(type_deref(et))) {
  752. ir_print_type(f, m, t_i32);
  753. ir_fprintf(f, " 1, ");
  754. }
  755. irValue *index =instr->ArrayElementPtr.elem_index;
  756. Type *t = ir_type(index);
  757. ir_print_type(f, m, t);
  758. ir_fprintf(f, " ");
  759. ir_print_value(f, m, index, t);
  760. ir_fprintf(f, "\n");
  761. } break;
  762. case irInstr_StructElementPtr: {
  763. Type *et = ir_type(instr->StructElementPtr.address);
  764. ir_fprintf(f, "%%%d = getelementptr inbounds ", value->index);
  765. i32 index = instr->StructElementPtr.elem_index;
  766. Type *st = base_type(type_deref(et));
  767. if (is_type_struct(st)) {
  768. if (st->Record.custom_align > 0) {
  769. index += 1;
  770. }
  771. } else if (is_type_union(st)) {
  772. index += 1;
  773. }
  774. ir_print_type(f, m, type_deref(et));
  775. ir_fprintf(f, ", ");
  776. ir_print_type(f, m, et);
  777. ir_fprintf(f, " ");
  778. ir_print_value(f, m, instr->StructElementPtr.address, et);
  779. ir_fprintf(f, ", ");
  780. ir_print_type(f, m, t_int);
  781. ir_fprintf(f, " 0, ");
  782. ir_print_type(f, m, t_i32);
  783. ir_fprintf(f, " %d", index);
  784. ir_fprintf(f, "\n");
  785. } break;
  786. case irInstr_PtrOffset: {
  787. Type *pt = ir_type(instr->PtrOffset.address);
  788. ir_fprintf(f, "%%%d = getelementptr inbounds ", value->index);
  789. ir_print_type(f, m, type_deref(pt));
  790. ir_fprintf(f, ", ");
  791. ir_print_type(f, m, pt);
  792. ir_fprintf(f, " ");
  793. ir_print_value(f, m, instr->PtrOffset.address, pt);
  794. irValue *offset = instr->PtrOffset.offset;
  795. Type *t = ir_type(offset);
  796. ir_fprintf(f, ", ");
  797. ir_print_type(f, m, t);
  798. ir_fprintf(f, " ");
  799. ir_print_value(f, m, offset, t);
  800. ir_fprintf(f, "\n");
  801. } break;
  802. case irInstr_Phi: {
  803. ir_fprintf(f, "%%%d = phi ", value->index);
  804. ir_print_type(f, m, instr->Phi.type);
  805. ir_fprintf(f, " ", value->index);
  806. for (isize i = 0; i < instr->Phi.edges.count; i++) {
  807. if (i > 0) {
  808. ir_fprintf(f, ", ");
  809. }
  810. irValue *edge = instr->Phi.edges[i];
  811. irBlock *block = nullptr;
  812. if (instr->parent != nullptr &&
  813. i < instr->parent->preds.count) {
  814. block = instr->parent->preds[i];
  815. }
  816. ir_fprintf(f, "[ ");
  817. ir_print_value(f, m, edge, instr->Phi.type);
  818. ir_fprintf(f, ", %%");
  819. ir_print_block_name(f, block);
  820. ir_fprintf(f, " ]");
  821. }
  822. ir_fprintf(f, "\n");
  823. } break;
  824. case irInstr_StructExtractValue: {
  825. Type *et = ir_type(instr->StructExtractValue.address);
  826. ir_fprintf(f, "%%%d = extractvalue ", value->index);
  827. i32 index = instr->StructExtractValue.index;
  828. Type *st = base_type(et);
  829. if (is_type_struct(st)) {
  830. if (st->Record.custom_align > 0) {
  831. index += 1;
  832. }
  833. } else if (is_type_union(st)) {
  834. index += 1;
  835. }
  836. ir_print_type(f, m, et);
  837. ir_fprintf(f, " ");
  838. ir_print_value(f, m, instr->StructExtractValue.address, et);
  839. ir_fprintf(f, ", %d\n", index);
  840. } break;
  841. case irInstr_UnionTagPtr: {
  842. Type *et = ir_type(instr->UnionTagPtr.address);
  843. ir_fprintf(f, "%%%d = getelementptr inbounds ", value->index);
  844. Type *t = base_type(type_deref(et));
  845. GB_ASSERT(is_type_union(t));
  846. ir_print_type(f, m, type_deref(et));
  847. ir_fprintf(f, ", ");
  848. ir_print_type(f, m, et);
  849. ir_fprintf(f, " ");
  850. ir_print_value(f, m, instr->UnionTagPtr.address, et);
  851. ir_fprintf(f, ", ");
  852. ir_print_type(f, m, t_int);
  853. ir_fprintf(f, " 0, ");
  854. ir_print_type(f, m, t_i32);
  855. #if 1
  856. ir_fprintf(f, " %d", 2 + t->Record.field_count);
  857. #else
  858. ir_fprintf(f, " %d", 2);
  859. #endif
  860. ir_fprintf(f, " ; UnionTagPtr");
  861. ir_fprintf(f, "\n");
  862. } break;
  863. case irInstr_UnionTagValue: {
  864. Type *et = ir_type(instr->UnionTagValue.address);
  865. ir_fprintf(f, "%%%d = extractvalue ", value->index);
  866. Type *t = base_type(et);
  867. GB_ASSERT(is_type_union(t));
  868. ir_print_type(f, m, et);
  869. ir_fprintf(f, " ");
  870. ir_print_value(f, m, instr->UnionTagValue.address, et);
  871. ir_fprintf(f, ",");
  872. #if 1
  873. ir_fprintf(f, " %d", 2 + t->Record.field_count);
  874. #else
  875. ir_fprintf(f, " %d", 2);
  876. #endif
  877. ir_fprintf(f, ", %d", 2 + t->Record.field_count);
  878. ir_fprintf(f, " ; UnionTagValue");
  879. ir_fprintf(f, "\n");
  880. } break;
  881. case irInstr_Jump: {;
  882. ir_fprintf(f, "br label %%");
  883. ir_print_block_name(f, instr->Jump.block);
  884. ir_fprintf(f, "\n");
  885. } break;
  886. case irInstr_If: {;
  887. ir_fprintf(f, "br ");
  888. ir_print_type(f, m, t_bool);
  889. ir_fprintf(f, " ");
  890. ir_print_value(f, m, instr->If.cond, t_bool);
  891. ir_fprintf(f, ", ", instr->If.cond->index);
  892. ir_fprintf(f, "label %%"); ir_print_block_name(f, instr->If.true_block);
  893. ir_fprintf(f, ", label %%"); ir_print_block_name(f, instr->If.false_block);
  894. ir_fprintf(f, "\n");
  895. } break;
  896. case irInstr_Return: {
  897. irInstrReturn *ret = &instr->Return;
  898. ir_fprintf(f, "ret ");
  899. if (ret->value == nullptr) {
  900. ir_fprintf(f, "void");
  901. } else {
  902. Type *t = ir_type(ret->value);
  903. ir_print_type(f, m, t);
  904. ir_fprintf(f, " ");
  905. ir_print_value(f, m, ret->value, t);
  906. }
  907. ir_fprintf(f, "\n");
  908. } break;
  909. case irInstr_Conv: {
  910. irInstrConv *c = &instr->Conv;
  911. ir_fprintf(f, "%%%d = %.*s ", value->index, LIT(ir_conv_strings[c->kind]));
  912. ir_print_type(f, m, c->from);
  913. ir_fprintf(f, " ");
  914. ir_print_value(f, m, c->value, c->from);
  915. ir_fprintf(f, " to ");
  916. ir_print_type(f, m, c->to);
  917. ir_fprintf(f, "\n");
  918. } break;
  919. case irInstr_Unreachable: {
  920. ir_fprintf(f, "unreachable\n");
  921. } break;
  922. case irInstr_UnaryOp: {
  923. irInstrUnaryOp *uo = &value->Instr.UnaryOp;
  924. Type *type = base_type(ir_type(uo->expr));
  925. Type *elem_type = type;
  926. while (elem_type->kind == Type_Vector) {
  927. elem_type = base_type(elem_type->Vector.elem);
  928. }
  929. ir_fprintf(f, "%%%d = ", value->index);
  930. switch (uo->op) {
  931. case Token_Sub:
  932. if (is_type_float(elem_type)) {
  933. ir_fprintf(f, "fsub");
  934. } else {
  935. ir_fprintf(f, "sub");
  936. }
  937. break;
  938. case Token_Xor:
  939. case Token_Not:
  940. GB_ASSERT(is_type_integer(type) || is_type_boolean(type));
  941. ir_fprintf(f, "xor");
  942. break;
  943. default:
  944. GB_PANIC("Unknown unary operator");
  945. break;
  946. }
  947. ir_fprintf(f, " ");
  948. ir_print_type(f, m, type);
  949. ir_fprintf(f, " ");
  950. switch (uo->op) {
  951. case Token_Sub:
  952. if (is_type_float(elem_type)) {
  953. ir_print_exact_value(f, m, exact_value_float(0), elem_type);
  954. } else {
  955. ir_fprintf(f, "0");
  956. }
  957. break;
  958. case Token_Xor:
  959. case Token_Not:
  960. GB_ASSERT(is_type_integer(type) || is_type_boolean(type));
  961. ir_fprintf(f, "-1");
  962. break;
  963. }
  964. ir_fprintf(f, ", ");
  965. ir_print_value(f, m, uo->expr, type);
  966. ir_fprintf(f, "\n");
  967. } break;
  968. case irInstr_BinaryOp: {
  969. irInstrBinaryOp *bo = &value->Instr.BinaryOp;
  970. Type *type = base_type(ir_type(bo->left));
  971. Type *elem_type = type;
  972. GB_ASSERT_MSG(!is_type_vector(elem_type), type_to_string(elem_type));
  973. ir_fprintf(f, "%%%d = ", value->index);
  974. if (gb_is_between(bo->op, Token__ComparisonBegin+1, Token__ComparisonEnd-1)) {
  975. if (is_type_string(elem_type)) {
  976. ir_fprintf(f, "call ");
  977. ir_print_calling_convention(f, m, ProcCC_Odin);
  978. ir_print_type(f, m, t_bool);
  979. char *runtime_proc = "";
  980. switch (bo->op) {
  981. case Token_CmpEq: runtime_proc = "__string_eq"; break;
  982. case Token_NotEq: runtime_proc = "__string_ne"; break;
  983. case Token_Lt: runtime_proc = "__string_lt"; break;
  984. case Token_Gt: runtime_proc = "__string_gt"; break;
  985. case Token_LtEq: runtime_proc = "__string_le"; break;
  986. case Token_GtEq: runtime_proc = "__string_gt"; break;
  987. }
  988. ir_fprintf(f, " ");
  989. ir_print_encoded_global(f, make_string_c(runtime_proc), false);
  990. ir_fprintf(f, "(");
  991. ir_print_type(f, m, type);
  992. ir_fprintf(f, " ");
  993. ir_print_value(f, m, bo->left, type);
  994. ir_fprintf(f, ", ");
  995. ir_print_type(f, m, type);
  996. ir_fprintf(f, " ");
  997. ir_print_value(f, m, bo->right, type);
  998. ir_fprintf(f, ")\n");
  999. return;
  1000. } else if (is_type_float(elem_type)) {
  1001. ir_fprintf(f, "fcmp ");
  1002. switch (bo->op) {
  1003. case Token_CmpEq: ir_fprintf(f, "oeq"); break;
  1004. case Token_NotEq: ir_fprintf(f, "one"); break;
  1005. case Token_Lt: ir_fprintf(f, "olt"); break;
  1006. case Token_Gt: ir_fprintf(f, "ogt"); break;
  1007. case Token_LtEq: ir_fprintf(f, "ole"); break;
  1008. case Token_GtEq: ir_fprintf(f, "oge"); break;
  1009. }
  1010. } else if (is_type_complex(elem_type)) {
  1011. ir_fprintf(f, "call ");
  1012. ir_print_calling_convention(f, m, ProcCC_Odin);
  1013. ir_print_type(f, m, t_bool);
  1014. char *runtime_proc = "";
  1015. i64 sz = 8*type_size_of(m->allocator, elem_type);
  1016. switch (sz) {
  1017. case 64:
  1018. switch (bo->op) {
  1019. case Token_CmpEq: runtime_proc = "__complex64_eq"; break;
  1020. case Token_NotEq: runtime_proc = "__complex64_ne"; break;
  1021. }
  1022. break;
  1023. case 128:
  1024. switch (bo->op) {
  1025. case Token_CmpEq: runtime_proc = "__complex128_eq"; break;
  1026. case Token_NotEq: runtime_proc = "__complex128_ne"; break;
  1027. }
  1028. break;
  1029. }
  1030. ir_fprintf(f, " ");
  1031. ir_print_encoded_global(f, make_string_c(runtime_proc), false);
  1032. ir_fprintf(f, "(");
  1033. ir_print_type(f, m, type);
  1034. ir_fprintf(f, " ");
  1035. ir_print_value(f, m, bo->left, type);
  1036. ir_fprintf(f, ", ");
  1037. ir_print_type(f, m, type);
  1038. ir_fprintf(f, " ");
  1039. ir_print_value(f, m, bo->right, type);
  1040. ir_fprintf(f, ")\n");
  1041. return;
  1042. } else {
  1043. ir_fprintf(f, "icmp ");
  1044. if (bo->op != Token_CmpEq &&
  1045. bo->op != Token_NotEq) {
  1046. if (is_type_unsigned(elem_type)) {
  1047. ir_fprintf(f, "u");
  1048. } else {
  1049. ir_fprintf(f, "s");
  1050. }
  1051. }
  1052. switch (bo->op) {
  1053. case Token_CmpEq: ir_fprintf(f, "eq"); break;
  1054. case Token_NotEq: ir_fprintf(f, "ne"); break;
  1055. case Token_Lt: ir_fprintf(f, "lt"); break;
  1056. case Token_Gt: ir_fprintf(f, "gt"); break;
  1057. case Token_LtEq: ir_fprintf(f, "le"); break;
  1058. case Token_GtEq: ir_fprintf(f, "ge"); break;
  1059. default: GB_PANIC("invalid comparison");break;
  1060. }
  1061. }
  1062. } else {
  1063. if (is_type_float(elem_type)) {
  1064. ir_fprintf(f, "f");
  1065. }
  1066. switch (bo->op) {
  1067. case Token_Add: ir_fprintf(f, "add"); break;
  1068. case Token_Sub: ir_fprintf(f, "sub"); break;
  1069. case Token_And: ir_fprintf(f, "and"); break;
  1070. case Token_Or: ir_fprintf(f, "or"); break;
  1071. case Token_Xor: ir_fprintf(f, "xor"); break;
  1072. case Token_Shl: ir_fprintf(f, "shl"); break;
  1073. case Token_Shr: ir_fprintf(f, "lshr"); break;
  1074. case Token_Mul: ir_fprintf(f, "mul"); break;
  1075. case Token_Not: ir_fprintf(f, "xor"); break;
  1076. case Token_AndNot: GB_PANIC("Token_AndNot Should never be called");
  1077. default: {
  1078. if (!is_type_float(elem_type)) {
  1079. if (is_type_unsigned(elem_type)) {
  1080. ir_fprintf(f, "u");
  1081. } else {
  1082. ir_fprintf(f, "s");
  1083. }
  1084. }
  1085. switch (bo->op) {
  1086. case Token_Quo: ir_fprintf(f, "div"); break;
  1087. case Token_Mod: ir_fprintf(f, "rem"); break;
  1088. }
  1089. } break;
  1090. }
  1091. }
  1092. ir_fprintf(f, " ");
  1093. ir_print_type(f, m, type);
  1094. ir_fprintf(f, " ");
  1095. ir_print_value(f, m, bo->left, type);
  1096. ir_fprintf(f, ", ");
  1097. ir_print_value(f, m, bo->right, type);
  1098. ir_fprintf(f, "\n");
  1099. } break;
  1100. case irInstr_Call: {
  1101. irInstrCall *call = &instr->Call;
  1102. Type *proc_type = base_type(ir_type(call->value));
  1103. GB_ASSERT(is_type_proc(proc_type));
  1104. bool is_c_vararg = proc_type->Proc.c_vararg;
  1105. Type *result_type = call->type;
  1106. if (result_type) {
  1107. ir_fprintf(f, "%%%d = ", value->index);
  1108. }
  1109. ir_fprintf(f, "call ");
  1110. ir_print_calling_convention(f, m, proc_type->Proc.calling_convention);
  1111. if (is_c_vararg) {
  1112. ir_print_proc_type_without_pointer(f, m, proc_type);
  1113. } else if (result_type && !proc_type->Proc.return_by_pointer) {
  1114. ir_print_proc_results(f, m, proc_type);
  1115. } else {
  1116. ir_fprintf(f, "void");
  1117. }
  1118. ir_fprintf(f, " ");
  1119. ir_print_value(f, m, call->value, call->type);
  1120. ir_fprintf(f, "(");
  1121. if (proc_type->Proc.return_by_pointer) {
  1122. GB_ASSERT(call->return_ptr != nullptr);
  1123. ir_print_type(f, m, proc_type->Proc.results);
  1124. ir_fprintf(f, "* ");
  1125. ir_print_value(f, m, call->return_ptr, ir_type(call->return_ptr));
  1126. if (call->arg_count > 0) {
  1127. ir_fprintf(f, ", ");
  1128. }
  1129. }
  1130. isize param_index = 0;
  1131. if (call->arg_count > 0) {
  1132. TypeTuple *params = &proc_type->Proc.params->Tuple;
  1133. if (proc_type->Proc.c_vararg) {
  1134. isize i = 0;
  1135. for (; i < params->variable_count-1; i++) {
  1136. Entity *e = params->variables[i];
  1137. GB_ASSERT(e != nullptr);
  1138. if (e->kind != Entity_Variable) continue;
  1139. if (param_index > 0) ir_fprintf(f, ", ");
  1140. Type *t = proc_type->Proc.abi_compat_params[i];
  1141. ir_print_type(f, m, t);
  1142. if (e->flags&EntityFlag_NoAlias) {
  1143. ir_fprintf(f, " noalias");
  1144. }
  1145. ir_fprintf(f, " ");
  1146. irValue *arg = call->args[i];
  1147. ir_print_value(f, m, arg, t);
  1148. param_index++;
  1149. }
  1150. for (; i < call->arg_count; i++) {
  1151. if (param_index > 0) ir_fprintf(f, ", ");
  1152. irValue *arg = call->args[i];
  1153. Type *t = ir_type(arg);
  1154. ir_print_type(f, m, t);
  1155. ir_fprintf(f, " ");
  1156. ir_print_value(f, m, arg, t);
  1157. param_index++;
  1158. }
  1159. } else {
  1160. GB_ASSERT(call->arg_count == params->variable_count);
  1161. isize param_count = params->variable_count;
  1162. for (isize i = 0; i < param_count; i++) {
  1163. Entity *e = params->variables[i];
  1164. GB_ASSERT(e != nullptr);
  1165. if (e->kind != Entity_Variable) continue;
  1166. if (param_index > 0) ir_fprintf(f, ", ");
  1167. irValue *arg = call->args[i];
  1168. Type *t = proc_type->Proc.abi_compat_params[i];
  1169. ir_print_type(f, m, t);
  1170. if (e->flags&EntityFlag_NoAlias) {
  1171. ir_fprintf(f, " noalias");
  1172. }
  1173. ir_fprintf(f, " ");
  1174. ir_print_value(f, m, arg, t);
  1175. param_index++;
  1176. }
  1177. }
  1178. }
  1179. if (proc_type->Proc.calling_convention == ProcCC_Odin) {
  1180. if (param_index > 0) ir_fprintf(f, ", ");
  1181. ir_print_type(f, m, t_context_ptr);
  1182. ir_fprintf(f, " noalias nonnull");
  1183. ir_print_value(f, m, call->context_ptr, t_context_ptr);
  1184. }
  1185. ir_fprintf(f, ")\n");
  1186. } break;
  1187. case irInstr_Select: {
  1188. ir_fprintf(f, "%%%d = select i1 ", value->index);
  1189. ir_print_value(f, m, instr->Select.cond, t_bool);
  1190. ir_fprintf(f, ", ");
  1191. ir_print_type(f, m, ir_type(instr->Select.true_value));
  1192. ir_fprintf(f, " ");
  1193. ir_print_value(f, m, instr->Select.true_value, ir_type(instr->Select.true_value));
  1194. ir_fprintf(f, ", ");
  1195. ir_print_type(f, m, ir_type(instr->Select.false_value));
  1196. ir_fprintf(f, " ");
  1197. ir_print_value(f, m, instr->Select.false_value, ir_type(instr->Select.false_value));
  1198. ir_fprintf(f, "\n");
  1199. } break;
  1200. // case irInstr_VectorExtractElement: {
  1201. // Type *vt = ir_type(instr->VectorExtractElement.vector);
  1202. // Type *it = ir_type(instr->VectorExtractElement.index);
  1203. // ir_fprintf(f, "%%%d = extractelement ", value->index);
  1204. // ir_print_type(f, m, vt);
  1205. // ir_fprintf(f, " ");
  1206. // ir_print_value(f, m, instr->VectorExtractElement.vector, vt);
  1207. // ir_fprintf(f, ", ");
  1208. // ir_print_type(f, m, it);
  1209. // ir_fprintf(f, " ");
  1210. // ir_print_value(f, m, instr->VectorExtractElement.index, it);
  1211. // ir_fprintf(f, "\n");
  1212. // } break;
  1213. // case irInstr_VectorInsertElement: {
  1214. // irInstrVectorInsertElement *ie = &instr->VectorInsertElement;
  1215. // Type *vt = ir_type(ie->vector);
  1216. // ir_fprintf(f, "%%%d = insertelement ", value->index);
  1217. // ir_print_type(f, m, vt);
  1218. // ir_fprintf(f, " ");
  1219. // ir_print_value(f, m, ie->vector, vt);
  1220. // ir_fprintf(f, ", ");
  1221. // ir_print_type(f, m, ir_type(ie->elem));
  1222. // ir_fprintf(f, " ");
  1223. // ir_print_value(f, m, ie->elem, ir_type(ie->elem));
  1224. // ir_fprintf(f, ", ");
  1225. // ir_print_type(f, m, ir_type(ie->index));
  1226. // ir_fprintf(f, " ");
  1227. // ir_print_value(f, m, ie->index, ir_type(ie->index));
  1228. // ir_fprintf(f, "\n");
  1229. // } break;
  1230. // case irInstr_VectorShuffle: {
  1231. // irInstrVectorShuffle *sv = &instr->VectorShuffle;
  1232. // Type *vt = ir_type(sv->vector);
  1233. // ir_fprintf(f, "%%%d = shufflevector ", value->index);
  1234. // ir_print_type(f, m, vt);
  1235. // ir_fprintf(f, " ");
  1236. // ir_print_value(f, m, sv->vector, vt);
  1237. // ir_fprintf(f, ", ");
  1238. // ir_print_type(f, m, vt);
  1239. // ir_fprintf(f, " ");
  1240. // ir_print_value(f, m, sv->vector, vt);
  1241. // ir_fprintf(f, ", ");
  1242. // ir_fprintf(f, "<%td x i32> <", sv->index_count);
  1243. // for (isize i = 0; i < sv->index_count; i++) {
  1244. // if (i > 0) {
  1245. // ir_fprintf(f, ", ");
  1246. // }
  1247. // ir_fprintf(f, "i32 %d", sv->indices[i]);
  1248. // }
  1249. // ir_fprintf(f, ">");
  1250. // ir_fprintf(f, "\n");
  1251. // } break;
  1252. #if 0
  1253. case irInstr_BoundsCheck: {
  1254. irInstrBoundsCheck *bc = &instr->BoundsCheck;
  1255. ir_fprintf(f, "call void ");
  1256. ir_print_encoded_global(f, str_lit("__bounds_check_error"), false);
  1257. ir_fprintf(f, "(");
  1258. ir_print_compound_element(f, m, exact_value_string(bc->pos.file), t_string);
  1259. ir_fprintf(f, ", ");
  1260. ir_print_type(f, m, t_int);
  1261. ir_fprintf(f, " ");
  1262. ir_print_exact_value(f, m, exact_value_i64(bc->pos.line), t_int);
  1263. ir_fprintf(f, ", ");
  1264. ir_print_type(f, m, t_int);
  1265. ir_fprintf(f, " ");
  1266. ir_print_exact_value(f, m, exact_value_i64(bc->pos.column), t_int);
  1267. ir_fprintf(f, ", ");
  1268. ir_print_type(f, m, t_int);
  1269. ir_fprintf(f, " ");
  1270. ir_print_value(f, m, bc->index, t_int);
  1271. ir_fprintf(f, ", ");
  1272. ir_print_type(f, m, t_int);
  1273. ir_fprintf(f, " ");
  1274. ir_print_value(f, m, bc->len, t_int);
  1275. ir_fprintf(f, ")\n");
  1276. } break;
  1277. case irInstr_SliceBoundsCheck: {
  1278. irInstrSliceBoundsCheck *bc = &instr->SliceBoundsCheck;
  1279. ir_fprintf(f, "call void ");
  1280. if (bc->is_substring) {
  1281. ir_print_encoded_global(f, str_lit("__substring_expr_error"), false);
  1282. } else {
  1283. ir_print_encoded_global(f, str_lit("__slice_expr_error"), false);
  1284. }
  1285. ir_fprintf(f, "(");
  1286. ir_print_compound_element(f, m, exact_value_string(bc->pos.file), t_string);
  1287. ir_fprintf(f, ", ");
  1288. ir_print_type(f, m, t_int);
  1289. ir_fprintf(f, " ");
  1290. ir_print_exact_value(f, m, exact_value_i64(bc->pos.line), t_int);
  1291. ir_fprintf(f, ", ");
  1292. ir_print_type(f, m, t_int);
  1293. ir_fprintf(f, " ");
  1294. ir_print_exact_value(f, m, exact_value_i64(bc->pos.column), t_int);
  1295. ir_fprintf(f, ", ");
  1296. ir_print_type(f, m, t_int);
  1297. ir_fprintf(f, " ");
  1298. ir_print_value(f, m, bc->low, t_int);
  1299. ir_fprintf(f, ", ");
  1300. ir_print_type(f, m, t_int);
  1301. ir_fprintf(f, " ");
  1302. ir_print_value(f, m, bc->high, t_int);
  1303. if (!bc->is_substring) {
  1304. ir_fprintf(f, ", ");
  1305. ir_print_type(f, m, t_int);
  1306. ir_fprintf(f, " ");
  1307. ir_print_value(f, m, bc->max, t_int);
  1308. }
  1309. ir_fprintf(f, ")\n");
  1310. } break;
  1311. #endif
  1312. case irInstr_DebugDeclare: {
  1313. /* irInstrDebugDeclare *dd = &instr->DebugDeclare;
  1314. Type *vt = ir_type(dd->value);
  1315. irDebugInfo *di = dd->debug_info;
  1316. Entity *e = dd->entity;
  1317. String name = e->token.string;
  1318. TokenPos pos = e->token.pos;
  1319. // gb_printf("debug_declare %.*s\n", LIT(dd->entity->token.string));
  1320. ir_fprintf(f, "call void @llvm.dbg.declare(");
  1321. ir_fprintf(f, "metadata ");
  1322. ir_print_type(f, m, vt);
  1323. ir_fprintf(f, " ");
  1324. ir_print_value(f, m, dd->value, vt);
  1325. ir_fprintf(f, ", metadata !DILocalVariable(name: \"");
  1326. ir_print_escape_string(f, name, false);
  1327. ir_fprintf(f, "\", scope: !%d, line: %td)", di->id, pos.line);
  1328. ir_fprintf(f, ", metadata !DIExpression()");
  1329. ir_fprintf(f, ")");
  1330. ir_fprintf(f, ", !dbg !DILocation(line: %td, column: %td, scope: !%d)", pos.line, pos.column, di->id);
  1331. ir_fprintf(f, "\n"); */
  1332. } break;
  1333. }
  1334. }
  1335. void ir_print_proc(irFileBuffer *f, irModule *m, irProcedure *proc) {
  1336. if (proc->body == nullptr) {
  1337. ir_fprintf(f, "declare ");
  1338. // if (proc->tags & ProcTag_dll_import) {
  1339. // ir_fprintf(f, "dllimport ");
  1340. // }
  1341. } else {
  1342. ir_fprintf(f, "\n");
  1343. ir_fprintf(f, "define ");
  1344. if (build_context.is_dll) {
  1345. // if (proc->tags & (ProcTag_export|ProcTag_dll_export)) {
  1346. if (proc->tags & (ProcTag_export)) {
  1347. ir_fprintf(f, "dllexport ");
  1348. }
  1349. }
  1350. }
  1351. TypeProc *proc_type = &proc->type->Proc;
  1352. ir_print_calling_convention(f, m, proc_type->calling_convention);
  1353. isize param_count = proc_type->param_count;
  1354. isize result_count = proc_type->result_count;
  1355. ir_print_proc_results(f, m, proc->type);
  1356. ir_fprintf(f, " ");
  1357. // #ifndef GB_SYSTEM_WINDOWS
  1358. #if 0
  1359. if(uses_args)
  1360. ir_fprintf(f, "@.nix_argpatch_main");
  1361. else
  1362. #endif
  1363. ir_print_encoded_global(f, proc->name, ir_print_is_proc_global(m, proc));
  1364. ir_fprintf(f, "(");
  1365. if (proc_type->return_by_pointer) {
  1366. ir_print_type(f, m, reduce_tuple_to_single_type(proc_type->results));
  1367. ir_fprintf(f, "* sret noalias ");
  1368. ir_fprintf(f, "%%agg.result");
  1369. if (param_count > 0) {
  1370. ir_fprintf(f, ", ");
  1371. }
  1372. }
  1373. isize param_index = 0;
  1374. if (param_count > 0) {
  1375. TypeTuple *params = &proc_type->params->Tuple;
  1376. for (isize i = 0; i < param_count; i++) {
  1377. Entity *e = params->variables[i];
  1378. Type *original_type = e->type;
  1379. Type *abi_type = proc_type->abi_compat_params[i];
  1380. if (e->kind != Entity_Variable) continue;
  1381. if (param_index > 0) ir_fprintf(f, ", ");
  1382. if (i+1 == params->variable_count && proc_type->c_vararg) {
  1383. ir_fprintf(f, " ...");
  1384. } else {
  1385. ir_print_type(f, m, abi_type);
  1386. if (e->flags&EntityFlag_NoAlias) {
  1387. ir_fprintf(f, " noalias");
  1388. }
  1389. if (proc->body != nullptr) {
  1390. if (e->token.string != "" &&
  1391. e->token.string != "_") {
  1392. ir_fprintf(f, " ");
  1393. ir_print_encoded_local(f, e->token.string);
  1394. } else {
  1395. ir_fprintf(f, " %%_.param_%td", i);
  1396. }
  1397. }
  1398. }
  1399. param_index++;
  1400. }
  1401. }
  1402. if (proc_type->calling_convention == ProcCC_Odin) {
  1403. if (param_index > 0) ir_fprintf(f, ", ");
  1404. ir_print_type(f, m, t_context_ptr);
  1405. ir_fprintf(f, " noalias nonnull %%__.context_ptr");
  1406. }
  1407. ir_fprintf(f, ") ");
  1408. if (proc->tags & ProcTag_inline) {
  1409. ir_fprintf(f, "alwaysinline ");
  1410. }
  1411. if (proc->tags & ProcTag_no_inline) {
  1412. ir_fprintf(f, "noinline ");
  1413. }
  1414. if (proc->entity != nullptr) {
  1415. if (proc->body != nullptr) {
  1416. irDebugInfo **di_ = map_get(&proc->module->debug_info, hash_pointer(proc->entity));
  1417. if (di_ != nullptr) {
  1418. irDebugInfo *di = *di_;
  1419. GB_ASSERT(di->kind == irDebugInfo_Proc);
  1420. // ir_fprintf(f, "!dbg !%d ", di->id);
  1421. }
  1422. }
  1423. }
  1424. if (proc->body != nullptr) {
  1425. // ir_fprintf(f, "nounwind uwtable {\n");
  1426. ir_fprintf(f, "{\n");
  1427. for_array(i, proc->blocks) {
  1428. irBlock *block = proc->blocks[i];
  1429. if (i > 0) ir_fprintf(f, "\n");
  1430. ir_print_block_name(f, block);
  1431. ir_fprintf(f, ":\n");
  1432. for_array(j, block->instrs) {
  1433. irValue *value = block->instrs[j];
  1434. ir_print_instr(f, m, value);
  1435. }
  1436. }
  1437. ir_fprintf(f, "}\n");
  1438. } else {
  1439. ir_fprintf(f, "\n");
  1440. }
  1441. for_array(i, proc->children) {
  1442. ir_print_proc(f, m, proc->children[i]);
  1443. }
  1444. }
  1445. void ir_print_type_name(irFileBuffer *f, irModule *m, irValue *v) {
  1446. GB_ASSERT(v->kind == irValue_TypeName);
  1447. Type *bt = base_type(ir_type(v));
  1448. if (!is_type_struct(bt) && !is_type_union(bt)) {
  1449. return;
  1450. }
  1451. ir_print_encoded_local(f, v->TypeName.name);
  1452. ir_fprintf(f, " = type ");
  1453. ir_print_type(f, m, base_type(v->TypeName.type));
  1454. ir_fprintf(f, "\n");
  1455. }
  1456. void print_llvm_ir(irGen *ir) {
  1457. irModule *m = &ir->module;
  1458. irFileBuffer buf = {}, *f = &buf;
  1459. ir_file_buffer_init(f, &ir->output_file);
  1460. ir_print_encoded_local(f, str_lit("..string"));
  1461. ir_fprintf(f, " = type {i8*, ");
  1462. ir_print_type(f, m, t_int);
  1463. ir_fprintf(f, "} ; Basic_string\n");
  1464. ir_print_encoded_local(f, str_lit("..rawptr"));
  1465. ir_fprintf(f, " = type i8* ; Basic_rawptr\n");
  1466. ir_print_encoded_local(f, str_lit("..complex32"));
  1467. ir_fprintf(f, " = type {half, half} ; Basic_complex32\n");
  1468. ir_print_encoded_local(f, str_lit("..complex64"));
  1469. ir_fprintf(f, " = type {float, float} ; Basic_complex64\n");
  1470. ir_print_encoded_local(f, str_lit("..complex128"));
  1471. ir_fprintf(f, " = type {double, double} ; Basic_complex128\n");
  1472. ir_print_encoded_local(f, str_lit("..any"));
  1473. ir_fprintf(f, " = type {");
  1474. ir_print_type(f, m, t_rawptr);
  1475. ir_fprintf(f, ", ");
  1476. ir_print_type(f, m, t_type_info_ptr);
  1477. ir_fprintf(f, "} ; Basic_any\n");
  1478. ir_fprintf(f, "declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone \n");
  1479. ir_fprintf(f, "\n");
  1480. for_array(member_index, m->members.entries) {
  1481. auto *entry = &m->members.entries[member_index];
  1482. irValue *v = entry->value;
  1483. if (v->kind != irValue_TypeName) {
  1484. continue;
  1485. }
  1486. ir_print_type_name(f, m, v);
  1487. }
  1488. ir_fprintf(f, "\n");
  1489. bool dll_main_found = false;
  1490. for_array(member_index, m->members.entries) {
  1491. auto *entry = &m->members.entries[member_index];
  1492. irValue *v = entry->value;
  1493. if (v->kind != irValue_Proc) {
  1494. continue;
  1495. }
  1496. if (v->Proc.body == nullptr) {
  1497. ir_print_proc(f, m, &v->Proc);
  1498. }
  1499. }
  1500. for_array(member_index, m->members.entries) {
  1501. auto *entry = &m->members.entries[member_index];
  1502. irValue *v = entry->value;
  1503. if (v->kind != irValue_Proc) {
  1504. continue;
  1505. }
  1506. if (v->Proc.body != nullptr) {
  1507. ir_print_proc(f, m, &v->Proc);
  1508. }
  1509. }
  1510. for_array(member_index, m->members.entries) {
  1511. auto *entry = &m->members.entries[member_index];
  1512. irValue *v = entry->value;
  1513. if (v->kind != irValue_Global) {
  1514. continue;
  1515. }
  1516. irValueGlobal *g = &v->Global;
  1517. Scope *scope = g->entity->scope;
  1518. bool in_global_scope = false;
  1519. if (scope != nullptr) {
  1520. // TODO(bill): Fix this rule. What should it be?
  1521. in_global_scope = scope->is_global || scope->is_init;
  1522. // in_global_scope = value->Global.name_is_not_mangled;
  1523. }
  1524. ir_print_encoded_global(f, ir_get_global_name(m, v), in_global_scope);
  1525. ir_fprintf(f, " = ");
  1526. if (g->is_foreign) {
  1527. ir_fprintf(f, "external ");
  1528. }
  1529. if (g->is_thread_local) {
  1530. ir_fprintf(f, "thread_local ");
  1531. }
  1532. if (g->is_private) {
  1533. ir_fprintf(f, "private ");
  1534. }
  1535. if (g->is_constant) {
  1536. if (g->is_unnamed_addr) {
  1537. ir_fprintf(f, "unnamed_addr ");
  1538. }
  1539. ir_fprintf(f, "constant ");
  1540. } else {
  1541. ir_fprintf(f, "global ");
  1542. }
  1543. ir_print_type(f, m, g->entity->type);
  1544. ir_fprintf(f, " ");
  1545. if (!g->is_foreign) {
  1546. if (g->value != nullptr) {
  1547. ir_print_value(f, m, g->value, g->entity->type);
  1548. } else {
  1549. ir_fprintf(f, "zeroinitializer");
  1550. }
  1551. }
  1552. ir_fprintf(f, "\n");
  1553. }
  1554. #if 0
  1555. // if (m->generate_debug_info) {
  1556. {
  1557. ir_fprintf(f, "\n");
  1558. i32 diec = m->debug_info.entries.count;
  1559. ir_fprintf(f, "!llvm.dbg.cu = !{!0}\n");
  1560. ir_fprintf(f, "!llvm.ident = !{!%d}\n", diec+3);
  1561. ir_fprintf(f, "!%d = !{i32 2, !\"Dwarf Version\", i32 4}\n", diec+0);
  1562. ir_fprintf(f, "!%d = !{i32 2, !\"Debug Info Version\", i32 3}\n", diec+1);
  1563. ir_fprintf(f, "!%d = !{i32 1, !\"PIC Level\", i32 2}\n", diec+2);
  1564. ir_fprintf(f, "!%d = !{!\"clang version 3.9.0 (branches/release_39)\"}\n", diec+3);
  1565. for_array(di_index, m->debug_info.entries) {
  1566. MapIrDebugInfoEntry *entry = &m->debug_info.entries[di_index];
  1567. irDebugInfo *di = entry->value;
  1568. ir_fprintf(f, "!%d = ", di->id);
  1569. switch (di->kind) {
  1570. case irDebugInfo_CompileUnit: {
  1571. irDebugInfo *file = *map_get(&m->debug_info, hash_pointer(di->CompileUnit.file));
  1572. ir_fprintf(f,
  1573. "distinct !DICompileUnit("
  1574. "language: DW_LANG_Go, " // Is this good enough?
  1575. "file: !%d, "
  1576. "producer: \"clang version 3.9.0 (branches/release_39)\", "
  1577. "flags: \"\", "
  1578. "runtimeVersion: 0, "
  1579. "isOptimized: false, "
  1580. "emissionKind: FullDebug"
  1581. ")",
  1582. file->id);
  1583. } break;
  1584. case irDebugInfo_File:
  1585. ir_fprintf(f, "!DIFile(filename: \"");
  1586. ir_print_escape_string(f, di->File.filename, false);
  1587. ir_fprintf(f, "\", directory: \"");
  1588. ir_print_escape_string(f, di->File.directory, false);
  1589. ir_fprintf(f, "\")");
  1590. break;
  1591. case irDebugInfo_Proc:
  1592. ir_fprintf(f, "distinct !DISubprogram("
  1593. "name: \"%.*s\", "
  1594. // "linkageName: \"\", "
  1595. "file: !%d, "
  1596. "line: %td, "
  1597. "isDefinition: true, "
  1598. "isLocal: false, "
  1599. "unit: !0"
  1600. ")",
  1601. LIT(di->Proc.name),
  1602. di->Proc.file->id,
  1603. di->Proc.pos.line);
  1604. break;
  1605. case irDebugInfo_AllProcs:
  1606. ir_fprintf(f, "!{");
  1607. for_array(proc_index, di->AllProcs.procs) {
  1608. irDebugInfo *p = di->AllProcs.procs[proc_index];
  1609. if (proc_index > 0) {ir_fprintf(f, ",");}
  1610. ir_fprintf(f, "!%d", p->id);
  1611. }
  1612. ir_fprintf(f, "}");
  1613. break;
  1614. }
  1615. ir_fprintf(f, "\n");
  1616. }
  1617. }
  1618. #endif
  1619. ir_file_buffer_destroy(f);
  1620. }