llvm_backend_const.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
  336. LLVMTypeRef llvm_type = lb_type(m, t);
  337. array_data = LLVMBuildAlloca(p->builder, llvm_type, "");
  338. LLVMSetAlignment(array_data, 16); // TODO(bill): Make this configurable
  339. LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
  340. LLVMBuildStore(p->builder, backing_array.value, array_data);
  341. {
  342. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  343. LLVMValueRef ptr = LLVMBuildInBoundsGEP(p->builder, array_data, indices, 2, "");
  344. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  345. lbAddr slice = lb_add_local_generated(p, type, false);
  346. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  347. return lb_addr_load(p, slice);
  348. }
  349. } else {
  350. isize max_len = 7+8+1;
  351. char *str = gb_alloc_array(permanent_allocator(), char, max_len);
  352. u32 id = m->gen->global_array_index.fetch_add(1);
  353. isize len = gb_snprintf(str, max_len, "csba$%x", id);
  354. String name = make_string(cast(u8 *)str, len-1);
  355. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  356. array_data = LLVMAddGlobal(m->mod, lb_type(m, t), str);
  357. LLVMSetInitializer(array_data, backing_array.value);
  358. lbValue g = {};
  359. g.value = array_data;
  360. g.type = t;
  361. lb_add_entity(m, e, g);
  362. lb_add_member(m, name, g);
  363. {
  364. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  365. LLVMValueRef ptr = LLVMConstInBoundsGEP(array_data, indices, 2);
  366. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  367. LLVMValueRef values[2] = {ptr, len};
  368. res.value = llvm_const_named_struct(m, original_type, values, 2);
  369. return res;
  370. }
  371. }
  372. }
  373. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  374. if (is_type_rune_array(type) && value.kind == ExactValue_String) {
  375. i64 count = type->Array.count;
  376. Type *elem = type->Array.elem;
  377. LLVMTypeRef et = lb_type(m, elem);
  378. Rune rune;
  379. isize offset = 0;
  380. isize width = 1;
  381. String s = value.value_string;
  382. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  383. for (i64 i = 0; i < count && offset < s.len; i++) {
  384. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  385. offset += width;
  386. elems[i] = LLVMConstInt(et, rune, true);
  387. }
  388. GB_ASSERT(offset == s.len);
  389. res.value = llvm_const_array(et, elems, cast(unsigned)count);
  390. return res;
  391. }
  392. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  393. Type *core_elem = core_array_type(type);
  394. return lb_const_value(m, core_elem, value, allow_local);
  395. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  396. GB_ASSERT(type->Array.count == value.value_string.len);
  397. LLVMValueRef data = LLVMConstStringInContext(ctx,
  398. cast(char const *)value.value_string.text,
  399. cast(unsigned)value.value_string.len,
  400. true /*DontNullTerminate*/);
  401. res.value = data;
  402. return res;
  403. } else if (is_type_array(type) &&
  404. value.kind != ExactValue_Invalid &&
  405. value.kind != ExactValue_String &&
  406. value.kind != ExactValue_Compound) {
  407. i64 count = type->Array.count;
  408. Type *elem = type->Array.elem;
  409. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  410. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  411. for (i64 i = 0; i < count; i++) {
  412. elems[i] = single_elem.value;
  413. }
  414. res.value = llvm_const_array(lb_type(m, elem), elems, cast(unsigned)count);
  415. return res;
  416. } else if (is_type_matrix(type) &&
  417. value.kind != ExactValue_Invalid &&
  418. value.kind != ExactValue_Compound) {
  419. i64 row = type->Matrix.row_count;
  420. i64 column = type->Matrix.column_count;
  421. GB_ASSERT(row == column);
  422. Type *elem = type->Matrix.elem;
  423. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  424. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  425. i64 total_elem_count = matrix_type_total_internal_elems(type);
  426. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  427. for (i64 i = 0; i < row; i++) {
  428. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  429. }
  430. for (i64 i = 0; i < total_elem_count; i++) {
  431. if (elems[i] == nullptr) {
  432. elems[i] = LLVMConstNull(lb_type(m, elem));
  433. }
  434. }
  435. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  436. return res;
  437. } else if (is_type_simd_vector(type) &&
  438. value.kind != ExactValue_Invalid &&
  439. value.kind != ExactValue_Compound) {
  440. i64 count = type->SimdVector.count;
  441. Type *elem = type->SimdVector.elem;
  442. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  443. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  444. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  445. for (i64 i = 0; i < count; i++) {
  446. elems[i] = single_elem.value;
  447. }
  448. res.value = LLVMConstVector(elems, cast(unsigned)count);
  449. return res;
  450. }
  451. switch (value.kind) {
  452. case ExactValue_Invalid:
  453. res.value = LLVMConstNull(lb_type(m, original_type));
  454. return res;
  455. case ExactValue_Bool:
  456. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  457. return res;
  458. case ExactValue_String:
  459. {
  460. LLVMValueRef ptr = lb_find_or_add_entity_string_ptr(m, value.value_string);
  461. lbValue res = {};
  462. res.type = default_type(original_type);
  463. if (is_type_cstring(res.type)) {
  464. res.value = ptr;
  465. } else {
  466. if (value.value_string.len == 0) {
  467. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  468. }
  469. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string.len, true);
  470. LLVMValueRef values[2] = {ptr, str_len};
  471. GB_ASSERT(is_type_string(original_type));
  472. res.value = llvm_const_named_struct(m, original_type, values, 2);
  473. }
  474. return res;
  475. }
  476. case ExactValue_Integer:
  477. if (is_type_pointer(type) || is_type_multi_pointer(type)) {
  478. LLVMTypeRef t = lb_type(m, original_type);
  479. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  480. res.value = LLVMConstIntToPtr(i, t);
  481. } else {
  482. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  483. }
  484. return res;
  485. case ExactValue_Float:
  486. if (is_type_different_to_arch_endianness(type)) {
  487. u64 u = bit_cast<u64>(value.value_float);
  488. u = gb_endian_swap64(u);
  489. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  490. } else {
  491. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  492. }
  493. return res;
  494. case ExactValue_Complex:
  495. {
  496. LLVMValueRef values[2] = {};
  497. switch (8*type_size_of(type)) {
  498. case 32:
  499. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  500. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  501. break;
  502. case 64:
  503. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  504. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  505. break;
  506. case 128:
  507. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  508. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  509. break;
  510. }
  511. res.value = llvm_const_named_struct(m, original_type, values, 2);
  512. return res;
  513. }
  514. break;
  515. case ExactValue_Quaternion:
  516. {
  517. LLVMValueRef values[4] = {};
  518. switch (8*type_size_of(type)) {
  519. case 64:
  520. // @QuaternionLayout
  521. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  522. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  523. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  524. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  525. break;
  526. case 128:
  527. // @QuaternionLayout
  528. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  529. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  530. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  531. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  532. break;
  533. case 256:
  534. // @QuaternionLayout
  535. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  536. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  537. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  538. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  539. break;
  540. }
  541. res.value = llvm_const_named_struct(m, original_type, values, 4);
  542. return res;
  543. }
  544. break;
  545. case ExactValue_Pointer:
  546. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  547. return res;
  548. case ExactValue_Compound:
  549. if (is_type_slice(type)) {
  550. return lb_const_value(m, type, value, allow_local);
  551. } else if (is_type_array(type)) {
  552. ast_node(cl, CompoundLit, value.value_compound);
  553. Type *elem_type = type->Array.elem;
  554. isize elem_count = cl->elems.count;
  555. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  556. return lb_const_nil(m, original_type);
  557. }
  558. if (cl->elems[0]->kind == Ast_FieldValue) {
  559. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  560. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  561. isize value_index = 0;
  562. for (i64 i = 0; i < type->Array.count; i++) {
  563. bool found = false;
  564. for (isize j = 0; j < elem_count; j++) {
  565. Ast *elem = cl->elems[j];
  566. ast_node(fv, FieldValue, elem);
  567. if (is_ast_range(fv->field)) {
  568. ast_node(ie, BinaryExpr, fv->field);
  569. TypeAndValue lo_tav = ie->left->tav;
  570. TypeAndValue hi_tav = ie->right->tav;
  571. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  572. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  573. TokenKind op = ie->op.kind;
  574. i64 lo = exact_value_to_i64(lo_tav.value);
  575. i64 hi = exact_value_to_i64(hi_tav.value);
  576. if (op != Token_RangeHalf) {
  577. hi += 1;
  578. }
  579. if (lo == i) {
  580. TypeAndValue tav = fv->value->tav;
  581. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  582. for (i64 k = lo; k < hi; k++) {
  583. values[value_index++] = val;
  584. }
  585. found = true;
  586. i += (hi-lo-1);
  587. break;
  588. }
  589. } else {
  590. TypeAndValue index_tav = fv->field->tav;
  591. GB_ASSERT(index_tav.mode == Addressing_Constant);
  592. i64 index = exact_value_to_i64(index_tav.value);
  593. if (index == i) {
  594. TypeAndValue tav = fv->value->tav;
  595. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  596. values[value_index++] = val;
  597. found = true;
  598. break;
  599. }
  600. }
  601. }
  602. if (!found) {
  603. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  604. }
  605. }
  606. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
  607. return res;
  608. } else {
  609. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  610. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  611. for (isize i = 0; i < elem_count; i++) {
  612. TypeAndValue tav = cl->elems[i]->tav;
  613. GB_ASSERT(tav.mode != Addressing_Invalid);
  614. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  615. }
  616. for (isize i = elem_count; i < type->Array.count; i++) {
  617. values[i] = LLVMConstNull(lb_type(m, elem_type));
  618. }
  619. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
  620. return res;
  621. }
  622. } else if (is_type_enumerated_array(type)) {
  623. ast_node(cl, CompoundLit, value.value_compound);
  624. Type *elem_type = type->EnumeratedArray.elem;
  625. isize elem_count = cl->elems.count;
  626. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  627. return lb_const_nil(m, original_type);
  628. }
  629. if (cl->elems[0]->kind == Ast_FieldValue) {
  630. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  631. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  632. isize value_index = 0;
  633. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  634. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  635. for (i64 i = total_lo; i <= total_hi; i++) {
  636. bool found = false;
  637. for (isize j = 0; j < elem_count; j++) {
  638. Ast *elem = cl->elems[j];
  639. ast_node(fv, FieldValue, elem);
  640. if (is_ast_range(fv->field)) {
  641. ast_node(ie, BinaryExpr, fv->field);
  642. TypeAndValue lo_tav = ie->left->tav;
  643. TypeAndValue hi_tav = ie->right->tav;
  644. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  645. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  646. TokenKind op = ie->op.kind;
  647. i64 lo = exact_value_to_i64(lo_tav.value);
  648. i64 hi = exact_value_to_i64(hi_tav.value);
  649. if (op != Token_RangeHalf) {
  650. hi += 1;
  651. }
  652. if (lo == i) {
  653. TypeAndValue tav = fv->value->tav;
  654. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  655. for (i64 k = lo; k < hi; k++) {
  656. values[value_index++] = val;
  657. }
  658. found = true;
  659. i += (hi-lo-1);
  660. break;
  661. }
  662. } else {
  663. TypeAndValue index_tav = fv->field->tav;
  664. GB_ASSERT(index_tav.mode == Addressing_Constant);
  665. i64 index = exact_value_to_i64(index_tav.value);
  666. if (index == i) {
  667. TypeAndValue tav = fv->value->tav;
  668. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  669. values[value_index++] = val;
  670. found = true;
  671. break;
  672. }
  673. }
  674. }
  675. if (!found) {
  676. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  677. }
  678. }
  679. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  680. return res;
  681. } else {
  682. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  683. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  684. for (isize i = 0; i < elem_count; i++) {
  685. TypeAndValue tav = cl->elems[i]->tav;
  686. GB_ASSERT(tav.mode != Addressing_Invalid);
  687. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  688. }
  689. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  690. values[i] = LLVMConstNull(lb_type(m, elem_type));
  691. }
  692. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  693. return res;
  694. }
  695. } else if (is_type_simd_vector(type)) {
  696. ast_node(cl, CompoundLit, value.value_compound);
  697. Type *elem_type = type->SimdVector.elem;
  698. isize elem_count = cl->elems.count;
  699. if (elem_count == 0) {
  700. return lb_const_nil(m, original_type);
  701. }
  702. GB_ASSERT(elem_type_can_be_constant(elem_type));
  703. isize total_elem_count = cast(isize)type->SimdVector.count;
  704. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  705. if (cl->elems[0]->kind == Ast_FieldValue) {
  706. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  707. isize value_index = 0;
  708. for (i64 i = 0; i < total_elem_count; i++) {
  709. bool found = false;
  710. for (isize j = 0; j < elem_count; j++) {
  711. Ast *elem = cl->elems[j];
  712. ast_node(fv, FieldValue, elem);
  713. if (is_ast_range(fv->field)) {
  714. ast_node(ie, BinaryExpr, fv->field);
  715. TypeAndValue lo_tav = ie->left->tav;
  716. TypeAndValue hi_tav = ie->right->tav;
  717. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  718. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  719. TokenKind op = ie->op.kind;
  720. i64 lo = exact_value_to_i64(lo_tav.value);
  721. i64 hi = exact_value_to_i64(hi_tav.value);
  722. if (op != Token_RangeHalf) {
  723. hi += 1;
  724. }
  725. if (lo == i) {
  726. TypeAndValue tav = fv->value->tav;
  727. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  728. for (i64 k = lo; k < hi; k++) {
  729. values[value_index++] = val;
  730. }
  731. found = true;
  732. i += (hi-lo-1);
  733. break;
  734. }
  735. } else {
  736. TypeAndValue index_tav = fv->field->tav;
  737. GB_ASSERT(index_tav.mode == Addressing_Constant);
  738. i64 index = exact_value_to_i64(index_tav.value);
  739. if (index == i) {
  740. TypeAndValue tav = fv->value->tav;
  741. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  742. values[value_index++] = val;
  743. found = true;
  744. break;
  745. }
  746. }
  747. }
  748. if (!found) {
  749. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  750. }
  751. }
  752. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  753. return res;
  754. } else {
  755. for (isize i = 0; i < elem_count; i++) {
  756. TypeAndValue tav = cl->elems[i]->tav;
  757. GB_ASSERT(tav.mode != Addressing_Invalid);
  758. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  759. }
  760. LLVMTypeRef et = lb_type(m, elem_type);
  761. for (isize i = elem_count; i < total_elem_count; i++) {
  762. values[i] = LLVMConstNull(et);
  763. }
  764. for (isize i = 0; i < total_elem_count; i++) {
  765. values[i] = llvm_const_cast(values[i], et);
  766. }
  767. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  768. return res;
  769. }
  770. } else if (is_type_struct(type)) {
  771. ast_node(cl, CompoundLit, value.value_compound);
  772. if (cl->elems.count == 0) {
  773. return lb_const_nil(m, original_type);
  774. }
  775. if (is_type_raw_union(type)) {
  776. return lb_const_nil(m, original_type);
  777. }
  778. LLVMTypeRef struct_type = lb_type(m, original_type);
  779. auto field_remapping = lb_get_struct_remapping(m, type);
  780. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  781. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  782. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  783. if (cl->elems.count > 0) {
  784. if (cl->elems[0]->kind == Ast_FieldValue) {
  785. isize elem_count = cl->elems.count;
  786. for (isize i = 0; i < elem_count; i++) {
  787. ast_node(fv, FieldValue, cl->elems[i]);
  788. String name = fv->field->Ident.token.string;
  789. TypeAndValue tav = fv->value->tav;
  790. GB_ASSERT(tav.mode != Addressing_Invalid);
  791. Selection sel = lookup_field(type, name, false);
  792. Entity *f = type->Struct.fields[sel.index[0]];
  793. i32 index = field_remapping[f->Variable.field_index];
  794. if (elem_type_can_be_constant(f->type)) {
  795. values[index] = lb_const_value(m, f->type, tav.value, allow_local).value;
  796. visited[index] = true;
  797. }
  798. }
  799. } else {
  800. for_array(i, cl->elems) {
  801. Entity *f = type->Struct.fields[i];
  802. TypeAndValue tav = cl->elems[i]->tav;
  803. ExactValue val = {};
  804. if (tav.mode != Addressing_Invalid) {
  805. val = tav.value;
  806. }
  807. i32 index = field_remapping[f->Variable.field_index];
  808. if (elem_type_can_be_constant(f->type)) {
  809. values[index] = lb_const_value(m, f->type, val, allow_local).value;
  810. visited[index] = true;
  811. }
  812. }
  813. }
  814. }
  815. for (isize i = 0; i < value_count; i++) {
  816. if (!visited[i]) {
  817. GB_ASSERT(values[i] == nullptr);
  818. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  819. values[i] = LLVMConstNull(type);
  820. }
  821. }
  822. bool is_constant = true;
  823. for (isize i = 0; i < value_count; i++) {
  824. LLVMValueRef val = values[i];
  825. if (!LLVMIsConstant(val)) {
  826. GB_ASSERT(is_local);
  827. GB_ASSERT(LLVMGetInstructionOpcode(val) == LLVMLoad);
  828. is_constant = false;
  829. }
  830. }
  831. if (is_constant) {
  832. res.value = llvm_const_named_struct_internal(struct_type, values, cast(unsigned)value_count);
  833. return res;
  834. } else {
  835. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  836. LLVMValueRef *old_values = values;
  837. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  838. for (isize i = 0; i < value_count; i++) {
  839. LLVMValueRef old_value = old_values[i];
  840. if (LLVMIsConstant(old_value)) {
  841. new_values[i] = old_value;
  842. } else {
  843. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  844. }
  845. }
  846. LLVMValueRef constant_value = llvm_const_named_struct_internal(struct_type, new_values, cast(unsigned)value_count);
  847. GB_ASSERT(is_local);
  848. lbProcedure *p = m->curr_procedure;
  849. lbAddr v = lb_add_local_generated(p, res.type, true);
  850. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  851. for (isize i = 0; i < value_count; i++) {
  852. LLVMValueRef val = old_values[i];
  853. if (!LLVMIsConstant(val)) {
  854. LLVMValueRef dst = LLVMBuildStructGEP(p->builder, v.addr.value, cast(unsigned)i, "");
  855. LLVMBuildStore(p->builder, val, dst);
  856. }
  857. }
  858. return lb_addr_load(p, v);
  859. }
  860. } else if (is_type_bit_set(type)) {
  861. ast_node(cl, CompoundLit, value.value_compound);
  862. if (cl->elems.count == 0) {
  863. return lb_const_nil(m, original_type);
  864. }
  865. i64 sz = type_size_of(type);
  866. if (sz == 0) {
  867. return lb_const_nil(m, original_type);
  868. }
  869. u64 bits = 0;
  870. for_array(i, cl->elems) {
  871. Ast *e = cl->elems[i];
  872. GB_ASSERT(e->kind != Ast_FieldValue);
  873. TypeAndValue tav = e->tav;
  874. if (tav.mode != Addressing_Constant) {
  875. continue;
  876. }
  877. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  878. i64 v = big_int_to_i64(&tav.value.value_integer);
  879. i64 lower = type->BitSet.lower;
  880. bits |= 1ull<<cast(u64)(v-lower);
  881. }
  882. if (is_type_different_to_arch_endianness(type)) {
  883. i64 size = type_size_of(type);
  884. switch (size) {
  885. case 2: bits = cast(u64)gb_endian_swap16(cast(u16)bits); break;
  886. case 4: bits = cast(u64)gb_endian_swap32(cast(u32)bits); break;
  887. case 8: bits = cast(u64)gb_endian_swap64(cast(u64)bits); break;
  888. }
  889. }
  890. res.value = LLVMConstInt(lb_type(m, original_type), bits, false);
  891. return res;
  892. } else if (is_type_matrix(type)) {
  893. ast_node(cl, CompoundLit, value.value_compound);
  894. Type *elem_type = type->Matrix.elem;
  895. isize elem_count = cl->elems.count;
  896. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  897. return lb_const_nil(m, original_type);
  898. }
  899. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  900. i64 total_count = matrix_type_total_internal_elems(type);
  901. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  902. if (cl->elems[0]->kind == Ast_FieldValue) {
  903. for_array(j, cl->elems) {
  904. Ast *elem = cl->elems[j];
  905. ast_node(fv, FieldValue, elem);
  906. if (is_ast_range(fv->field)) {
  907. ast_node(ie, BinaryExpr, fv->field);
  908. TypeAndValue lo_tav = ie->left->tav;
  909. TypeAndValue hi_tav = ie->right->tav;
  910. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  911. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  912. TokenKind op = ie->op.kind;
  913. i64 lo = exact_value_to_i64(lo_tav.value);
  914. i64 hi = exact_value_to_i64(hi_tav.value);
  915. if (op != Token_RangeHalf) {
  916. hi += 1;
  917. }
  918. GB_ASSERT(0 <= lo && lo <= max_count);
  919. GB_ASSERT(0 <= hi && hi <= max_count);
  920. GB_ASSERT(lo <= hi);
  921. TypeAndValue tav = fv->value->tav;
  922. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  923. for (i64 k = lo; k < hi; k++) {
  924. i64 offset = matrix_row_major_index_to_offset(type, k);
  925. GB_ASSERT(values[offset] == nullptr);
  926. values[offset] = val;
  927. }
  928. } else {
  929. TypeAndValue index_tav = fv->field->tav;
  930. GB_ASSERT(index_tav.mode == Addressing_Constant);
  931. i64 index = exact_value_to_i64(index_tav.value);
  932. GB_ASSERT(index < max_count);
  933. TypeAndValue tav = fv->value->tav;
  934. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  935. i64 offset = matrix_row_major_index_to_offset(type, index);
  936. GB_ASSERT(values[offset] == nullptr);
  937. values[offset] = val;
  938. }
  939. }
  940. for (i64 i = 0; i < total_count; i++) {
  941. if (values[i] == nullptr) {
  942. values[i] = LLVMConstNull(lb_type(m, elem_type));
  943. }
  944. }
  945. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  946. return res;
  947. } else {
  948. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  949. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  950. for_array(i, cl->elems) {
  951. TypeAndValue tav = cl->elems[i]->tav;
  952. GB_ASSERT(tav.mode != Addressing_Invalid);
  953. i64 offset = matrix_row_major_index_to_offset(type, i);
  954. values[offset] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  955. }
  956. for (isize i = 0; i < total_count; i++) {
  957. if (values[i] == nullptr) {
  958. values[i] = LLVMConstNull(lb_type(m, elem_type));
  959. }
  960. }
  961. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  962. return res;
  963. }
  964. } else {
  965. return lb_const_nil(m, original_type);
  966. }
  967. break;
  968. case ExactValue_Procedure:
  969. {
  970. Ast *expr = value.value_procedure;
  971. GB_ASSERT(expr != nullptr);
  972. if (expr->kind == Ast_ProcLit) {
  973. return lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  974. }
  975. }
  976. break;
  977. case ExactValue_Typeid:
  978. return lb_typeid(m, value.value_typeid);
  979. }
  980. return lb_const_nil(m, original_type);
  981. }