llvm_backend_const.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. gb_internal bool lb_is_const(lbValue value) {
  2. LLVMValueRef v = value.value;
  3. if (is_type_untyped_nil(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. gb_internal 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. gb_internal 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. gb_internal 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. gb_internal 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. gb_internal 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. gb_internal 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. // GB_PANIC("%s -> %s", LLVMPrintValueToString(val), LLVMPrintTypeToString(dst));
  92. // NOTE(bill): It's not possible to do a bit cast on a struct, why was this code even here in the first place?
  93. // It seems mostly to exist to get around the "anonymous -> named" struct assignments
  94. // return LLVMConstBitCast(val, dst);
  95. return val;
  96. default:
  97. GB_PANIC("Unhandled const cast %s to %s", LLVMPrintTypeToString(src), LLVMPrintTypeToString(dst));
  98. }
  99. return val;
  100. }
  101. gb_internal lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) {
  102. GB_ASSERT(is_type_internally_pointer_like(value.type));
  103. GB_ASSERT(is_type_internally_pointer_like(t));
  104. GB_ASSERT(lb_is_const(value));
  105. lbValue res = {};
  106. res.value = LLVMConstPointerCast(value.value, lb_type(m, t));
  107. res.type = t;
  108. return res;
  109. }
  110. gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) {
  111. LLVMTypeRef struct_type = lb_type(m, t);
  112. GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind);
  113. unsigned value_count = cast(unsigned)value_count_;
  114. unsigned elem_count = LLVMCountStructElementTypes(struct_type);
  115. if (elem_count == value_count) {
  116. return llvm_const_named_struct_internal(struct_type, values, value_count_);
  117. }
  118. Type *bt = base_type(t);
  119. GB_ASSERT(bt->kind == Type_Struct);
  120. GB_ASSERT(value_count_ == bt->Struct.fields.count);
  121. auto field_remapping = lb_get_struct_remapping(m, t);
  122. unsigned values_with_padding_count = LLVMCountStructElementTypes(struct_type);
  123. LLVMValueRef *values_with_padding = gb_alloc_array(permanent_allocator(), LLVMValueRef, values_with_padding_count);
  124. for (unsigned i = 0; i < value_count; i++) {
  125. values_with_padding[field_remapping[i]] = values[i];
  126. }
  127. for (unsigned i = 0; i < values_with_padding_count; i++) {
  128. if (values_with_padding[i] == nullptr) {
  129. values_with_padding[i] = LLVMConstNull(LLVMStructGetTypeAtIndex(struct_type, i));
  130. }
  131. }
  132. return llvm_const_named_struct_internal(struct_type, values_with_padding, values_with_padding_count);
  133. }
  134. gb_internal LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_) {
  135. unsigned value_count = cast(unsigned)value_count_;
  136. unsigned elem_count = LLVMCountStructElementTypes(t);
  137. GB_ASSERT_MSG(value_count == elem_count, "%s %u %u", LLVMPrintTypeToString(t), value_count, elem_count);
  138. for (unsigned i = 0; i < elem_count; i++) {
  139. LLVMTypeRef elem_type = LLVMStructGetTypeAtIndex(t, i);
  140. values[i] = llvm_const_cast(values[i], elem_type);
  141. }
  142. return LLVMConstNamedStruct(t, values, value_count);
  143. }
  144. gb_internal LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
  145. unsigned value_count = cast(unsigned)value_count_;
  146. for (unsigned i = 0; i < value_count; i++) {
  147. values[i] = llvm_const_cast(values[i], elem_type);
  148. }
  149. return LLVMConstArray(elem_type, values, value_count);
  150. }
  151. gb_internal LLVMValueRef llvm_const_slice_internal(lbModule *m, LLVMValueRef data, LLVMValueRef len) {
  152. if (build_context.metrics.ptr_size < build_context.metrics.int_size) {
  153. GB_ASSERT(build_context.metrics.ptr_size == 4);
  154. GB_ASSERT(build_context.metrics.int_size == 8);
  155. LLVMValueRef vals[3] = {
  156. data,
  157. LLVMConstNull(lb_type(m, t_u32)),
  158. len,
  159. };
  160. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  161. } else {
  162. LLVMValueRef vals[2] = {
  163. data,
  164. len,
  165. };
  166. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  167. }
  168. }
  169. gb_internal LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) {
  170. GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type));
  171. GB_ASSERT(are_types_identical(len.type, t_int));
  172. return llvm_const_slice_internal(m, data.value, len.value);
  173. }
  174. gb_internal lbValue lb_const_nil(lbModule *m, Type *type) {
  175. LLVMValueRef v = LLVMConstNull(lb_type(m, type));
  176. return lbValue{v, type};
  177. }
  178. gb_internal lbValue lb_const_undef(lbModule *m, Type *type) {
  179. LLVMValueRef v = LLVMGetUndef(lb_type(m, type));
  180. return lbValue{v, type};
  181. }
  182. gb_internal lbValue lb_const_int(lbModule *m, Type *type, u64 value) {
  183. lbValue res = {};
  184. res.value = LLVMConstInt(lb_type(m, type), cast(unsigned long long)value, !is_type_unsigned(type));
  185. res.type = type;
  186. return res;
  187. }
  188. gb_internal lbValue lb_const_string(lbModule *m, String const &value) {
  189. return lb_const_value(m, t_string, exact_value_string(value));
  190. }
  191. gb_internal lbValue lb_const_bool(lbModule *m, Type *type, bool value) {
  192. lbValue res = {};
  193. res.value = LLVMConstInt(lb_type(m, type), value, false);
  194. res.type = type;
  195. return res;
  196. }
  197. gb_internal LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) {
  198. GB_ASSERT(type_size_of(type) == 2);
  199. u16 u = f32_to_f16(f);
  200. if (is_type_different_to_arch_endianness(type)) {
  201. u = gb_endian_swap16(u);
  202. }
  203. LLVMValueRef i = LLVMConstInt(LLVMInt16TypeInContext(m->ctx), u, false);
  204. return LLVMConstBitCast(i, lb_type(m, type));
  205. }
  206. gb_internal LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) {
  207. GB_ASSERT(type_size_of(type) == 4);
  208. u32 u = bit_cast<u32>(f);
  209. if (is_type_different_to_arch_endianness(type)) {
  210. u = gb_endian_swap32(u);
  211. }
  212. LLVMValueRef i = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), u, false);
  213. return LLVMConstBitCast(i, lb_type(m, type));
  214. }
  215. gb_internal bool lb_is_expr_untyped_const(Ast *expr) {
  216. auto const &tv = type_and_value_of_expr(expr);
  217. if (is_type_untyped(tv.type)) {
  218. return tv.value.kind != ExactValue_Invalid;
  219. }
  220. return false;
  221. }
  222. gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
  223. GB_ASSERT(is_type_typed(t));
  224. auto const &tv = type_and_value_of_expr(expr);
  225. return lb_const_value(m, t, tv.value);
  226. }
  227. gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  228. lbModule *m = p->module;
  229. LLVMValueRef fields[4] = {};
  230. fields[0]/*file*/ = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id)).value;
  231. fields[1]/*line*/ = lb_const_int(m, t_i32, pos.line).value;
  232. fields[2]/*column*/ = lb_const_int(m, t_i32, pos.column).value;
  233. fields[3]/*procedure*/ = lb_find_or_add_entity_string(p->module, procedure).value;
  234. lbValue res = {};
  235. res.value = llvm_const_named_struct(m, t_source_code_location, fields, gb_count_of(fields));
  236. res.type = t_source_code_location;
  237. return res;
  238. }
  239. gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) {
  240. String proc_name = {};
  241. if (p->entity) {
  242. proc_name = p->entity->token.string;
  243. }
  244. TokenPos pos = {};
  245. if (node) {
  246. pos = ast_token(node).pos;
  247. }
  248. return lb_emit_source_code_location_const(p, proc_name, pos);
  249. }
  250. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  251. lbValue loc = lb_emit_source_code_location_const(p, procedure, pos);
  252. lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
  253. lb_make_global_private_const(addr);
  254. return addr.addr;
  255. }
  256. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, Ast *node) {
  257. lbValue loc = lb_emit_source_code_location_const(p, node);
  258. lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr);
  259. lb_make_global_private_const(addr);
  260. return addr.addr;
  261. }
  262. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  263. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, procedure, pos));
  264. }
  265. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) {
  266. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, node));
  267. }
  268. gb_internal LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) {
  269. bool is_local = allow_local && m->curr_procedure != nullptr;
  270. bool is_const = true;
  271. if (is_local) {
  272. for (isize i = 0; i < count; i++) {
  273. GB_ASSERT(values[i] != nullptr);
  274. if (!LLVMIsConstant(values[i])) {
  275. is_const = false;
  276. break;
  277. }
  278. }
  279. }
  280. if (!is_const) {
  281. LLVMTypeRef llvm_elem_type = lb_type(m, elem_type);
  282. lbProcedure *p = m->curr_procedure;
  283. GB_ASSERT(p != nullptr);
  284. lbAddr v = lb_add_local_generated(p, type, false);
  285. lbValue ptr = lb_addr_get_ptr(p, v);
  286. for (isize i = 0; i < count; i++) {
  287. lbValue elem = lb_emit_array_epi(p, ptr, i);
  288. if (is_type_proc(elem_type)) {
  289. values[i] = LLVMConstPointerCast(values[i], llvm_elem_type);
  290. }
  291. LLVMBuildStore(p->builder, values[i], elem.value);
  292. }
  293. return lb_addr_load(p, v).value;
  294. }
  295. return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count);
  296. }
  297. gb_internal LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
  298. if (big_int_is_zero(a)) {
  299. return LLVMConstNull(lb_type(m, original_type));
  300. }
  301. size_t sz = cast(size_t)type_size_of(original_type);
  302. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will be fine :P
  303. u8 *rop = cast(u8 *)rop64;
  304. size_t max_count = 0;
  305. size_t written = 0;
  306. size_t size = 1;
  307. size_t nails = 0;
  308. mp_endian endian = MP_LITTLE_ENDIAN;
  309. max_count = mp_pack_count(a, nails, size);
  310. if (sz < max_count) {
  311. debug_print_big_int(a);
  312. gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
  313. }
  314. GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
  315. GB_ASSERT(gb_size_of(rop64) >= sz);
  316. mp_err err = mp_pack(rop, sz, &written,
  317. MP_LSB_FIRST,
  318. size, endian, nails,
  319. a);
  320. GB_ASSERT(err == MP_OKAY);
  321. if (!is_type_endian_little(original_type)) {
  322. for (size_t i = 0; i < sz/2; i++) {
  323. u8 tmp = rop[i];
  324. rop[i] = rop[sz-1-i];
  325. rop[sz-1-i] = tmp;
  326. }
  327. }
  328. LLVMValueRef value = LLVMConstIntOfArbitraryPrecision(lb_type(m, original_type), cast(unsigned)((sz+7)/8), cast(u64 *)rop);
  329. if (big_int_is_neg(a)) {
  330. value = LLVMConstNeg(value);
  331. }
  332. return value;
  333. }
  334. gb_internal bool lb_is_nested_possibly_constant(Type *ft, Selection const &sel, Ast *elem) {
  335. GB_ASSERT(!sel.indirect);
  336. for (i32 index : sel.index) {
  337. Type *bt = base_type(ft);
  338. switch (bt->kind) {
  339. case Type_Struct:
  340. if (bt->Struct.is_raw_union) {
  341. return false;
  342. }
  343. ft = bt->Struct.fields[index]->type;
  344. break;
  345. case Type_Array:
  346. ft = bt->Array.elem;
  347. break;
  348. default:
  349. return false;
  350. }
  351. }
  352. if (is_type_raw_union(ft) || is_type_typeid(ft)) {
  353. return false;
  354. }
  355. return lb_is_elem_const(elem, ft);
  356. }
  357. gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local) {
  358. LLVMContextRef ctx = m->ctx;
  359. type = default_type(type);
  360. Type *original_type = type;
  361. lbValue res = {};
  362. res.type = original_type;
  363. type = core_type(type);
  364. value = convert_exact_value_for_type(value, type);
  365. if (value.kind == ExactValue_Typeid) {
  366. return lb_typeid(m, value.value_typeid);
  367. }
  368. if (value.kind == ExactValue_Invalid) {
  369. return lb_const_nil(m, type);
  370. }
  371. if (value.kind == ExactValue_Procedure) {
  372. lbValue res = {};
  373. Ast *expr = unparen_expr(value.value_procedure);
  374. if (expr->kind == Ast_ProcLit) {
  375. res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  376. } else {
  377. Entity *e = entity_from_expr(expr);
  378. res = lb_find_procedure_value_from_entity(m, e);
  379. }
  380. GB_ASSERT(res.value != nullptr);
  381. GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
  382. res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
  383. return res;
  384. }
  385. bool is_local = allow_local && m->curr_procedure != nullptr;
  386. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  387. if (is_type_slice(type)) {
  388. if (value.kind == ExactValue_String) {
  389. GB_ASSERT(is_type_slice(type));
  390. res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value;
  391. return res;
  392. } else {
  393. ast_node(cl, CompoundLit, value.value_compound);
  394. isize count = cl->elems.count;
  395. if (count == 0) {
  396. return lb_const_nil(m, type);
  397. }
  398. count = gb_max(cast(isize)cl->max_count, count);
  399. Type *elem = base_type(type)->Slice.elem;
  400. Type *t = alloc_type_array(elem, count);
  401. lbValue backing_array = lb_const_value(m, t, value, allow_local);
  402. LLVMValueRef array_data = nullptr;
  403. if (is_local) {
  404. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  405. // its backing data on the stack
  406. lbProcedure *p = m->curr_procedure;
  407. LLVMTypeRef llvm_type = lb_type(m, t);
  408. array_data = llvm_alloca(p, llvm_type, 16);
  409. LLVMBuildStore(p->builder, backing_array.value, array_data);
  410. {
  411. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  412. LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
  413. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  414. lbAddr slice = lb_add_local_generated(p, type, false);
  415. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
  416. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  417. return lb_addr_load(p, slice);
  418. }
  419. } else {
  420. isize max_len = 7+8+1;
  421. char *str = gb_alloc_array(permanent_allocator(), char, max_len);
  422. u32 id = m->gen->global_array_index.fetch_add(1);
  423. isize len = gb_snprintf(str, max_len, "csba$%x", id);
  424. String name = make_string(cast(u8 *)str, len-1);
  425. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  426. array_data = LLVMAddGlobal(m->mod, lb_type(m, t), str);
  427. LLVMSetInitializer(array_data, backing_array.value);
  428. lbValue g = {};
  429. g.value = array_data;
  430. g.type = t;
  431. lb_add_entity(m, e, g);
  432. lb_add_member(m, name, g);
  433. {
  434. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  435. LLVMValueRef ptr = LLVMConstInBoundsGEP2(lb_type(m, t), array_data, indices, 2);
  436. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  437. LLVMValueRef values[2] = {ptr, len};
  438. res.value = llvm_const_named_struct(m, original_type, values, 2);
  439. return res;
  440. }
  441. }
  442. }
  443. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  444. if (is_type_rune_array(type) && value.kind == ExactValue_String) {
  445. i64 count = type->Array.count;
  446. Type *elem = type->Array.elem;
  447. LLVMTypeRef et = lb_type(m, elem);
  448. Rune rune;
  449. isize offset = 0;
  450. isize width = 1;
  451. String s = value.value_string;
  452. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  453. for (i64 i = 0; i < count && offset < s.len; i++) {
  454. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  455. offset += width;
  456. elems[i] = LLVMConstInt(et, rune, true);
  457. }
  458. GB_ASSERT(offset == s.len);
  459. res.value = llvm_const_array(et, elems, cast(unsigned)count);
  460. return res;
  461. }
  462. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  463. Type *core_elem = core_array_type(type);
  464. return lb_const_value(m, core_elem, value, allow_local);
  465. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  466. GB_ASSERT(type->Array.count == value.value_string.len);
  467. LLVMValueRef data = LLVMConstStringInContext(ctx,
  468. cast(char const *)value.value_string.text,
  469. cast(unsigned)value.value_string.len,
  470. true /*DontNullTerminate*/);
  471. res.value = data;
  472. return res;
  473. } else if (is_type_array(type) &&
  474. value.kind != ExactValue_Invalid &&
  475. value.kind != ExactValue_String &&
  476. value.kind != ExactValue_Compound) {
  477. i64 count = type->Array.count;
  478. Type *elem = type->Array.elem;
  479. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  480. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  481. for (i64 i = 0; i < count; i++) {
  482. elems[i] = single_elem.value;
  483. }
  484. res.value = llvm_const_array(lb_type(m, elem), elems, cast(unsigned)count);
  485. return res;
  486. } else if (is_type_matrix(type) &&
  487. value.kind != ExactValue_Invalid &&
  488. value.kind != ExactValue_Compound) {
  489. i64 row = type->Matrix.row_count;
  490. i64 column = type->Matrix.column_count;
  491. GB_ASSERT(row == column);
  492. Type *elem = type->Matrix.elem;
  493. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  494. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  495. i64 total_elem_count = matrix_type_total_internal_elems(type);
  496. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  497. for (i64 i = 0; i < row; i++) {
  498. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  499. }
  500. for (i64 i = 0; i < total_elem_count; i++) {
  501. if (elems[i] == nullptr) {
  502. elems[i] = LLVMConstNull(lb_type(m, elem));
  503. }
  504. }
  505. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  506. return res;
  507. } else if (is_type_simd_vector(type) &&
  508. value.kind != ExactValue_Invalid &&
  509. value.kind != ExactValue_Compound) {
  510. i64 count = type->SimdVector.count;
  511. Type *elem = type->SimdVector.elem;
  512. lbValue single_elem = lb_const_value(m, elem, value, allow_local);
  513. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  514. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  515. for (i64 i = 0; i < count; i++) {
  516. elems[i] = single_elem.value;
  517. }
  518. res.value = LLVMConstVector(elems, cast(unsigned)count);
  519. return res;
  520. }
  521. switch (value.kind) {
  522. case ExactValue_Invalid:
  523. res.value = LLVMConstNull(lb_type(m, original_type));
  524. return res;
  525. case ExactValue_Bool:
  526. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  527. return res;
  528. case ExactValue_String:
  529. {
  530. LLVMValueRef ptr = lb_find_or_add_entity_string_ptr(m, value.value_string);
  531. lbValue res = {};
  532. res.type = default_type(original_type);
  533. if (is_type_cstring(res.type)) {
  534. res.value = ptr;
  535. } else {
  536. if (value.value_string.len == 0) {
  537. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  538. }
  539. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string.len, true);
  540. LLVMValueRef values[2] = {ptr, str_len};
  541. GB_ASSERT(is_type_string(original_type));
  542. res.value = llvm_const_named_struct(m, original_type, values, 2);
  543. }
  544. return res;
  545. }
  546. case ExactValue_Integer:
  547. if (is_type_pointer(type) || is_type_multi_pointer(type)) {
  548. LLVMTypeRef t = lb_type(m, original_type);
  549. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  550. res.value = LLVMConstIntToPtr(i, t);
  551. } else {
  552. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  553. }
  554. return res;
  555. case ExactValue_Float:
  556. if (is_type_different_to_arch_endianness(type)) {
  557. u64 u = bit_cast<u64>(value.value_float);
  558. u = gb_endian_swap64(u);
  559. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  560. } else {
  561. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  562. }
  563. return res;
  564. case ExactValue_Complex:
  565. {
  566. LLVMValueRef values[2] = {};
  567. switch (8*type_size_of(type)) {
  568. case 32:
  569. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  570. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  571. break;
  572. case 64:
  573. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  574. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  575. break;
  576. case 128:
  577. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  578. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  579. break;
  580. }
  581. res.value = llvm_const_named_struct(m, original_type, values, 2);
  582. return res;
  583. }
  584. break;
  585. case ExactValue_Quaternion:
  586. {
  587. LLVMValueRef values[4] = {};
  588. switch (8*type_size_of(type)) {
  589. case 64:
  590. // @QuaternionLayout
  591. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  592. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  593. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  594. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  595. break;
  596. case 128:
  597. // @QuaternionLayout
  598. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  599. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  600. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  601. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  602. break;
  603. case 256:
  604. // @QuaternionLayout
  605. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  606. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  607. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  608. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  609. break;
  610. }
  611. res.value = llvm_const_named_struct(m, original_type, values, 4);
  612. return res;
  613. }
  614. break;
  615. case ExactValue_Pointer:
  616. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  617. return res;
  618. case ExactValue_Compound:
  619. if (is_type_slice(type)) {
  620. return lb_const_value(m, type, value, allow_local);
  621. } else if (is_type_array(type)) {
  622. ast_node(cl, CompoundLit, value.value_compound);
  623. Type *elem_type = type->Array.elem;
  624. isize elem_count = cl->elems.count;
  625. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  626. return lb_const_nil(m, original_type);
  627. }
  628. if (cl->elems[0]->kind == Ast_FieldValue) {
  629. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  630. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  631. isize value_index = 0;
  632. for (i64 i = 0; i < type->Array.count; 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->Array.count, values, allow_local);
  677. return res;
  678. } else {
  679. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  680. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.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->Array.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->Array.count, values, allow_local);
  690. return res;
  691. }
  692. } else if (is_type_enumerated_array(type)) {
  693. ast_node(cl, CompoundLit, value.value_compound);
  694. Type *elem_type = type->EnumeratedArray.elem;
  695. isize elem_count = cl->elems.count;
  696. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  697. return lb_const_nil(m, original_type);
  698. }
  699. if (cl->elems[0]->kind == Ast_FieldValue) {
  700. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  701. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  702. isize value_index = 0;
  703. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  704. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  705. for (i64 i = total_lo; i <= total_hi; 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 = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  750. return res;
  751. } else {
  752. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  753. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  754. for (isize i = 0; i < elem_count; i++) {
  755. TypeAndValue tav = cl->elems[i]->tav;
  756. GB_ASSERT(tav.mode != Addressing_Invalid);
  757. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  758. }
  759. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  760. values[i] = LLVMConstNull(lb_type(m, elem_type));
  761. }
  762. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
  763. return res;
  764. }
  765. } else if (is_type_simd_vector(type)) {
  766. ast_node(cl, CompoundLit, value.value_compound);
  767. Type *elem_type = type->SimdVector.elem;
  768. isize elem_count = cl->elems.count;
  769. if (elem_count == 0) {
  770. return lb_const_nil(m, original_type);
  771. }
  772. GB_ASSERT(elem_type_can_be_constant(elem_type));
  773. isize total_elem_count = cast(isize)type->SimdVector.count;
  774. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  775. if (cl->elems[0]->kind == Ast_FieldValue) {
  776. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  777. isize value_index = 0;
  778. for (i64 i = 0; i < total_elem_count; i++) {
  779. bool found = false;
  780. for (isize j = 0; j < elem_count; j++) {
  781. Ast *elem = cl->elems[j];
  782. ast_node(fv, FieldValue, elem);
  783. if (is_ast_range(fv->field)) {
  784. ast_node(ie, BinaryExpr, fv->field);
  785. TypeAndValue lo_tav = ie->left->tav;
  786. TypeAndValue hi_tav = ie->right->tav;
  787. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  788. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  789. TokenKind op = ie->op.kind;
  790. i64 lo = exact_value_to_i64(lo_tav.value);
  791. i64 hi = exact_value_to_i64(hi_tav.value);
  792. if (op != Token_RangeHalf) {
  793. hi += 1;
  794. }
  795. if (lo == i) {
  796. TypeAndValue tav = fv->value->tav;
  797. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  798. for (i64 k = lo; k < hi; k++) {
  799. values[value_index++] = val;
  800. }
  801. found = true;
  802. i += (hi-lo-1);
  803. break;
  804. }
  805. } else {
  806. TypeAndValue index_tav = fv->field->tav;
  807. GB_ASSERT(index_tav.mode == Addressing_Constant);
  808. i64 index = exact_value_to_i64(index_tav.value);
  809. if (index == i) {
  810. TypeAndValue tav = fv->value->tav;
  811. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  812. values[value_index++] = val;
  813. found = true;
  814. break;
  815. }
  816. }
  817. }
  818. if (!found) {
  819. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  820. }
  821. }
  822. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  823. return res;
  824. } else {
  825. for (isize i = 0; i < elem_count; i++) {
  826. TypeAndValue tav = cl->elems[i]->tav;
  827. GB_ASSERT(tav.mode != Addressing_Invalid);
  828. values[i] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  829. }
  830. LLVMTypeRef et = lb_type(m, elem_type);
  831. for (isize i = elem_count; i < total_elem_count; i++) {
  832. values[i] = LLVMConstNull(et);
  833. }
  834. for (isize i = 0; i < total_elem_count; i++) {
  835. values[i] = llvm_const_cast(values[i], et);
  836. }
  837. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  838. return res;
  839. }
  840. } else if (is_type_struct(type)) {
  841. ast_node(cl, CompoundLit, value.value_compound);
  842. if (cl->elems.count == 0) {
  843. return lb_const_nil(m, original_type);
  844. }
  845. if (is_type_raw_union(type)) {
  846. return lb_const_nil(m, original_type);
  847. }
  848. LLVMTypeRef struct_type = lb_type(m, original_type);
  849. auto field_remapping = lb_get_struct_remapping(m, type);
  850. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  851. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  852. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  853. if (cl->elems.count > 0) {
  854. if (cl->elems[0]->kind == Ast_FieldValue) {
  855. isize elem_count = cl->elems.count;
  856. for (isize i = 0; i < elem_count; i++) {
  857. ast_node(fv, FieldValue, cl->elems[i]);
  858. String name = fv->field->Ident.token.string;
  859. TypeAndValue tav = fv->value->tav;
  860. GB_ASSERT(tav.mode != Addressing_Invalid);
  861. Selection sel = lookup_field(type, name, false);
  862. GB_ASSERT(!sel.indirect);
  863. Entity *f = type->Struct.fields[sel.index[0]];
  864. i32 index = field_remapping[f->Variable.field_index];
  865. if (elem_type_can_be_constant(f->type)) {
  866. if (sel.index.count == 1) {
  867. values[index] = lb_const_value(m, f->type, tav.value, allow_local).value;
  868. visited[index] = true;
  869. } else {
  870. if (!visited[index]) {
  871. values[index] = lb_const_value(m, f->type, {}, false).value;
  872. visited[index] = true;
  873. }
  874. unsigned idx_list_len = cast(unsigned)sel.index.count-1;
  875. unsigned *idx_list = gb_alloc_array(temporary_allocator(), unsigned, idx_list_len);
  876. if (lb_is_nested_possibly_constant(type, sel, fv->value)) {
  877. bool is_constant = true;
  878. Type *cv_type = f->type;
  879. for (isize j = 1; j < sel.index.count; j++) {
  880. i32 index = sel.index[j];
  881. Type *cvt = base_type(cv_type);
  882. if (cvt->kind == Type_Struct) {
  883. if (cvt->Struct.is_raw_union) {
  884. // sanity check which should have been caught by `lb_is_nested_possibly_constant`
  885. is_constant = false;
  886. break;
  887. }
  888. cv_type = cvt->Struct.fields[index]->type;
  889. if (is_type_struct(cvt)) {
  890. auto cv_field_remapping = lb_get_struct_remapping(m, cvt);
  891. unsigned remapped_index = cast(unsigned)cv_field_remapping[index];
  892. idx_list[j-1] = remapped_index;
  893. } else {
  894. idx_list[j-1] = cast(unsigned)index;
  895. }
  896. } else if (cvt->kind == Type_Array) {
  897. cv_type = cvt->Array.elem;
  898. idx_list[j-1] = cast(unsigned)index;
  899. } else {
  900. GB_PANIC("UNKNOWN TYPE: %s", type_to_string(cv_type));
  901. }
  902. }
  903. if (is_constant) {
  904. LLVMValueRef elem_value = lb_const_value(m, tav.type, tav.value, allow_local).value;
  905. GB_ASSERT(LLVMIsConstant(elem_value));
  906. values[index] = LLVMConstInsertValue(values[index], elem_value, idx_list, idx_list_len);
  907. }
  908. }
  909. }
  910. }
  911. }
  912. } else {
  913. for_array(i, cl->elems) {
  914. Entity *f = type->Struct.fields[i];
  915. TypeAndValue tav = cl->elems[i]->tav;
  916. ExactValue val = {};
  917. if (tav.mode != Addressing_Invalid) {
  918. val = tav.value;
  919. }
  920. i32 index = field_remapping[f->Variable.field_index];
  921. if (elem_type_can_be_constant(f->type)) {
  922. values[index] = lb_const_value(m, f->type, val, allow_local).value;
  923. visited[index] = true;
  924. }
  925. }
  926. }
  927. }
  928. for (isize i = 0; i < value_count; i++) {
  929. if (!visited[i]) {
  930. GB_ASSERT(values[i] == nullptr);
  931. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  932. values[i] = LLVMConstNull(type);
  933. }
  934. }
  935. bool is_constant = true;
  936. for (isize i = 0; i < value_count; i++) {
  937. LLVMValueRef val = values[i];
  938. if (!LLVMIsConstant(val)) {
  939. GB_ASSERT(is_local);
  940. GB_ASSERT(LLVMGetInstructionOpcode(val) == LLVMLoad);
  941. is_constant = false;
  942. }
  943. }
  944. if (is_constant) {
  945. res.value = llvm_const_named_struct_internal(struct_type, values, cast(unsigned)value_count);
  946. return res;
  947. } else {
  948. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  949. LLVMValueRef *old_values = values;
  950. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  951. for (isize i = 0; i < value_count; i++) {
  952. LLVMValueRef old_value = old_values[i];
  953. if (LLVMIsConstant(old_value)) {
  954. new_values[i] = old_value;
  955. } else {
  956. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  957. }
  958. }
  959. LLVMValueRef constant_value = llvm_const_named_struct_internal(struct_type, new_values, cast(unsigned)value_count);
  960. GB_ASSERT(is_local);
  961. lbProcedure *p = m->curr_procedure;
  962. lbAddr v = lb_add_local_generated(p, res.type, true);
  963. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, v);
  964. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  965. for (isize i = 0; i < value_count; i++) {
  966. LLVMValueRef val = old_values[i];
  967. if (!LLVMIsConstant(val)) {
  968. LLVMValueRef dst = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), v.addr.value, cast(unsigned)i, "");
  969. LLVMBuildStore(p->builder, val, dst);
  970. }
  971. }
  972. return lb_addr_load(p, v);
  973. }
  974. } else if (is_type_bit_set(type)) {
  975. ast_node(cl, CompoundLit, value.value_compound);
  976. if (cl->elems.count == 0) {
  977. return lb_const_nil(m, original_type);
  978. }
  979. i64 sz = type_size_of(type);
  980. if (sz == 0) {
  981. return lb_const_nil(m, original_type);
  982. }
  983. BigInt bits = {};
  984. BigInt one = {};
  985. big_int_from_u64(&one, 1);
  986. for_array(i, cl->elems) {
  987. Ast *e = cl->elems[i];
  988. GB_ASSERT(e->kind != Ast_FieldValue);
  989. TypeAndValue tav = e->tav;
  990. if (tav.mode != Addressing_Constant) {
  991. continue;
  992. }
  993. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  994. i64 v = big_int_to_i64(&tav.value.value_integer);
  995. i64 lower = type->BitSet.lower;
  996. u64 index = cast(u64)(v-lower);
  997. BigInt bit = {};
  998. big_int_from_u64(&bit, index);
  999. big_int_shl(&bit, &one, &bit);
  1000. big_int_or(&bits, &bits, &bit);
  1001. }
  1002. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  1003. return res;
  1004. } else if (is_type_matrix(type)) {
  1005. ast_node(cl, CompoundLit, value.value_compound);
  1006. Type *elem_type = type->Matrix.elem;
  1007. isize elem_count = cl->elems.count;
  1008. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1009. return lb_const_nil(m, original_type);
  1010. }
  1011. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  1012. i64 total_count = matrix_type_total_internal_elems(type);
  1013. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1014. if (cl->elems[0]->kind == Ast_FieldValue) {
  1015. for_array(j, cl->elems) {
  1016. Ast *elem = cl->elems[j];
  1017. ast_node(fv, FieldValue, elem);
  1018. if (is_ast_range(fv->field)) {
  1019. ast_node(ie, BinaryExpr, fv->field);
  1020. TypeAndValue lo_tav = ie->left->tav;
  1021. TypeAndValue hi_tav = ie->right->tav;
  1022. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1023. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1024. TokenKind op = ie->op.kind;
  1025. i64 lo = exact_value_to_i64(lo_tav.value);
  1026. i64 hi = exact_value_to_i64(hi_tav.value);
  1027. if (op != Token_RangeHalf) {
  1028. hi += 1;
  1029. }
  1030. GB_ASSERT(0 <= lo && lo <= max_count);
  1031. GB_ASSERT(0 <= hi && hi <= max_count);
  1032. GB_ASSERT(lo <= hi);
  1033. TypeAndValue tav = fv->value->tav;
  1034. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  1035. for (i64 k = lo; k < hi; k++) {
  1036. i64 offset = matrix_row_major_index_to_offset(type, k);
  1037. GB_ASSERT(values[offset] == nullptr);
  1038. values[offset] = val;
  1039. }
  1040. } else {
  1041. TypeAndValue index_tav = fv->field->tav;
  1042. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1043. i64 index = exact_value_to_i64(index_tav.value);
  1044. GB_ASSERT(index < max_count);
  1045. TypeAndValue tav = fv->value->tav;
  1046. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
  1047. i64 offset = matrix_row_major_index_to_offset(type, index);
  1048. GB_ASSERT(values[offset] == nullptr);
  1049. values[offset] = val;
  1050. }
  1051. }
  1052. for (i64 i = 0; i < total_count; i++) {
  1053. if (values[i] == nullptr) {
  1054. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1055. }
  1056. }
  1057. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  1058. return res;
  1059. } else {
  1060. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  1061. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1062. for_array(i, cl->elems) {
  1063. TypeAndValue tav = cl->elems[i]->tav;
  1064. GB_ASSERT(tav.mode != Addressing_Invalid);
  1065. i64 offset = matrix_row_major_index_to_offset(type, i);
  1066. values[offset] = lb_const_value(m, elem_type, tav.value, allow_local).value;
  1067. }
  1068. for (isize i = 0; i < total_count; i++) {
  1069. if (values[i] == nullptr) {
  1070. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1071. }
  1072. }
  1073. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, allow_local);
  1074. return res;
  1075. }
  1076. } else {
  1077. return lb_const_nil(m, original_type);
  1078. }
  1079. break;
  1080. case ExactValue_Procedure:
  1081. GB_PANIC("handled earlier");
  1082. break;
  1083. case ExactValue_Typeid:
  1084. return lb_typeid(m, value.value_typeid);
  1085. }
  1086. return lb_const_nil(m, original_type);
  1087. }