llvm_backend_const.cpp 43 KB

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