llvm_backend_const.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. // TODO remove use of LLVMGetElementType
  17. #if 0
  18. if (LLVMGetValueKind(value.value) == LLVMGlobalVariableValueKind) {
  19. LLVMTypeRef t = LLVMGetElementType(LLVMTypeOf(value.value));
  20. if (!lb_is_type_kind(t, LLVMPointerTypeKind)) {
  21. return false;
  22. }
  23. LLVMTypeRef elem = LLVMGetElementType(t);
  24. return lb_is_type_kind(elem, LLVMFunctionTypeKind);
  25. }
  26. #endif
  27. return false;
  28. }
  29. bool lb_is_elem_const(Ast *elem, Type *elem_type) {
  30. if (!elem_type_can_be_constant(elem_type)) {
  31. return false;
  32. }
  33. if (elem->kind == Ast_FieldValue) {
  34. elem = elem->FieldValue.value;
  35. }
  36. TypeAndValue tav = type_and_value_of_expr(elem);
  37. GB_ASSERT_MSG(tav.mode != Addressing_Invalid, "%s %s", expr_to_string(elem), type_to_string(tav.type));
  38. return tav.value.kind != ExactValue_Invalid;
  39. }
  40. bool lb_is_const_nil(lbValue value) {
  41. LLVMValueRef v = value.value;
  42. if (LLVMIsConstant(v)) {
  43. if (LLVMIsAConstantAggregateZero(v)) {
  44. return true;
  45. } else if (LLVMIsAConstantPointerNull(v)) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. bool lb_is_expr_constant_zero(Ast *expr) {
  52. GB_ASSERT(expr != nullptr);
  53. auto v = exact_value_to_integer(expr->tav.value);
  54. if (v.kind == ExactValue_Integer) {
  55. return big_int_cmp_zero(&v.value_integer) == 0;
  56. }
  57. return false;
  58. }
  59. String lb_get_const_string(lbModule *m, lbValue value) {
  60. GB_ASSERT(lb_is_const(value));
  61. GB_ASSERT(LLVMIsConstant(value.value));
  62. Type *t = base_type(value.type);
  63. GB_ASSERT(are_types_identical(t, t_string));
  64. unsigned ptr_indices[1] = {0};
  65. unsigned len_indices[1] = {1};
  66. LLVMValueRef underlying_ptr = LLVMConstExtractValue(value.value, ptr_indices, gb_count_of(ptr_indices));
  67. LLVMValueRef underlying_len = LLVMConstExtractValue(value.value, len_indices, gb_count_of(len_indices));
  68. GB_ASSERT(LLVMGetConstOpcode(underlying_ptr) == LLVMGetElementPtr);
  69. underlying_ptr = LLVMGetOperand(underlying_ptr, 0);
  70. GB_ASSERT(LLVMIsAGlobalVariable(underlying_ptr));
  71. underlying_ptr = LLVMGetInitializer(underlying_ptr);
  72. size_t length = 0;
  73. char const *text = LLVMGetAsString(underlying_ptr, &length);
  74. isize real_length = cast(isize)LLVMConstIntGetSExtValue(underlying_len);
  75. return make_string(cast(u8 const *)text, real_length);
  76. }
  77. LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) {
  78. LLVMTypeRef src = LLVMTypeOf(val);
  79. if (src == dst) {
  80. return val;
  81. }
  82. if (LLVMIsNull(val)) {
  83. return LLVMConstNull(dst);
  84. }
  85. GB_ASSERT_MSG(lb_sizeof(dst) == lb_sizeof(src), "%s vs %s", LLVMPrintTypeToString(dst), LLVMPrintTypeToString(src));
  86. LLVMTypeKind kind = LLVMGetTypeKind(dst);
  87. switch (kind) {
  88. case LLVMPointerTypeKind:
  89. return LLVMConstPointerCast(val, dst);
  90. case LLVMStructTypeKind:
  91. return LLVMConstBitCast(val, dst);
  92. default:
  93. GB_PANIC("Unhandled const cast %s to %s", LLVMPrintTypeToString(src), LLVMPrintTypeToString(dst));
  94. }
  95. return val;
  96. }
  97. lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) {
  98. GB_ASSERT(is_type_internally_pointer_like(value.type));
  99. GB_ASSERT(is_type_internally_pointer_like(t));
  100. GB_ASSERT(lb_is_const(value));
  101. lbValue res = {};
  102. res.value = LLVMConstPointerCast(value.value, lb_type(m, t));
  103. res.type = t;
  104. return res;
  105. }
  106. LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) {
  107. LLVMTypeRef struct_type = lb_type(m, t);
  108. GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind);
  109. unsigned value_count = cast(unsigned)value_count_;
  110. unsigned elem_count = LLVMCountStructElementTypes(struct_type);
  111. if (elem_count == value_count) {
  112. return llvm_const_named_struct_internal(struct_type, values, value_count_);
  113. }
  114. Type *bt = base_type(t);
  115. GB_ASSERT(bt->kind == Type_Struct);
  116. GB_ASSERT(value_count_ == bt->Struct.fields.count);
  117. auto field_remapping = lb_get_struct_remapping(m, t);
  118. unsigned values_with_padding_count = LLVMCountStructElementTypes(struct_type);
  119. LLVMValueRef *values_with_padding = gb_alloc_array(permanent_allocator(), LLVMValueRef, values_with_padding_count);
  120. for (unsigned i = 0; i < value_count; i++) {
  121. values_with_padding[field_remapping[i]] = values[i];
  122. }
  123. for (unsigned i = 0; i < values_with_padding_count; i++) {
  124. if (values_with_padding[i] == nullptr) {
  125. values_with_padding[i] = LLVMConstNull(LLVMStructGetTypeAtIndex(struct_type, i));
  126. }
  127. }
  128. return llvm_const_named_struct_internal(struct_type, values_with_padding, values_with_padding_count);
  129. }
  130. LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_) {
  131. unsigned value_count = cast(unsigned)value_count_;
  132. unsigned elem_count = LLVMCountStructElementTypes(t);
  133. GB_ASSERT_MSG(value_count == elem_count, "%s %u %u", LLVMPrintTypeToString(t), value_count, elem_count);
  134. for (unsigned i = 0; i < elem_count; i++) {
  135. LLVMTypeRef elem_type = LLVMStructGetTypeAtIndex(t, i);
  136. values[i] = llvm_const_cast(values[i], elem_type);
  137. }
  138. return LLVMConstNamedStruct(t, values, value_count);
  139. }
  140. LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
  141. unsigned value_count = cast(unsigned)value_count_;
  142. for (unsigned i = 0; i < value_count; i++) {
  143. values[i] = llvm_const_cast(values[i], elem_type);
  144. }
  145. return LLVMConstArray(elem_type, values, value_count);
  146. }
  147. LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) {
  148. GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type));
  149. GB_ASSERT(are_types_identical(len.type, t_int));
  150. LLVMValueRef vals[2] = {
  151. data.value,
  152. len.value,
  153. };
  154. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  155. }
  156. lbValue lb_const_nil(lbModule *m, Type *type) {
  157. LLVMValueRef v = LLVMConstNull(lb_type(m, type));
  158. return lbValue{v, type};
  159. }
  160. lbValue lb_const_undef(lbModule *m, Type *type) {
  161. LLVMValueRef v = LLVMGetUndef(lb_type(m, type));
  162. return lbValue{v, type};
  163. }
  164. lbValue lb_const_int(lbModule *m, Type *type, u64 value) {
  165. lbValue res = {};
  166. res.value = LLVMConstInt(lb_type(m, type), cast(unsigned long long)value, !is_type_unsigned(type));
  167. res.type = type;
  168. return res;
  169. }
  170. lbValue lb_const_string(lbModule *m, String const &value) {
  171. return lb_const_value(m, t_string, exact_value_string(value));
  172. }
  173. lbValue lb_const_bool(lbModule *m, Type *type, bool value) {
  174. lbValue res = {};
  175. res.value = LLVMConstInt(lb_type(m, type), value, false);
  176. res.type = type;
  177. return res;
  178. }
  179. LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) {
  180. GB_ASSERT(type_size_of(type) == 2);
  181. u16 u = f32_to_f16(f);
  182. if (is_type_different_to_arch_endianness(type)) {
  183. u = gb_endian_swap16(u);
  184. }
  185. LLVMValueRef i = LLVMConstInt(LLVMInt16TypeInContext(m->ctx), u, false);
  186. return LLVMConstBitCast(i, lb_type(m, type));
  187. }
  188. LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) {
  189. GB_ASSERT(type_size_of(type) == 4);
  190. u32 u = bit_cast<u32>(f);
  191. if (is_type_different_to_arch_endianness(type)) {
  192. u = gb_endian_swap32(u);
  193. }
  194. LLVMValueRef i = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), u, false);
  195. return LLVMConstBitCast(i, lb_type(m, type));
  196. }
  197. bool lb_is_expr_untyped_const(Ast *expr) {
  198. auto const &tv = type_and_value_of_expr(expr);
  199. if (is_type_untyped(tv.type)) {
  200. return tv.value.kind != ExactValue_Invalid;
  201. }
  202. return false;
  203. }
  204. lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
  205. GB_ASSERT(is_type_typed(t));
  206. auto const &tv = type_and_value_of_expr(expr);
  207. return lb_const_value(m, t, tv.value);
  208. }
  209. lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  210. lbModule *m = p->module;
  211. LLVMValueRef fields[4] = {};
  212. fields[0]/*file*/ = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id)).value;
  213. fields[1]/*line*/ = lb_const_int(m, t_i32, pos.line).value;
  214. fields[2]/*column*/ = lb_const_int(m, t_i32, pos.column).value;
  215. fields[3]/*procedure*/ = lb_find_or_add_entity_string(p->module, procedure).value;
  216. lbValue res = {};
  217. res.value = llvm_const_named_struct(m, t_source_code_location, fields, gb_count_of(fields));
  218. res.type = t_source_code_location;
  219. return res;
  220. }
  221. lbValue lb_emit_source_code_location(lbProcedure *p, Ast *node) {
  222. String proc_name = {};
  223. if (p->entity) {
  224. proc_name = p->entity->token.string;
  225. }
  226. TokenPos pos = {};
  227. if (node) {
  228. pos = ast_token(node).pos;
  229. }
  230. return lb_emit_source_code_location(p, proc_name, pos);
  231. }
  232. LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) {
  233. bool is_local = allow_local && m->curr_procedure != nullptr;
  234. bool is_const = true;
  235. if (is_local) {
  236. for (isize i = 0; i < count; i++) {
  237. GB_ASSERT(values[i] != nullptr);
  238. if (!LLVMIsConstant(values[i])) {
  239. is_const = false;
  240. break;
  241. }
  242. }
  243. }
  244. if (!is_const) {
  245. lbProcedure *p = m->curr_procedure;
  246. GB_ASSERT(p != nullptr);
  247. lbAddr v = lb_add_local_generated(p, type, false);
  248. lbValue ptr = lb_addr_get_ptr(p, v);
  249. for (isize i = 0; i < count; i++) {
  250. lbValue elem = lb_emit_array_epi(p, ptr, i);
  251. LLVMBuildStore(p->builder, values[i], elem.value);
  252. }
  253. return lb_addr_load(p, v).value;
  254. }
  255. return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count);
  256. }
  257. LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
  258. if (big_int_is_zero(a)) {
  259. return LLVMConstNull(lb_type(m, original_type));
  260. }
  261. size_t sz = cast(size_t)type_size_of(original_type);
  262. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will be fine :P
  263. u8 *rop = cast(u8 *)rop64;
  264. size_t max_count = 0;
  265. size_t written = 0;
  266. size_t size = 1;
  267. size_t nails = 0;
  268. mp_endian endian = MP_LITTLE_ENDIAN;
  269. max_count = mp_pack_count(a, nails, size);
  270. if (sz < max_count) {
  271. debug_print_big_int(a);
  272. gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
  273. }
  274. GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
  275. GB_ASSERT(gb_size_of(rop64) >= sz);
  276. mp_err err = mp_pack(rop, sz, &written,
  277. MP_LSB_FIRST,
  278. size, endian, nails,
  279. a);
  280. GB_ASSERT(err == MP_OKAY);
  281. if (!is_type_endian_little(original_type)) {
  282. for (size_t i = 0; i < sz/2; i++) {
  283. u8 tmp = rop[i];
  284. rop[i] = rop[sz-1-i];
  285. rop[sz-1-i] = tmp;
  286. }
  287. }
  288. LLVMValueRef value = LLVMConstIntOfArbitraryPrecision(lb_type(m, original_type), cast(unsigned)(sz+7/8), cast(u64 *)rop);
  289. if (big_int_is_neg(a)) {
  290. value = LLVMConstNeg(value);
  291. }
  292. return value;
  293. }
  294. lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local) {
  295. LLVMContextRef ctx = m->ctx;
  296. type = default_type(type);
  297. Type *original_type = type;
  298. lbValue res = {};
  299. res.type = original_type;
  300. type = core_type(type);
  301. value = convert_exact_value_for_type(value, type);
  302. if (value.kind == ExactValue_Typeid) {
  303. return lb_typeid(m, value.value_typeid);
  304. }
  305. if (value.kind == ExactValue_Invalid) {
  306. return lb_const_nil(m, type);
  307. }
  308. if (value.kind == ExactValue_Procedure) {
  309. Ast *expr = unparen_expr(value.value_procedure);
  310. if (expr->kind == Ast_ProcLit) {
  311. return lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  312. }
  313. Entity *e = entity_from_expr(expr);
  314. return lb_find_procedure_value_from_entity(m, e);
  315. }
  316. bool is_local = allow_local && m->curr_procedure != nullptr;
  317. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  318. if (is_type_slice(type)) {
  319. if (value.kind == ExactValue_String) {
  320. GB_ASSERT(is_type_u8_slice(type));
  321. res.value = lb_find_or_add_entity_string_byte_slice(m, value.value_string).value;
  322. return res;
  323. } else {
  324. ast_node(cl, CompoundLit, value.value_compound);
  325. isize count = cl->elems.count;
  326. if (count == 0) {
  327. return lb_const_nil(m, type);
  328. }
  329. count = gb_max(cast(isize)cl->max_count, count);
  330. Type *elem = base_type(type)->Slice.elem;
  331. Type *t = alloc_type_array(elem, count);
  332. lbValue backing_array = lb_const_value(m, t, value, allow_local);
  333. LLVMValueRef array_data = nullptr;
  334. if (is_local) {
  335. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  336. // its backing data on the stack
  337. lbProcedure *p = m->curr_procedure;
  338. LLVMTypeRef llvm_type = lb_type(m, t);
  339. array_data = llvm_alloca(p, llvm_type, 16);
  340. LLVMBuildStore(p->builder, backing_array.value, array_data);
  341. {
  342. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  343. LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, 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 = LLVMConstInBoundsGEP2(lb_type(m, t), 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 = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), 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. BigInt bits = {};
  870. BigInt one = {};
  871. big_int_from_u64(&one, 1);
  872. for_array(i, cl->elems) {
  873. Ast *e = cl->elems[i];
  874. GB_ASSERT(e->kind != Ast_FieldValue);
  875. TypeAndValue tav = e->tav;
  876. if (tav.mode != Addressing_Constant) {
  877. continue;
  878. }
  879. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  880. i64 v = big_int_to_i64(&tav.value.value_integer);
  881. i64 lower = type->BitSet.lower;
  882. u64 index = cast(u64)(v-lower);
  883. BigInt bit = {};
  884. big_int_from_u64(&bit, index);
  885. big_int_shl(&bit, &one, &bit);
  886. big_int_or(&bits, &bits, &bit);
  887. }
  888. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  889. return res;
  890. } else if (is_type_matrix(type)) {
  891. ast_node(cl, CompoundLit, value.value_compound);
  892. Type *elem_type = type->Matrix.elem;
  893. isize elem_count = cl->elems.count;
  894. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  895. return lb_const_nil(m, original_type);
  896. }
  897. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  898. i64 total_count = matrix_type_total_internal_elems(type);
  899. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  900. if (cl->elems[0]->kind == Ast_FieldValue) {
  901. for_array(j, cl->elems) {
  902. Ast *elem = cl->elems[j];
  903. ast_node(fv, FieldValue, elem);
  904. if (is_ast_range(fv->field)) {
  905. ast_node(ie, BinaryExpr, fv->field);
  906. TypeAndValue lo_tav = ie->left->tav;
  907. TypeAndValue hi_tav = ie->right->tav;
  908. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  909. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  910. TokenKind op = ie->op.kind;
  911. i64 lo = exact_value_to_i64(lo_tav.value);
  912. i64 hi = exact_value_to_i64(hi_tav.value);
  913. if (op != Token_RangeHalf) {
  914. hi += 1;
  915. }
  916. GB_ASSERT(0 <= lo && lo <= max_count);
  917. GB_ASSERT(0 <= hi && hi <= max_count);
  918. GB_ASSERT(lo <= hi);
  919. TypeAndValue tav = fv->value->tav;
  920. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  921. for (i64 k = lo; k < hi; k++) {
  922. i64 offset = matrix_row_major_index_to_offset(type, k);
  923. GB_ASSERT(values[offset] == nullptr);
  924. values[offset] = val;
  925. }
  926. } else {
  927. TypeAndValue index_tav = fv->field->tav;
  928. GB_ASSERT(index_tav.mode == Addressing_Constant);
  929. i64 index = exact_value_to_i64(index_tav.value);
  930. GB_ASSERT(index < max_count);
  931. TypeAndValue tav = fv->value->tav;
  932. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  933. i64 offset = matrix_row_major_index_to_offset(type, index);
  934. GB_ASSERT(values[offset] == nullptr);
  935. values[offset] = val;
  936. }
  937. }
  938. for (i64 i = 0; i < total_count; i++) {
  939. if (values[i] == nullptr) {
  940. values[i] = LLVMConstNull(lb_type(m, elem_type));
  941. }
  942. }
  943. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  944. return res;
  945. } else {
  946. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  947. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  948. for_array(i, cl->elems) {
  949. TypeAndValue tav = cl->elems[i]->tav;
  950. GB_ASSERT(tav.mode != Addressing_Invalid);
  951. i64 offset = matrix_row_major_index_to_offset(type, i);
  952. values[offset] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  953. }
  954. for (isize i = 0; i < total_count; i++) {
  955. if (values[i] == nullptr) {
  956. values[i] = LLVMConstNull(lb_type(m, elem_type));
  957. }
  958. }
  959. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  960. return res;
  961. }
  962. } else {
  963. return lb_const_nil(m, original_type);
  964. }
  965. break;
  966. case ExactValue_Procedure:
  967. {
  968. Ast *expr = value.value_procedure;
  969. GB_ASSERT(expr != nullptr);
  970. if (expr->kind == Ast_ProcLit) {
  971. return lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  972. }
  973. }
  974. break;
  975. case ExactValue_Typeid:
  976. return lb_typeid(m, value.value_typeid);
  977. }
  978. return lb_const_nil(m, original_type);
  979. }