llvm_backend_const.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. bool lb_is_const(lbValue value) {
  2. LLVMValueRef v = value.value;
  3. if (is_type_untyped_nil(value.type) || is_type_untyped_undef(value.type)) {
  4. // TODO(bill): Is this correct behaviour?
  5. return true;
  6. }
  7. if (LLVMIsConstant(v)) {
  8. return true;
  9. }
  10. return false;
  11. }
  12. bool lb_is_const_or_global(lbValue value) {
  13. if (lb_is_const(value)) {
  14. return true;
  15. }
  16. if (LLVMGetValueKind(value.value) == LLVMGlobalVariableValueKind) {
  17. LLVMTypeRef t = LLVMGetElementType(LLVMTypeOf(value.value));
  18. if (!lb_is_type_kind(t, LLVMPointerTypeKind)) {
  19. return false;
  20. }
  21. LLVMTypeRef elem = LLVMGetElementType(t);
  22. return lb_is_type_kind(elem, LLVMFunctionTypeKind);
  23. }
  24. return false;
  25. }
  26. bool lb_is_elem_const(Ast *elem, Type *elem_type) {
  27. if (!elem_type_can_be_constant(elem_type)) {
  28. return false;
  29. }
  30. if (elem->kind == Ast_FieldValue) {
  31. elem = elem->FieldValue.value;
  32. }
  33. TypeAndValue tav = type_and_value_of_expr(elem);
  34. GB_ASSERT_MSG(tav.mode != Addressing_Invalid, "%s %s", expr_to_string(elem), type_to_string(tav.type));
  35. return tav.value.kind != ExactValue_Invalid;
  36. }
  37. bool lb_is_const_nil(lbValue value) {
  38. LLVMValueRef v = value.value;
  39. if (LLVMIsConstant(v)) {
  40. if (LLVMIsAConstantAggregateZero(v)) {
  41. return true;
  42. } else if (LLVMIsAConstantPointerNull(v)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. bool lb_is_expr_constant_zero(Ast *expr) {
  49. GB_ASSERT(expr != nullptr);
  50. auto v = exact_value_to_integer(expr->tav.value);
  51. if (v.kind == ExactValue_Integer) {
  52. return big_int_cmp_zero(&v.value_integer) == 0;
  53. }
  54. return false;
  55. }
  56. String lb_get_const_string(lbModule *m, lbValue value) {
  57. GB_ASSERT(lb_is_const(value));
  58. GB_ASSERT(LLVMIsConstant(value.value));
  59. Type *t = base_type(value.type);
  60. GB_ASSERT(are_types_identical(t, t_string));
  61. unsigned ptr_indices[1] = {0};
  62. unsigned len_indices[1] = {1};
  63. LLVMValueRef underlying_ptr = LLVMConstExtractValue(value.value, ptr_indices, gb_count_of(ptr_indices));
  64. LLVMValueRef underlying_len = LLVMConstExtractValue(value.value, len_indices, gb_count_of(len_indices));
  65. GB_ASSERT(LLVMGetConstOpcode(underlying_ptr) == LLVMGetElementPtr);
  66. underlying_ptr = LLVMGetOperand(underlying_ptr, 0);
  67. GB_ASSERT(LLVMIsAGlobalVariable(underlying_ptr));
  68. underlying_ptr = LLVMGetInitializer(underlying_ptr);
  69. size_t length = 0;
  70. char const *text = LLVMGetAsString(underlying_ptr, &length);
  71. isize real_length = cast(isize)LLVMConstIntGetSExtValue(underlying_len);
  72. return make_string(cast(u8 const *)text, real_length);
  73. }
  74. LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) {
  75. LLVMTypeRef src = LLVMTypeOf(val);
  76. if (src == dst) {
  77. return val;
  78. }
  79. if (LLVMIsNull(val)) {
  80. return LLVMConstNull(dst);
  81. }
  82. GB_ASSERT_MSG(lb_sizeof(dst) == lb_sizeof(src), "%s vs %s", LLVMPrintTypeToString(dst), LLVMPrintTypeToString(src));
  83. LLVMTypeKind kind = LLVMGetTypeKind(dst);
  84. switch (kind) {
  85. case LLVMPointerTypeKind:
  86. return LLVMConstPointerCast(val, dst);
  87. case LLVMStructTypeKind:
  88. return LLVMConstBitCast(val, dst);
  89. default:
  90. GB_PANIC("Unhandled const cast %s to %s", LLVMPrintTypeToString(src), LLVMPrintTypeToString(dst));
  91. }
  92. return val;
  93. }
  94. lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) {
  95. GB_ASSERT(is_type_internally_pointer_like(value.type));
  96. GB_ASSERT(is_type_internally_pointer_like(t));
  97. GB_ASSERT(lb_is_const(value));
  98. lbValue res = {};
  99. res.value = LLVMConstPointerCast(value.value, lb_type(m, t));
  100. res.type = t;
  101. return res;
  102. }
  103. LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) {
  104. LLVMTypeRef struct_type = lb_type(m, t);
  105. GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind);
  106. unsigned value_count = cast(unsigned)value_count_;
  107. unsigned elem_count = LLVMCountStructElementTypes(struct_type);
  108. if (elem_count == value_count) {
  109. return llvm_const_named_struct_internal(struct_type, values, value_count_);
  110. }
  111. Type *bt = base_type(t);
  112. GB_ASSERT(bt->kind == Type_Struct);
  113. GB_ASSERT(value_count_ == bt->Struct.fields.count);
  114. auto field_remapping = lb_get_struct_remapping(m, t);
  115. unsigned values_with_padding_count = LLVMCountStructElementTypes(struct_type);
  116. LLVMValueRef *values_with_padding = gb_alloc_array(permanent_allocator(), LLVMValueRef, values_with_padding_count);
  117. for (unsigned i = 0; i < value_count; i++) {
  118. values_with_padding[field_remapping[i]] = values[i];
  119. }
  120. for (unsigned i = 0; i < values_with_padding_count; i++) {
  121. if (values_with_padding[i] == nullptr) {
  122. values_with_padding[i] = LLVMConstNull(LLVMStructGetTypeAtIndex(struct_type, i));
  123. }
  124. }
  125. return llvm_const_named_struct_internal(struct_type, values_with_padding, values_with_padding_count);
  126. }
  127. LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_) {
  128. unsigned value_count = cast(unsigned)value_count_;
  129. unsigned elem_count = LLVMCountStructElementTypes(t);
  130. GB_ASSERT_MSG(value_count == elem_count, "%s %u %u", LLVMPrintTypeToString(t), value_count, elem_count);
  131. for (unsigned i = 0; i < elem_count; i++) {
  132. LLVMTypeRef elem_type = LLVMStructGetTypeAtIndex(t, i);
  133. values[i] = llvm_const_cast(values[i], elem_type);
  134. }
  135. return LLVMConstNamedStruct(t, values, value_count);
  136. }
  137. LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
  138. unsigned value_count = cast(unsigned)value_count_;
  139. for (unsigned i = 0; i < value_count; i++) {
  140. values[i] = llvm_const_cast(values[i], elem_type);
  141. }
  142. return LLVMConstArray(elem_type, values, value_count);
  143. }
  144. LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) {
  145. GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type));
  146. GB_ASSERT(are_types_identical(len.type, t_int));
  147. LLVMValueRef vals[2] = {
  148. data.value,
  149. len.value,
  150. };
  151. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  152. }
  153. lbValue lb_const_nil(lbModule *m, Type *type) {
  154. LLVMValueRef v = LLVMConstNull(lb_type(m, type));
  155. return lbValue{v, type};
  156. }
  157. lbValue lb_const_undef(lbModule *m, Type *type) {
  158. LLVMValueRef v = LLVMGetUndef(lb_type(m, type));
  159. return lbValue{v, type};
  160. }
  161. lbValue lb_const_int(lbModule *m, Type *type, u64 value) {
  162. lbValue res = {};
  163. res.value = LLVMConstInt(lb_type(m, type), cast(unsigned long long)value, !is_type_unsigned(type));
  164. res.type = type;
  165. return res;
  166. }
  167. lbValue lb_const_string(lbModule *m, String const &value) {
  168. return lb_const_value(m, t_string, exact_value_string(value));
  169. }
  170. lbValue lb_const_bool(lbModule *m, Type *type, bool value) {
  171. lbValue res = {};
  172. res.value = LLVMConstInt(lb_type(m, type), value, false);
  173. res.type = type;
  174. return res;
  175. }
  176. LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) {
  177. GB_ASSERT(type_size_of(type) == 2);
  178. u16 u = f32_to_f16(f);
  179. if (is_type_different_to_arch_endianness(type)) {
  180. u = gb_endian_swap16(u);
  181. }
  182. LLVMValueRef i = LLVMConstInt(LLVMInt16TypeInContext(m->ctx), u, false);
  183. return LLVMConstBitCast(i, lb_type(m, type));
  184. }
  185. LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) {
  186. GB_ASSERT(type_size_of(type) == 4);
  187. u32 u = bit_cast<u32>(f);
  188. if (is_type_different_to_arch_endianness(type)) {
  189. u = gb_endian_swap32(u);
  190. }
  191. LLVMValueRef i = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), u, false);
  192. return LLVMConstBitCast(i, lb_type(m, type));
  193. }
  194. bool lb_is_expr_untyped_const(Ast *expr) {
  195. auto const &tv = type_and_value_of_expr(expr);
  196. if (is_type_untyped(tv.type)) {
  197. return tv.value.kind != ExactValue_Invalid;
  198. }
  199. return false;
  200. }
  201. lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
  202. GB_ASSERT(is_type_typed(t));
  203. auto const &tv = type_and_value_of_expr(expr);
  204. return lb_const_value(m, t, tv.value);
  205. }
  206. lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  207. lbModule *m = p->module;
  208. LLVMValueRef fields[4] = {};
  209. fields[0]/*file*/ = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id)).value;
  210. fields[1]/*line*/ = lb_const_int(m, t_i32, pos.line).value;
  211. fields[2]/*column*/ = lb_const_int(m, t_i32, pos.column).value;
  212. fields[3]/*procedure*/ = lb_find_or_add_entity_string(p->module, procedure).value;
  213. lbValue res = {};
  214. res.value = llvm_const_named_struct(m, t_source_code_location, fields, gb_count_of(fields));
  215. res.type = t_source_code_location;
  216. return res;
  217. }
  218. lbValue lb_emit_source_code_location(lbProcedure *p, Ast *node) {
  219. String proc_name = {};
  220. if (p->entity) {
  221. proc_name = p->entity->token.string;
  222. }
  223. TokenPos pos = {};
  224. if (node) {
  225. pos = ast_token(node).pos;
  226. }
  227. return lb_emit_source_code_location(p, proc_name, pos);
  228. }
  229. LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) {
  230. bool is_local = allow_local && m->curr_procedure != nullptr;
  231. bool is_const = true;
  232. if (is_local) {
  233. for (isize i = 0; i < count; i++) {
  234. GB_ASSERT(values[i] != nullptr);
  235. if (!LLVMIsConstant(values[i])) {
  236. is_const = false;
  237. break;
  238. }
  239. }
  240. }
  241. if (!is_const) {
  242. lbProcedure *p = m->curr_procedure;
  243. GB_ASSERT(p != nullptr);
  244. lbAddr v = lb_add_local_generated(p, type, false);
  245. lbValue ptr = lb_addr_get_ptr(p, v);
  246. for (isize i = 0; i < count; i++) {
  247. lbValue elem = lb_emit_array_epi(p, ptr, i);
  248. LLVMBuildStore(p->builder, values[i], elem.value);
  249. }
  250. return lb_addr_load(p, v).value;
  251. }
  252. return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count);
  253. }
  254. LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
  255. if (big_int_is_zero(a)) {
  256. return LLVMConstNull(lb_type(m, original_type));
  257. }
  258. size_t sz = cast(size_t)type_size_of(original_type);
  259. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will be fine :P
  260. u8 *rop = cast(u8 *)rop64;
  261. size_t max_count = 0;
  262. size_t written = 0;
  263. size_t size = 1;
  264. size_t nails = 0;
  265. mp_endian endian = MP_LITTLE_ENDIAN;
  266. max_count = mp_pack_count(a, nails, size);
  267. if (sz < max_count) {
  268. debug_print_big_int(a);
  269. gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
  270. }
  271. GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
  272. GB_ASSERT(gb_size_of(rop64) >= sz);
  273. mp_err err = mp_pack(rop, sz, &written,
  274. MP_LSB_FIRST,
  275. size, endian, nails,
  276. a);
  277. GB_ASSERT(err == MP_OKAY);
  278. if (!is_type_endian_little(original_type)) {
  279. for (size_t i = 0; i < sz/2; i++) {
  280. u8 tmp = rop[i];
  281. rop[i] = rop[sz-1-i];
  282. rop[sz-1-i] = tmp;
  283. }
  284. }
  285. LLVMValueRef value = LLVMConstIntOfArbitraryPrecision(lb_type(m, original_type), cast(unsigned)(sz+7/8), cast(u64 *)rop);
  286. if (big_int_is_neg(a)) {
  287. value = LLVMConstNeg(value);
  288. }
  289. return value;
  290. }
  291. lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local) {
  292. LLVMContextRef ctx = m->ctx;
  293. type = default_type(type);
  294. Type *original_type = type;
  295. lbValue res = {};
  296. res.type = original_type;
  297. type = core_type(type);
  298. value = convert_exact_value_for_type(value, type);
  299. if (value.kind == ExactValue_Typeid) {
  300. return lb_typeid(m, value.value_typeid);
  301. }
  302. if (value.kind == ExactValue_Invalid) {
  303. return lb_const_nil(m, type);
  304. }
  305. if (value.kind == ExactValue_Procedure) {
  306. Ast *expr = unparen_expr(value.value_procedure);
  307. if (expr->kind == Ast_ProcLit) {
  308. return lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  309. }
  310. Entity *e = entity_from_expr(expr);
  311. return lb_find_procedure_value_from_entity(m, e);
  312. }
  313. bool is_local = allow_local && m->curr_procedure != nullptr;
  314. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  315. if (is_type_slice(type)) {
  316. if (value.kind == ExactValue_String) {
  317. GB_ASSERT(is_type_u8_slice(type));
  318. res.value = lb_find_or_add_entity_string_byte_slice(m, value.value_string).value;
  319. return res;
  320. } else {
  321. ast_node(cl, CompoundLit, value.value_compound);
  322. isize count = cl->elems.count;
  323. if (count == 0) {
  324. return lb_const_nil(m, type);
  325. }
  326. count = gb_max(cast(isize)cl->max_count, count);
  327. Type *elem = base_type(type)->Slice.elem;
  328. Type *t = alloc_type_array(elem, count);
  329. lbValue backing_array = lb_const_value(m, t, value, allow_local);
  330. LLVMValueRef array_data = nullptr;
  331. if (is_local) {
  332. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  333. // its backing data on the stack
  334. lbProcedure *p = m->curr_procedure;
  335. LLVMTypeRef llvm_type = lb_type(m, t);
  336. array_data = llvm_alloca(p, llvm_type, 16);
  337. LLVMBuildStore(p->builder, backing_array.value, array_data);
  338. {
  339. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  340. LLVMValueRef ptr = LLVMBuildInBoundsGEP(p->builder, array_data, indices, 2, "");
  341. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  342. lbAddr slice = lb_add_local_generated(p, type, false);
  343. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  344. return lb_addr_load(p, slice);
  345. }
  346. } else {
  347. isize max_len = 7+8+1;
  348. char *str = gb_alloc_array(permanent_allocator(), char, max_len);
  349. u32 id = m->gen->global_array_index.fetch_add(1);
  350. isize len = gb_snprintf(str, max_len, "csba$%x", id);
  351. String name = make_string(cast(u8 *)str, len-1);
  352. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  353. array_data = LLVMAddGlobal(m->mod, lb_type(m, t), str);
  354. LLVMSetInitializer(array_data, backing_array.value);
  355. lbValue g = {};
  356. g.value = array_data;
  357. g.type = t;
  358. lb_add_entity(m, e, g);
  359. lb_add_member(m, name, g);
  360. {
  361. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  362. LLVMValueRef ptr = LLVMConstInBoundsGEP(array_data, indices, 2);
  363. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  364. LLVMValueRef values[2] = {ptr, len};
  365. res.value = llvm_const_named_struct(m, original_type, values, 2);
  366. return res;
  367. }
  368. }
  369. }
  370. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  371. if (is_type_rune_array(type) && value.kind == ExactValue_String) {
  372. i64 count = type->Array.count;
  373. Type *elem = type->Array.elem;
  374. LLVMTypeRef et = lb_type(m, elem);
  375. Rune rune;
  376. isize offset = 0;
  377. isize width = 1;
  378. String s = value.value_string;
  379. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  380. for (i64 i = 0; i < count && offset < s.len; i++) {
  381. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  382. offset += width;
  383. elems[i] = LLVMConstInt(et, rune, true);
  384. }
  385. GB_ASSERT(offset == s.len);
  386. res.value = llvm_const_array(et, elems, cast(unsigned)count);
  387. return res;
  388. }
  389. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  390. Type *core_elem = core_array_type(type);
  391. return lb_const_value(m, core_elem, value, allow_local);
  392. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  393. GB_ASSERT(type->Array.count == value.value_string.len);
  394. LLVMValueRef data = LLVMConstStringInContext(ctx,
  395. cast(char const *)value.value_string.text,
  396. cast(unsigned)value.value_string.len,
  397. true /*DontNullTerminate*/);
  398. res.value = data;
  399. return res;
  400. } else if (is_type_array(type) &&
  401. value.kind != ExactValue_Invalid &&
  402. value.kind != ExactValue_String &&
  403. value.kind != ExactValue_Compound) {
  404. i64 count = type->Array.count;
  405. Type *elem = type->Array.elem;
  406. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  407. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  408. for (i64 i = 0; i < count; i++) {
  409. elems[i] = single_elem.value;
  410. }
  411. res.value = llvm_const_array(lb_type(m, elem), elems, cast(unsigned)count);
  412. return res;
  413. } else if (is_type_matrix(type) &&
  414. value.kind != ExactValue_Invalid &&
  415. value.kind != ExactValue_Compound) {
  416. i64 row = type->Matrix.row_count;
  417. i64 column = type->Matrix.column_count;
  418. GB_ASSERT(row == column);
  419. Type *elem = type->Matrix.elem;
  420. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  421. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  422. i64 total_elem_count = matrix_type_total_internal_elems(type);
  423. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  424. for (i64 i = 0; i < row; i++) {
  425. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  426. }
  427. for (i64 i = 0; i < total_elem_count; i++) {
  428. if (elems[i] == nullptr) {
  429. elems[i] = LLVMConstNull(lb_type(m, elem));
  430. }
  431. }
  432. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  433. return res;
  434. } else if (is_type_simd_vector(type) &&
  435. value.kind != ExactValue_Invalid &&
  436. value.kind != ExactValue_Compound) {
  437. i64 count = type->SimdVector.count;
  438. Type *elem = type->SimdVector.elem;
  439. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  440. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  441. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  442. for (i64 i = 0; i < count; i++) {
  443. elems[i] = single_elem.value;
  444. }
  445. res.value = LLVMConstVector(elems, cast(unsigned)count);
  446. return res;
  447. }
  448. switch (value.kind) {
  449. case ExactValue_Invalid:
  450. res.value = LLVMConstNull(lb_type(m, original_type));
  451. return res;
  452. case ExactValue_Bool:
  453. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  454. return res;
  455. case ExactValue_String:
  456. {
  457. LLVMValueRef ptr = lb_find_or_add_entity_string_ptr(m, value.value_string);
  458. lbValue res = {};
  459. res.type = default_type(original_type);
  460. if (is_type_cstring(res.type)) {
  461. res.value = ptr;
  462. } else {
  463. if (value.value_string.len == 0) {
  464. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  465. }
  466. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string.len, true);
  467. LLVMValueRef values[2] = {ptr, str_len};
  468. GB_ASSERT(is_type_string(original_type));
  469. res.value = llvm_const_named_struct(m, original_type, values, 2);
  470. }
  471. return res;
  472. }
  473. case ExactValue_Integer:
  474. if (is_type_pointer(type) || is_type_multi_pointer(type)) {
  475. LLVMTypeRef t = lb_type(m, original_type);
  476. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  477. res.value = LLVMConstIntToPtr(i, t);
  478. } else {
  479. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  480. }
  481. return res;
  482. case ExactValue_Float:
  483. if (is_type_different_to_arch_endianness(type)) {
  484. u64 u = bit_cast<u64>(value.value_float);
  485. u = gb_endian_swap64(u);
  486. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  487. } else {
  488. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  489. }
  490. return res;
  491. case ExactValue_Complex:
  492. {
  493. LLVMValueRef values[2] = {};
  494. switch (8*type_size_of(type)) {
  495. case 32:
  496. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  497. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  498. break;
  499. case 64:
  500. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  501. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  502. break;
  503. case 128:
  504. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  505. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  506. break;
  507. }
  508. res.value = llvm_const_named_struct(m, original_type, values, 2);
  509. return res;
  510. }
  511. break;
  512. case ExactValue_Quaternion:
  513. {
  514. LLVMValueRef values[4] = {};
  515. switch (8*type_size_of(type)) {
  516. case 64:
  517. // @QuaternionLayout
  518. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  519. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  520. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  521. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  522. break;
  523. case 128:
  524. // @QuaternionLayout
  525. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  526. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  527. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  528. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  529. break;
  530. case 256:
  531. // @QuaternionLayout
  532. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  533. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  534. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  535. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  536. break;
  537. }
  538. res.value = llvm_const_named_struct(m, original_type, values, 4);
  539. return res;
  540. }
  541. break;
  542. case ExactValue_Pointer:
  543. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  544. return res;
  545. case ExactValue_Compound:
  546. if (is_type_slice(type)) {
  547. return lb_const_value(m, type, value, allow_local);
  548. } else if (is_type_array(type)) {
  549. ast_node(cl, CompoundLit, value.value_compound);
  550. Type *elem_type = type->Array.elem;
  551. isize elem_count = cl->elems.count;
  552. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  553. return lb_const_nil(m, original_type);
  554. }
  555. if (cl->elems[0]->kind == Ast_FieldValue) {
  556. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  557. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  558. isize value_index = 0;
  559. for (i64 i = 0; i < type->Array.count; i++) {
  560. bool found = false;
  561. for (isize j = 0; j < elem_count; j++) {
  562. Ast *elem = cl->elems[j];
  563. ast_node(fv, FieldValue, elem);
  564. if (is_ast_range(fv->field)) {
  565. ast_node(ie, BinaryExpr, fv->field);
  566. TypeAndValue lo_tav = ie->left->tav;
  567. TypeAndValue hi_tav = ie->right->tav;
  568. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  569. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  570. TokenKind op = ie->op.kind;
  571. i64 lo = exact_value_to_i64(lo_tav.value);
  572. i64 hi = exact_value_to_i64(hi_tav.value);
  573. if (op != Token_RangeHalf) {
  574. hi += 1;
  575. }
  576. if (lo == i) {
  577. TypeAndValue tav = fv->value->tav;
  578. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  579. for (i64 k = lo; k < hi; k++) {
  580. values[value_index++] = val;
  581. }
  582. found = true;
  583. i += (hi-lo-1);
  584. break;
  585. }
  586. } else {
  587. TypeAndValue index_tav = fv->field->tav;
  588. GB_ASSERT(index_tav.mode == Addressing_Constant);
  589. i64 index = exact_value_to_i64(index_tav.value);
  590. if (index == i) {
  591. TypeAndValue tav = fv->value->tav;
  592. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  593. values[value_index++] = val;
  594. found = true;
  595. break;
  596. }
  597. }
  598. }
  599. if (!found) {
  600. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  601. }
  602. }
  603. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
  604. return res;
  605. } else {
  606. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  607. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  608. for (isize i = 0; i < elem_count; i++) {
  609. TypeAndValue tav = cl->elems[i]->tav;
  610. GB_ASSERT(tav.mode != Addressing_Invalid);
  611. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  612. }
  613. for (isize i = elem_count; i < type->Array.count; i++) {
  614. values[i] = LLVMConstNull(lb_type(m, elem_type));
  615. }
  616. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
  617. return res;
  618. }
  619. } else if (is_type_enumerated_array(type)) {
  620. ast_node(cl, CompoundLit, value.value_compound);
  621. Type *elem_type = type->EnumeratedArray.elem;
  622. isize elem_count = cl->elems.count;
  623. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  624. return lb_const_nil(m, original_type);
  625. }
  626. if (cl->elems[0]->kind == Ast_FieldValue) {
  627. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  628. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  629. isize value_index = 0;
  630. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  631. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  632. for (i64 i = total_lo; i <= total_hi; i++) {
  633. bool found = false;
  634. for (isize j = 0; j < elem_count; j++) {
  635. Ast *elem = cl->elems[j];
  636. ast_node(fv, FieldValue, elem);
  637. if (is_ast_range(fv->field)) {
  638. ast_node(ie, BinaryExpr, fv->field);
  639. TypeAndValue lo_tav = ie->left->tav;
  640. TypeAndValue hi_tav = ie->right->tav;
  641. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  642. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  643. TokenKind op = ie->op.kind;
  644. i64 lo = exact_value_to_i64(lo_tav.value);
  645. i64 hi = exact_value_to_i64(hi_tav.value);
  646. if (op != Token_RangeHalf) {
  647. hi += 1;
  648. }
  649. if (lo == i) {
  650. TypeAndValue tav = fv->value->tav;
  651. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  652. for (i64 k = lo; k < hi; k++) {
  653. values[value_index++] = val;
  654. }
  655. found = true;
  656. i += (hi-lo-1);
  657. break;
  658. }
  659. } else {
  660. TypeAndValue index_tav = fv->field->tav;
  661. GB_ASSERT(index_tav.mode == Addressing_Constant);
  662. i64 index = exact_value_to_i64(index_tav.value);
  663. if (index == i) {
  664. TypeAndValue tav = fv->value->tav;
  665. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  666. values[value_index++] = val;
  667. found = true;
  668. break;
  669. }
  670. }
  671. }
  672. if (!found) {
  673. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  674. }
  675. }
  676. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  677. return res;
  678. } else {
  679. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  680. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  681. for (isize i = 0; i < elem_count; i++) {
  682. TypeAndValue tav = cl->elems[i]->tav;
  683. GB_ASSERT(tav.mode != Addressing_Invalid);
  684. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  685. }
  686. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  687. values[i] = LLVMConstNull(lb_type(m, elem_type));
  688. }
  689. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  690. return res;
  691. }
  692. } else if (is_type_simd_vector(type)) {
  693. ast_node(cl, CompoundLit, value.value_compound);
  694. Type *elem_type = type->SimdVector.elem;
  695. isize elem_count = cl->elems.count;
  696. if (elem_count == 0) {
  697. return lb_const_nil(m, original_type);
  698. }
  699. GB_ASSERT(elem_type_can_be_constant(elem_type));
  700. isize total_elem_count = cast(isize)type->SimdVector.count;
  701. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  702. if (cl->elems[0]->kind == Ast_FieldValue) {
  703. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  704. isize value_index = 0;
  705. for (i64 i = 0; i < total_elem_count; i++) {
  706. bool found = false;
  707. for (isize j = 0; j < elem_count; j++) {
  708. Ast *elem = cl->elems[j];
  709. ast_node(fv, FieldValue, elem);
  710. if (is_ast_range(fv->field)) {
  711. ast_node(ie, BinaryExpr, fv->field);
  712. TypeAndValue lo_tav = ie->left->tav;
  713. TypeAndValue hi_tav = ie->right->tav;
  714. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  715. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  716. TokenKind op = ie->op.kind;
  717. i64 lo = exact_value_to_i64(lo_tav.value);
  718. i64 hi = exact_value_to_i64(hi_tav.value);
  719. if (op != Token_RangeHalf) {
  720. hi += 1;
  721. }
  722. if (lo == i) {
  723. TypeAndValue tav = fv->value->tav;
  724. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  725. for (i64 k = lo; k < hi; k++) {
  726. values[value_index++] = val;
  727. }
  728. found = true;
  729. i += (hi-lo-1);
  730. break;
  731. }
  732. } else {
  733. TypeAndValue index_tav = fv->field->tav;
  734. GB_ASSERT(index_tav.mode == Addressing_Constant);
  735. i64 index = exact_value_to_i64(index_tav.value);
  736. if (index == i) {
  737. TypeAndValue tav = fv->value->tav;
  738. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  739. values[value_index++] = val;
  740. found = true;
  741. break;
  742. }
  743. }
  744. }
  745. if (!found) {
  746. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  747. }
  748. }
  749. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  750. return res;
  751. } else {
  752. for (isize i = 0; i < elem_count; i++) {
  753. TypeAndValue tav = cl->elems[i]->tav;
  754. GB_ASSERT(tav.mode != Addressing_Invalid);
  755. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  756. }
  757. LLVMTypeRef et = lb_type(m, elem_type);
  758. for (isize i = elem_count; i < total_elem_count; i++) {
  759. values[i] = LLVMConstNull(et);
  760. }
  761. for (isize i = 0; i < total_elem_count; i++) {
  762. values[i] = llvm_const_cast(values[i], et);
  763. }
  764. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  765. return res;
  766. }
  767. } else if (is_type_struct(type)) {
  768. ast_node(cl, CompoundLit, value.value_compound);
  769. if (cl->elems.count == 0) {
  770. return lb_const_nil(m, original_type);
  771. }
  772. if (is_type_raw_union(type)) {
  773. return lb_const_nil(m, original_type);
  774. }
  775. LLVMTypeRef struct_type = lb_type(m, original_type);
  776. auto field_remapping = lb_get_struct_remapping(m, type);
  777. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  778. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  779. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  780. if (cl->elems.count > 0) {
  781. if (cl->elems[0]->kind == Ast_FieldValue) {
  782. isize elem_count = cl->elems.count;
  783. for (isize i = 0; i < elem_count; i++) {
  784. ast_node(fv, FieldValue, cl->elems[i]);
  785. String name = fv->field->Ident.token.string;
  786. TypeAndValue tav = fv->value->tav;
  787. GB_ASSERT(tav.mode != Addressing_Invalid);
  788. Selection sel = lookup_field(type, name, false);
  789. Entity *f = type->Struct.fields[sel.index[0]];
  790. i32 index = field_remapping[f->Variable.field_index];
  791. if (elem_type_can_be_constant(f->type)) {
  792. values[index] = lb_const_value(m, f->type, tav.value, allow_local).value;
  793. visited[index] = true;
  794. }
  795. }
  796. } else {
  797. for_array(i, cl->elems) {
  798. Entity *f = type->Struct.fields[i];
  799. TypeAndValue tav = cl->elems[i]->tav;
  800. ExactValue val = {};
  801. if (tav.mode != Addressing_Invalid) {
  802. val = tav.value;
  803. }
  804. i32 index = field_remapping[f->Variable.field_index];
  805. if (elem_type_can_be_constant(f->type)) {
  806. values[index] = lb_const_value(m, f->type, val, allow_local).value;
  807. visited[index] = true;
  808. }
  809. }
  810. }
  811. }
  812. for (isize i = 0; i < value_count; i++) {
  813. if (!visited[i]) {
  814. GB_ASSERT(values[i] == nullptr);
  815. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  816. values[i] = LLVMConstNull(type);
  817. }
  818. }
  819. bool is_constant = true;
  820. for (isize i = 0; i < value_count; i++) {
  821. LLVMValueRef val = values[i];
  822. if (!LLVMIsConstant(val)) {
  823. GB_ASSERT(is_local);
  824. GB_ASSERT(LLVMGetInstructionOpcode(val) == LLVMLoad);
  825. is_constant = false;
  826. }
  827. }
  828. if (is_constant) {
  829. res.value = llvm_const_named_struct_internal(struct_type, values, cast(unsigned)value_count);
  830. return res;
  831. } else {
  832. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  833. LLVMValueRef *old_values = values;
  834. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  835. for (isize i = 0; i < value_count; i++) {
  836. LLVMValueRef old_value = old_values[i];
  837. if (LLVMIsConstant(old_value)) {
  838. new_values[i] = old_value;
  839. } else {
  840. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  841. }
  842. }
  843. LLVMValueRef constant_value = llvm_const_named_struct_internal(struct_type, new_values, cast(unsigned)value_count);
  844. GB_ASSERT(is_local);
  845. lbProcedure *p = m->curr_procedure;
  846. lbAddr v = lb_add_local_generated(p, res.type, true);
  847. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  848. for (isize i = 0; i < value_count; i++) {
  849. LLVMValueRef val = old_values[i];
  850. if (!LLVMIsConstant(val)) {
  851. LLVMValueRef dst = LLVMBuildStructGEP(p->builder, v.addr.value, cast(unsigned)i, "");
  852. LLVMBuildStore(p->builder, val, dst);
  853. }
  854. }
  855. return lb_addr_load(p, v);
  856. }
  857. } else if (is_type_bit_set(type)) {
  858. ast_node(cl, CompoundLit, value.value_compound);
  859. if (cl->elems.count == 0) {
  860. return lb_const_nil(m, original_type);
  861. }
  862. i64 sz = type_size_of(type);
  863. if (sz == 0) {
  864. return lb_const_nil(m, original_type);
  865. }
  866. BigInt bits = {};
  867. BigInt one = {};
  868. big_int_from_u64(&one, 1);
  869. for_array(i, cl->elems) {
  870. Ast *e = cl->elems[i];
  871. GB_ASSERT(e->kind != Ast_FieldValue);
  872. TypeAndValue tav = e->tav;
  873. if (tav.mode != Addressing_Constant) {
  874. continue;
  875. }
  876. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  877. i64 v = big_int_to_i64(&tav.value.value_integer);
  878. i64 lower = type->BitSet.lower;
  879. u64 index = cast(u64)(v-lower);
  880. gb_printf_err("index: %llu\n", index);
  881. BigInt bit = {};
  882. big_int_from_u64(&bit, index);
  883. big_int_shl(&bit, &one, &bit);
  884. big_int_or(&bits, &bits, &bit);
  885. }
  886. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  887. return res;
  888. } else if (is_type_matrix(type)) {
  889. ast_node(cl, CompoundLit, value.value_compound);
  890. Type *elem_type = type->Matrix.elem;
  891. isize elem_count = cl->elems.count;
  892. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  893. return lb_const_nil(m, original_type);
  894. }
  895. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  896. i64 total_count = matrix_type_total_internal_elems(type);
  897. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  898. if (cl->elems[0]->kind == Ast_FieldValue) {
  899. for_array(j, cl->elems) {
  900. Ast *elem = cl->elems[j];
  901. ast_node(fv, FieldValue, elem);
  902. if (is_ast_range(fv->field)) {
  903. ast_node(ie, BinaryExpr, fv->field);
  904. TypeAndValue lo_tav = ie->left->tav;
  905. TypeAndValue hi_tav = ie->right->tav;
  906. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  907. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  908. TokenKind op = ie->op.kind;
  909. i64 lo = exact_value_to_i64(lo_tav.value);
  910. i64 hi = exact_value_to_i64(hi_tav.value);
  911. if (op != Token_RangeHalf) {
  912. hi += 1;
  913. }
  914. GB_ASSERT(0 <= lo && lo <= max_count);
  915. GB_ASSERT(0 <= hi && hi <= max_count);
  916. GB_ASSERT(lo <= hi);
  917. TypeAndValue tav = fv->value->tav;
  918. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  919. for (i64 k = lo; k < hi; k++) {
  920. i64 offset = matrix_row_major_index_to_offset(type, k);
  921. GB_ASSERT(values[offset] == nullptr);
  922. values[offset] = val;
  923. }
  924. } else {
  925. TypeAndValue index_tav = fv->field->tav;
  926. GB_ASSERT(index_tav.mode == Addressing_Constant);
  927. i64 index = exact_value_to_i64(index_tav.value);
  928. GB_ASSERT(index < max_count);
  929. TypeAndValue tav = fv->value->tav;
  930. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  931. i64 offset = matrix_row_major_index_to_offset(type, index);
  932. GB_ASSERT(values[offset] == nullptr);
  933. values[offset] = val;
  934. }
  935. }
  936. for (i64 i = 0; i < total_count; i++) {
  937. if (values[i] == nullptr) {
  938. values[i] = LLVMConstNull(lb_type(m, elem_type));
  939. }
  940. }
  941. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  942. return res;
  943. } else {
  944. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  945. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  946. for_array(i, cl->elems) {
  947. TypeAndValue tav = cl->elems[i]->tav;
  948. GB_ASSERT(tav.mode != Addressing_Invalid);
  949. i64 offset = matrix_row_major_index_to_offset(type, i);
  950. values[offset] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  951. }
  952. for (isize i = 0; i < total_count; i++) {
  953. if (values[i] == nullptr) {
  954. values[i] = LLVMConstNull(lb_type(m, elem_type));
  955. }
  956. }
  957. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  958. return res;
  959. }
  960. } else {
  961. return lb_const_nil(m, original_type);
  962. }
  963. break;
  964. case ExactValue_Procedure:
  965. {
  966. Ast *expr = value.value_procedure;
  967. GB_ASSERT(expr != nullptr);
  968. if (expr->kind == Ast_ProcLit) {
  969. return lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  970. }
  971. }
  972. break;
  973. case ExactValue_Typeid:
  974. return lb_typeid(m, value.value_typeid);
  975. }
  976. return lb_const_nil(m, original_type);
  977. }