llvm_backend_const.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 (v != nullptr && 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 = elem_count;
  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, false).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, false).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 String lb_source_code_location_gen_name(String const &procedure, TokenPos const &pos) {
  269. gbString s = gb_string_make(permanent_allocator(), "scl$[");
  270. s = gb_string_append_length(s, procedure.text, procedure.len);
  271. if (pos.offset != 0) {
  272. s = gb_string_append_fmt(s, "%d", pos.offset);
  273. } else {
  274. s = gb_string_append_fmt(s, "%d_%d", pos.line, pos.column);
  275. }
  276. s = gb_string_appendc(s, "]");
  277. return make_string(cast(u8 const *)s, gb_string_length(s));
  278. }
  279. gb_internal String lb_source_code_location_gen_name(lbProcedure *p, Ast *node) {
  280. String proc_name = {};
  281. if (p->entity) {
  282. proc_name = p->entity->token.string;
  283. }
  284. TokenPos pos = {};
  285. if (node) {
  286. pos = ast_token(node).pos;
  287. }
  288. return lb_source_code_location_gen_name(proc_name, pos);
  289. }
  290. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  291. lbValue loc = lb_emit_source_code_location_const(p, procedure, pos);
  292. lbAddr addr = lb_add_global_generated_with_name(p->module, loc.type, loc, lb_source_code_location_gen_name(procedure, pos));
  293. lb_make_global_private_const(addr);
  294. return addr.addr;
  295. }
  296. gb_internal lbValue lb_const_source_code_location_as_global_ptr(lbModule *m, String const &procedure, TokenPos const &pos) {
  297. lbValue loc = lb_const_source_code_location_const(m, procedure, pos);
  298. lbAddr addr = lb_add_global_generated_with_name(m, loc.type, loc, lb_source_code_location_gen_name(procedure, pos));
  299. lb_make_global_private_const(addr);
  300. return addr.addr;
  301. }
  302. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, Ast *node) {
  303. lbValue loc = lb_emit_source_code_location_const(p, node);
  304. lbAddr addr = lb_add_global_generated_with_name(p->module, loc.type, loc, lb_source_code_location_gen_name(p, node));
  305. lb_make_global_private_const(addr);
  306. return addr.addr;
  307. }
  308. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  309. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, procedure, pos));
  310. }
  311. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) {
  312. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, node));
  313. }
  314. gb_internal LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, lbConstContext cc) {
  315. if (cc.allow_local) {
  316. cc.is_rodata = false;
  317. }
  318. bool is_local = cc.allow_local && m->curr_procedure != nullptr;
  319. bool is_const = true;
  320. if (is_local) {
  321. for (isize i = 0; i < count; i++) {
  322. GB_ASSERT(values[i] != nullptr);
  323. if (!LLVMIsConstant(values[i])) {
  324. is_const = false;
  325. break;
  326. }
  327. }
  328. }
  329. if (!is_const) {
  330. LLVMTypeRef llvm_elem_type = lb_type(m, elem_type);
  331. lbProcedure *p = m->curr_procedure;
  332. GB_ASSERT(p != nullptr);
  333. lbAddr v = lb_add_local_generated(p, type, false);
  334. lbValue ptr = lb_addr_get_ptr(p, v);
  335. for (isize i = 0; i < count; i++) {
  336. lbValue elem = lb_emit_array_epi(p, ptr, i);
  337. if (is_type_proc(elem_type)) {
  338. values[i] = LLVMConstPointerCast(values[i], llvm_elem_type);
  339. }
  340. LLVMBuildStore(p->builder, values[i], elem.value);
  341. }
  342. return lb_addr_load(p, v).value;
  343. }
  344. return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count);
  345. }
  346. gb_internal LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
  347. if (big_int_is_zero(a)) {
  348. return LLVMConstNull(lb_type(m, original_type));
  349. }
  350. size_t sz = cast(size_t)type_size_of(original_type);
  351. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will be fine :P
  352. u8 *rop = cast(u8 *)rop64;
  353. size_t max_count = 0;
  354. size_t written = 0;
  355. size_t size = 1;
  356. size_t nails = 0;
  357. mp_endian endian = MP_LITTLE_ENDIAN;
  358. max_count = mp_pack_count(a, nails, size);
  359. if (sz < max_count) {
  360. debug_print_big_int(a);
  361. gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
  362. }
  363. GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
  364. GB_ASSERT(gb_size_of(rop64) >= sz);
  365. mp_err err = mp_pack(rop, sz, &written,
  366. MP_LSB_FIRST,
  367. size, endian, nails,
  368. a);
  369. GB_ASSERT(err == MP_OKAY);
  370. if (!is_type_endian_little(original_type)) {
  371. for (size_t i = 0; i < sz/2; i++) {
  372. u8 tmp = rop[i];
  373. rop[i] = rop[sz-1-i];
  374. rop[sz-1-i] = tmp;
  375. }
  376. }
  377. GB_ASSERT(!is_type_array(original_type));
  378. LLVMValueRef value = LLVMConstIntOfArbitraryPrecision(lb_type(m, original_type), cast(unsigned)((sz+7)/8), cast(u64 *)rop);
  379. if (big_int_is_neg(a)) {
  380. value = LLVMConstNeg(value);
  381. }
  382. return value;
  383. }
  384. gb_internal bool lb_is_nested_possibly_constant(Type *ft, Selection const &sel, Ast *elem) {
  385. GB_ASSERT(!sel.indirect);
  386. for (i32 index : sel.index) {
  387. Type *bt = base_type(ft);
  388. switch (bt->kind) {
  389. case Type_Struct:
  390. if (bt->Struct.is_raw_union) {
  391. return false;
  392. }
  393. ft = bt->Struct.fields[index]->type;
  394. break;
  395. case Type_Array:
  396. ft = bt->Array.elem;
  397. break;
  398. default:
  399. return false;
  400. }
  401. }
  402. if (is_type_raw_union(ft) || is_type_typeid(ft)) {
  403. return false;
  404. }
  405. return lb_is_elem_const(elem, ft);
  406. }
  407. gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lbConstContext cc) {
  408. if (cc.allow_local) {
  409. cc.is_rodata = false;
  410. }
  411. LLVMContextRef ctx = m->ctx;
  412. type = default_type(type);
  413. Type *original_type = type;
  414. lbValue res = {};
  415. res.type = original_type;
  416. type = core_type(type);
  417. value = convert_exact_value_for_type(value, type);
  418. if (value.kind == ExactValue_Typeid) {
  419. return lb_typeid(m, value.value_typeid);
  420. }
  421. if (value.kind == ExactValue_Invalid) {
  422. return lb_const_nil(m, original_type);
  423. }
  424. if (value.kind == ExactValue_Procedure) {
  425. lbValue res = {};
  426. Ast *expr = unparen_expr(value.value_procedure);
  427. GB_ASSERT(expr != nullptr);
  428. if (expr->kind == Ast_ProcLit) {
  429. res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  430. } else {
  431. Entity *e = entity_from_expr(expr);
  432. res = lb_find_procedure_value_from_entity(m, e);
  433. }
  434. if (res.value == nullptr) {
  435. // This is an unspecialized polymorphic procedure, return nil or dummy value
  436. return lb_const_nil(m, original_type);
  437. }
  438. GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
  439. if (LLVMGetIntrinsicID(res.value) == 0) {
  440. // NOTE(bill): do not cast intrinsics as they are not really procedures that can be casted
  441. res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
  442. }
  443. return res;
  444. }
  445. bool is_local = cc.allow_local && m->curr_procedure != nullptr;
  446. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  447. if (is_type_slice(type)) {
  448. if (value.kind == ExactValue_String) {
  449. GB_ASSERT(is_type_slice(type));
  450. res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value;
  451. return res;
  452. } else {
  453. ast_node(cl, CompoundLit, value.value_compound);
  454. isize count = cl->elems.count;
  455. if (count == 0) {
  456. return lb_const_nil(m, type);
  457. }
  458. count = gb_max(cast(isize)cl->max_count, count);
  459. Type *elem = base_type(type)->Slice.elem;
  460. Type *t = alloc_type_array(elem, count);
  461. lbValue backing_array = lb_const_value(m, t, value, cc);
  462. LLVMValueRef array_data = nullptr;
  463. if (is_local) {
  464. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  465. // its backing data on the stack
  466. lbProcedure *p = m->curr_procedure;
  467. LLVMTypeRef llvm_type = lb_type(m, t);
  468. array_data = llvm_alloca(p, llvm_type, 16);
  469. LLVMBuildStore(p->builder, backing_array.value, array_data);
  470. {
  471. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  472. LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
  473. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  474. lbAddr slice = lb_add_local_generated(p, original_type, false);
  475. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
  476. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  477. return lb_addr_load(p, slice);
  478. }
  479. } else {
  480. u32 id = m->global_array_index.fetch_add(1);
  481. gbString str = gb_string_make(temporary_allocator(), "csba$");
  482. str = gb_string_appendc(str, m->module_name);
  483. str = gb_string_append_fmt(str, "$%x", id);
  484. String name = make_string(cast(u8 const *)str, gb_string_length(str));
  485. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  486. array_data = LLVMAddGlobal(m->mod, lb_type(m, t), str);
  487. LLVMSetInitializer(array_data, backing_array.value);
  488. if (cc.link_section.len > 0) {
  489. LLVMSetSection(array_data, alloc_cstring(permanent_allocator(), cc.link_section));
  490. }
  491. if (cc.is_rodata) {
  492. LLVMSetGlobalConstant(array_data, true);
  493. }
  494. lbValue g = {};
  495. g.value = array_data;
  496. g.type = t;
  497. lb_add_entity(m, e, g);
  498. lb_add_member(m, name, g);
  499. {
  500. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  501. LLVMValueRef ptr = LLVMConstInBoundsGEP2(lb_type(m, t), array_data, indices, 2);
  502. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  503. LLVMValueRef values[2] = {ptr, len};
  504. res.value = llvm_const_named_struct(m, original_type, values, 2);
  505. return res;
  506. }
  507. }
  508. }
  509. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  510. if (is_type_rune_array(type)) {
  511. i64 count = type->Array.count;
  512. Type *elem = type->Array.elem;
  513. LLVMTypeRef et = lb_type(m, elem);
  514. Rune rune;
  515. isize offset = 0;
  516. isize width = 1;
  517. String s = value.value_string;
  518. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  519. for (i64 i = 0; i < count && offset < s.len; i++) {
  520. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  521. offset += width;
  522. elems[i] = LLVMConstInt(et, rune, true);
  523. }
  524. GB_ASSERT(offset == s.len);
  525. res.value = llvm_const_array(et, elems, cast(unsigned)count);
  526. return res;
  527. }
  528. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  529. Type *core_elem = core_array_type(type);
  530. return lb_const_value(m, core_elem, value, cc);
  531. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  532. GB_ASSERT(type->Array.count == value.value_string.len);
  533. LLVMValueRef data = LLVMConstStringInContext(ctx,
  534. cast(char const *)value.value_string.text,
  535. cast(unsigned)value.value_string.len,
  536. true /*DontNullTerminate*/);
  537. res.value = data;
  538. return res;
  539. } else if (is_type_array(type) &&
  540. value.kind != ExactValue_Invalid &&
  541. value.kind != ExactValue_String &&
  542. value.kind != ExactValue_Compound) {
  543. i64 count = type->Array.count;
  544. Type *elem = type->Array.elem;
  545. lbValue single_elem = lb_const_value(m, elem, value, cc);
  546. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  547. for (i64 i = 0; i < count; i++) {
  548. elems[i] = single_elem.value;
  549. }
  550. res.value = llvm_const_array(lb_type(m, elem), elems, cast(unsigned)count);
  551. return res;
  552. } else if (is_type_matrix(type) &&
  553. value.kind != ExactValue_Invalid &&
  554. value.kind != ExactValue_Compound) {
  555. i64 row = type->Matrix.row_count;
  556. i64 column = type->Matrix.column_count;
  557. GB_ASSERT(row == column);
  558. Type *elem = type->Matrix.elem;
  559. lbValue single_elem = lb_const_value(m, elem, value, cc);
  560. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  561. i64 total_elem_count = matrix_type_total_internal_elems(type);
  562. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  563. for (i64 i = 0; i < row; i++) {
  564. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  565. }
  566. for (i64 i = 0; i < total_elem_count; i++) {
  567. if (elems[i] == nullptr) {
  568. elems[i] = LLVMConstNull(lb_type(m, elem));
  569. }
  570. }
  571. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  572. return res;
  573. } else if (is_type_simd_vector(type) &&
  574. value.kind != ExactValue_Invalid &&
  575. value.kind != ExactValue_Compound) {
  576. i64 count = type->SimdVector.count;
  577. Type *elem = type->SimdVector.elem;
  578. lbValue single_elem = lb_const_value(m, elem, value, cc);
  579. single_elem.value = llvm_const_cast(single_elem.value, lb_type(m, elem));
  580. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  581. for (i64 i = 0; i < count; i++) {
  582. elems[i] = single_elem.value;
  583. }
  584. res.value = LLVMConstVector(elems, cast(unsigned)count);
  585. return res;
  586. }
  587. switch (value.kind) {
  588. case ExactValue_Invalid:
  589. res.value = LLVMConstNull(lb_type(m, original_type));
  590. return res;
  591. case ExactValue_Bool:
  592. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  593. return res;
  594. case ExactValue_String:
  595. {
  596. bool custom_link_section = cc.link_section.len > 0;
  597. LLVMValueRef ptr = lb_find_or_add_entity_string_ptr(m, value.value_string, custom_link_section);
  598. lbValue res = {};
  599. res.type = default_type(original_type);
  600. if (custom_link_section) {
  601. LLVMSetSection(ptr, alloc_cstring(permanent_allocator(), cc.link_section));
  602. }
  603. if (is_type_cstring(res.type)) {
  604. res.value = ptr;
  605. } else {
  606. if (value.value_string.len == 0) {
  607. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  608. }
  609. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string.len, true);
  610. GB_ASSERT(is_type_string(original_type));
  611. res.value = llvm_const_string_internal(m, original_type, ptr, str_len);
  612. }
  613. return res;
  614. }
  615. case ExactValue_Integer:
  616. if (is_type_pointer(type) || is_type_multi_pointer(type) || is_type_proc(type)) {
  617. LLVMTypeRef t = lb_type(m, original_type);
  618. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  619. res.value = LLVMConstIntToPtr(i, t);
  620. } else {
  621. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  622. }
  623. return res;
  624. case ExactValue_Float:
  625. if (is_type_different_to_arch_endianness(type)) {
  626. if (type->Basic.kind == Basic_f32le || type->Basic.kind == Basic_f32be) {
  627. f32 f = static_cast<float>(value.value_float);
  628. u32 u = bit_cast<u32>(f);
  629. u = gb_endian_swap32(u);
  630. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f32>(u));
  631. } else if (type->Basic.kind == Basic_f16le || type->Basic.kind == Basic_f16be) {
  632. f32 f = static_cast<float>(value.value_float);
  633. u16 u = f32_to_f16(f);
  634. u = gb_endian_swap16(u);
  635. res.value = LLVMConstReal(lb_type(m, original_type), f16_to_f32(u));
  636. } else {
  637. u64 u = bit_cast<u64>(value.value_float);
  638. u = gb_endian_swap64(u);
  639. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  640. }
  641. } else {
  642. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  643. }
  644. return res;
  645. case ExactValue_Complex:
  646. {
  647. LLVMValueRef values[2] = {};
  648. switch (8*type_size_of(type)) {
  649. case 32:
  650. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  651. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  652. break;
  653. case 64:
  654. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  655. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  656. break;
  657. case 128:
  658. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  659. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  660. break;
  661. }
  662. res.value = llvm_const_named_struct(m, original_type, values, 2);
  663. return res;
  664. }
  665. break;
  666. case ExactValue_Quaternion:
  667. {
  668. LLVMValueRef values[4] = {};
  669. switch (8*type_size_of(type)) {
  670. case 64:
  671. // @QuaternionLayout
  672. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  673. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  674. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  675. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  676. break;
  677. case 128:
  678. // @QuaternionLayout
  679. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  680. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  681. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  682. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  683. break;
  684. case 256:
  685. // @QuaternionLayout
  686. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  687. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  688. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  689. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  690. break;
  691. }
  692. res.value = llvm_const_named_struct(m, original_type, values, 4);
  693. return res;
  694. }
  695. break;
  696. case ExactValue_Pointer:
  697. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  698. return res;
  699. case ExactValue_Compound:
  700. if (is_type_slice(type)) {
  701. return lb_const_value(m, type, value, cc);
  702. } else if (is_type_array(type)) {
  703. ast_node(cl, CompoundLit, value.value_compound);
  704. Type *elem_type = type->Array.elem;
  705. isize elem_count = cl->elems.count;
  706. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  707. return lb_const_nil(m, original_type);
  708. }
  709. if (cl->elems[0]->kind == Ast_FieldValue) {
  710. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  711. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  712. isize value_index = 0;
  713. for (i64 i = 0; i < type->Array.count; i++) {
  714. bool found = false;
  715. for (isize j = 0; j < elem_count; j++) {
  716. Ast *elem = cl->elems[j];
  717. ast_node(fv, FieldValue, elem);
  718. if (is_ast_range(fv->field)) {
  719. ast_node(ie, BinaryExpr, fv->field);
  720. TypeAndValue lo_tav = ie->left->tav;
  721. TypeAndValue hi_tav = ie->right->tav;
  722. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  723. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  724. TokenKind op = ie->op.kind;
  725. i64 lo = exact_value_to_i64(lo_tav.value);
  726. i64 hi = exact_value_to_i64(hi_tav.value);
  727. if (op != Token_RangeHalf) {
  728. hi += 1;
  729. }
  730. if (lo == i) {
  731. TypeAndValue tav = fv->value->tav;
  732. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  733. for (i64 k = lo; k < hi; k++) {
  734. values[value_index++] = val;
  735. }
  736. found = true;
  737. i += (hi-lo-1);
  738. break;
  739. }
  740. } else {
  741. TypeAndValue index_tav = fv->field->tav;
  742. GB_ASSERT(index_tav.mode == Addressing_Constant);
  743. i64 index = exact_value_to_i64(index_tav.value);
  744. if (index == i) {
  745. TypeAndValue tav = fv->value->tav;
  746. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  747. values[value_index++] = val;
  748. found = true;
  749. break;
  750. }
  751. }
  752. }
  753. if (!found) {
  754. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  755. }
  756. }
  757. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  758. return res;
  759. } else {
  760. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  761. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  762. for (isize i = 0; i < elem_count; i++) {
  763. TypeAndValue tav = cl->elems[i]->tav;
  764. GB_ASSERT(tav.mode != Addressing_Invalid);
  765. values[i] = lb_const_value(m, elem_type, tav.value, cc).value;
  766. }
  767. for (isize i = elem_count; i < type->Array.count; i++) {
  768. values[i] = LLVMConstNull(lb_type(m, elem_type));
  769. }
  770. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  771. return res;
  772. }
  773. } else if (is_type_enumerated_array(type)) {
  774. ast_node(cl, CompoundLit, value.value_compound);
  775. Type *elem_type = type->EnumeratedArray.elem;
  776. isize elem_count = cl->elems.count;
  777. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  778. return lb_const_nil(m, original_type);
  779. }
  780. if (cl->elems[0]->kind == Ast_FieldValue) {
  781. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  782. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  783. isize value_index = 0;
  784. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  785. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  786. for (i64 i = total_lo; i <= total_hi; i++) {
  787. bool found = false;
  788. for (isize j = 0; j < elem_count; j++) {
  789. Ast *elem = cl->elems[j];
  790. ast_node(fv, FieldValue, elem);
  791. if (is_ast_range(fv->field)) {
  792. ast_node(ie, BinaryExpr, fv->field);
  793. TypeAndValue lo_tav = ie->left->tav;
  794. TypeAndValue hi_tav = ie->right->tav;
  795. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  796. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  797. TokenKind op = ie->op.kind;
  798. i64 lo = exact_value_to_i64(lo_tav.value);
  799. i64 hi = exact_value_to_i64(hi_tav.value);
  800. if (op != Token_RangeHalf) {
  801. hi += 1;
  802. }
  803. if (lo == i) {
  804. TypeAndValue tav = fv->value->tav;
  805. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  806. for (i64 k = lo; k < hi; k++) {
  807. values[value_index++] = val;
  808. }
  809. found = true;
  810. i += (hi-lo-1);
  811. break;
  812. }
  813. } else {
  814. TypeAndValue index_tav = fv->field->tav;
  815. GB_ASSERT(index_tav.mode == Addressing_Constant);
  816. i64 index = exact_value_to_i64(index_tav.value);
  817. if (index == i) {
  818. TypeAndValue tav = fv->value->tav;
  819. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  820. values[value_index++] = val;
  821. found = true;
  822. break;
  823. }
  824. }
  825. }
  826. if (!found) {
  827. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  828. }
  829. }
  830. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  831. return res;
  832. } else {
  833. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  834. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  835. for (isize i = 0; i < elem_count; i++) {
  836. TypeAndValue tav = cl->elems[i]->tav;
  837. GB_ASSERT(tav.mode != Addressing_Invalid);
  838. values[i] = lb_const_value(m, elem_type, tav.value, cc).value;
  839. }
  840. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  841. values[i] = LLVMConstNull(lb_type(m, elem_type));
  842. }
  843. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  844. return res;
  845. }
  846. } else if (is_type_simd_vector(type)) {
  847. ast_node(cl, CompoundLit, value.value_compound);
  848. Type *elem_type = type->SimdVector.elem;
  849. isize elem_count = cl->elems.count;
  850. if (elem_count == 0) {
  851. return lb_const_nil(m, original_type);
  852. }
  853. GB_ASSERT(elem_type_can_be_constant(elem_type));
  854. isize total_elem_count = cast(isize)type->SimdVector.count;
  855. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  856. if (cl->elems[0]->kind == Ast_FieldValue) {
  857. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  858. isize value_index = 0;
  859. for (i64 i = 0; i < total_elem_count; i++) {
  860. bool found = false;
  861. for (isize j = 0; j < elem_count; j++) {
  862. Ast *elem = cl->elems[j];
  863. ast_node(fv, FieldValue, elem);
  864. if (is_ast_range(fv->field)) {
  865. ast_node(ie, BinaryExpr, fv->field);
  866. TypeAndValue lo_tav = ie->left->tav;
  867. TypeAndValue hi_tav = ie->right->tav;
  868. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  869. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  870. TokenKind op = ie->op.kind;
  871. i64 lo = exact_value_to_i64(lo_tav.value);
  872. i64 hi = exact_value_to_i64(hi_tav.value);
  873. if (op != Token_RangeHalf) {
  874. hi += 1;
  875. }
  876. if (lo == i) {
  877. TypeAndValue tav = fv->value->tav;
  878. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  879. for (i64 k = lo; k < hi; k++) {
  880. values[value_index++] = val;
  881. }
  882. found = true;
  883. i += (hi-lo-1);
  884. break;
  885. }
  886. } else {
  887. TypeAndValue index_tav = fv->field->tav;
  888. GB_ASSERT(index_tav.mode == Addressing_Constant);
  889. i64 index = exact_value_to_i64(index_tav.value);
  890. if (index == i) {
  891. TypeAndValue tav = fv->value->tav;
  892. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  893. values[value_index++] = val;
  894. found = true;
  895. break;
  896. }
  897. }
  898. }
  899. if (!found) {
  900. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  901. }
  902. }
  903. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  904. return res;
  905. } else {
  906. for (isize i = 0; i < elem_count; i++) {
  907. TypeAndValue tav = cl->elems[i]->tav;
  908. GB_ASSERT(tav.mode != Addressing_Invalid);
  909. values[i] = lb_const_value(m, elem_type, tav.value, cc).value;
  910. }
  911. LLVMTypeRef et = lb_type(m, elem_type);
  912. for (isize i = elem_count; i < total_elem_count; i++) {
  913. values[i] = LLVMConstNull(et);
  914. }
  915. for (isize i = 0; i < total_elem_count; i++) {
  916. values[i] = llvm_const_cast(values[i], et);
  917. }
  918. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  919. return res;
  920. }
  921. } else if (is_type_struct(type)) {
  922. ast_node(cl, CompoundLit, value.value_compound);
  923. if (cl->elems.count == 0) {
  924. return lb_const_nil(m, original_type);
  925. }
  926. if (is_type_raw_union(type)) {
  927. return lb_const_nil(m, original_type);
  928. }
  929. LLVMTypeRef struct_type = lb_type(m, original_type);
  930. auto field_remapping = lb_get_struct_remapping(m, type);
  931. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  932. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  933. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  934. if (cl->elems[0]->kind == Ast_FieldValue) {
  935. isize elem_count = cl->elems.count;
  936. for (isize i = 0; i < elem_count; i++) {
  937. ast_node(fv, FieldValue, cl->elems[i]);
  938. String name = fv->field->Ident.token.string;
  939. TypeAndValue tav = fv->value->tav;
  940. GB_ASSERT(tav.mode != Addressing_Invalid);
  941. Selection sel = lookup_field(type, name, false);
  942. GB_ASSERT(!sel.indirect);
  943. Entity *f = type->Struct.fields[sel.index[0]];
  944. i32 index = field_remapping[f->Variable.field_index];
  945. if (elem_type_can_be_constant(f->type)) {
  946. if (sel.index.count == 1) {
  947. values[index] = lb_const_value(m, f->type, tav.value, cc).value;
  948. visited[index] = true;
  949. } else {
  950. if (!visited[index]) {
  951. auto new_cc = cc;
  952. new_cc.allow_local = false;
  953. values[index] = lb_const_value(m, f->type, {}, new_cc).value;
  954. visited[index] = true;
  955. }
  956. unsigned idx_list_len = cast(unsigned)sel.index.count-1;
  957. unsigned *idx_list = gb_alloc_array(temporary_allocator(), unsigned, idx_list_len);
  958. if (lb_is_nested_possibly_constant(type, sel, fv->value)) {
  959. bool is_constant = true;
  960. Type *cv_type = f->type;
  961. for (isize j = 1; j < sel.index.count; j++) {
  962. i32 index = sel.index[j];
  963. Type *cvt = base_type(cv_type);
  964. if (cvt->kind == Type_Struct) {
  965. if (cvt->Struct.is_raw_union) {
  966. // sanity check which should have been caught by `lb_is_nested_possibly_constant`
  967. is_constant = false;
  968. break;
  969. }
  970. cv_type = cvt->Struct.fields[index]->type;
  971. if (is_type_struct(cvt)) {
  972. auto cv_field_remapping = lb_get_struct_remapping(m, cvt);
  973. unsigned remapped_index = cast(unsigned)cv_field_remapping[index];
  974. idx_list[j-1] = remapped_index;
  975. } else {
  976. idx_list[j-1] = cast(unsigned)index;
  977. }
  978. } else if (cvt->kind == Type_Array) {
  979. cv_type = cvt->Array.elem;
  980. idx_list[j-1] = cast(unsigned)index;
  981. } else {
  982. GB_PANIC("UNKNOWN TYPE: %s", type_to_string(cv_type));
  983. }
  984. }
  985. if (is_constant) {
  986. LLVMValueRef elem_value = lb_const_value(m, tav.type, tav.value, cc).value;
  987. if (LLVMIsConstant(elem_value) && LLVMIsConstant(values[index])) {
  988. values[index] = llvm_const_insert_value(m, values[index], elem_value, idx_list, idx_list_len);
  989. } else if (is_local) {
  990. #if 1
  991. lbProcedure *p = m->curr_procedure;
  992. GB_ASSERT(p != nullptr);
  993. if (LLVMIsConstant(values[index])) {
  994. lbAddr addr = lb_add_local_generated(p, f->type, false);
  995. lb_addr_store(p, addr, lbValue{values[index], f->type});
  996. values[index] = lb_addr_load(p, addr).value;
  997. }
  998. GB_ASSERT(LLVMIsALoadInst(values[index]));
  999. LLVMValueRef ptr = LLVMGetOperand(values[index], 0);
  1000. LLVMValueRef *indices = gb_alloc_array(temporary_allocator(), LLVMValueRef, idx_list_len);
  1001. LLVMTypeRef lt_u32 = lb_type(m, t_u32);
  1002. for (unsigned i = 0; i < idx_list_len; i++) {
  1003. indices[i] = LLVMConstInt(lt_u32, idx_list[i], false);
  1004. }
  1005. ptr = LLVMBuildGEP2(p->builder, lb_type(m, f->type), ptr, indices, idx_list_len, "");
  1006. ptr = LLVMBuildPointerCast(p->builder, ptr, lb_type(m, alloc_type_pointer(tav.type)), "");
  1007. if (LLVMIsALoadInst(elem_value)) {
  1008. i64 sz = type_size_of(tav.type);
  1009. LLVMValueRef src = LLVMGetOperand(elem_value, 0);
  1010. lb_mem_copy_non_overlapping(p, {ptr, t_rawptr}, {src, t_rawptr}, lb_const_int(m, t_int, sz), false);
  1011. } else {
  1012. LLVMBuildStore(p->builder, elem_value, ptr);
  1013. }
  1014. #endif
  1015. is_constant = false;
  1016. } else {
  1017. is_constant = false;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. } else {
  1025. for_array(i, cl->elems) {
  1026. Entity *f = type->Struct.fields[i];
  1027. TypeAndValue tav = cl->elems[i]->tav;
  1028. ExactValue val = {};
  1029. if (tav.mode != Addressing_Invalid) {
  1030. val = tav.value;
  1031. }
  1032. i32 index = field_remapping[f->Variable.field_index];
  1033. if (elem_type_can_be_constant(f->type)) {
  1034. values[index] = lb_const_value(m, f->type, val, cc).value;
  1035. visited[index] = true;
  1036. }
  1037. }
  1038. }
  1039. for (isize i = 0; i < value_count; i++) {
  1040. if (!visited[i]) {
  1041. GB_ASSERT(values[i] == nullptr);
  1042. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  1043. values[i] = LLVMConstNull(type);
  1044. }
  1045. }
  1046. bool is_constant = true;
  1047. for (isize i = 0; i < value_count; i++) {
  1048. LLVMValueRef val = values[i];
  1049. if (!LLVMIsConstant(val)) {
  1050. GB_ASSERT(is_local);
  1051. GB_ASSERT(LLVMIsALoadInst(val));
  1052. is_constant = false;
  1053. }
  1054. }
  1055. if (is_constant) {
  1056. res.value = llvm_const_named_struct_internal(struct_type, values, cast(unsigned)value_count);
  1057. return res;
  1058. } else {
  1059. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  1060. LLVMValueRef *old_values = values;
  1061. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  1062. for (isize i = 0; i < value_count; i++) {
  1063. LLVMValueRef old_value = old_values[i];
  1064. if (LLVMIsConstant(old_value)) {
  1065. new_values[i] = old_value;
  1066. } else {
  1067. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  1068. }
  1069. }
  1070. LLVMValueRef constant_value = llvm_const_named_struct_internal(struct_type, new_values, cast(unsigned)value_count);
  1071. GB_ASSERT(is_local);
  1072. lbProcedure *p = m->curr_procedure;
  1073. lbAddr v = lb_add_local_generated(p, res.type, true);
  1074. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, v);
  1075. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  1076. for (isize i = 0; i < value_count; i++) {
  1077. LLVMValueRef val = old_values[i];
  1078. if (!LLVMIsConstant(val)) {
  1079. LLVMValueRef dst = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), v.addr.value, cast(unsigned)i, "");
  1080. // if (LLVMIsALoadInst(val)) {
  1081. // Type *ptr_type = v.addr.type;
  1082. // i64 sz = type_size_of(type_deref(ptr_type));
  1083. // LLVMValueRef src = LLVMGetOperand(val, 0);
  1084. // lb_mem_copy_non_overlapping(p, {dst, ptr_type}, {src, ptr_type}, lb_const_int(m, t_int, sz), false);
  1085. // } else {
  1086. LLVMBuildStore(p->builder, val, dst);
  1087. // }
  1088. }
  1089. }
  1090. return lb_addr_load(p, v);
  1091. }
  1092. } else if (is_type_bit_set(type)) {
  1093. ast_node(cl, CompoundLit, value.value_compound);
  1094. if (cl->elems.count == 0) {
  1095. return lb_const_nil(m, original_type);
  1096. }
  1097. i64 sz = type_size_of(type);
  1098. if (sz == 0) {
  1099. return lb_const_nil(m, original_type);
  1100. }
  1101. BigInt bits = {};
  1102. BigInt one = {};
  1103. big_int_from_u64(&one, 1);
  1104. for_array(i, cl->elems) {
  1105. Ast *e = cl->elems[i];
  1106. GB_ASSERT(e->kind != Ast_FieldValue);
  1107. TypeAndValue tav = e->tav;
  1108. if (tav.mode != Addressing_Constant) {
  1109. continue;
  1110. }
  1111. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  1112. i64 v = big_int_to_i64(&tav.value.value_integer);
  1113. i64 lower = type->BitSet.lower;
  1114. u64 index = cast(u64)(v-lower);
  1115. BigInt bit = {};
  1116. big_int_from_u64(&bit, index);
  1117. big_int_shl(&bit, &one, &bit);
  1118. big_int_or(&bits, &bits, &bit);
  1119. }
  1120. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  1121. return res;
  1122. } else if (is_type_matrix(type)) {
  1123. ast_node(cl, CompoundLit, value.value_compound);
  1124. Type *elem_type = type->Matrix.elem;
  1125. isize elem_count = cl->elems.count;
  1126. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1127. return lb_const_nil(m, original_type);
  1128. }
  1129. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  1130. i64 total_count = matrix_type_total_internal_elems(type);
  1131. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1132. if (cl->elems[0]->kind == Ast_FieldValue) {
  1133. for_array(j, cl->elems) {
  1134. Ast *elem = cl->elems[j];
  1135. ast_node(fv, FieldValue, elem);
  1136. if (is_ast_range(fv->field)) {
  1137. ast_node(ie, BinaryExpr, fv->field);
  1138. TypeAndValue lo_tav = ie->left->tav;
  1139. TypeAndValue hi_tav = ie->right->tav;
  1140. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1141. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1142. TokenKind op = ie->op.kind;
  1143. i64 lo = exact_value_to_i64(lo_tav.value);
  1144. i64 hi = exact_value_to_i64(hi_tav.value);
  1145. if (op != Token_RangeHalf) {
  1146. hi += 1;
  1147. }
  1148. GB_ASSERT(0 <= lo && lo <= max_count);
  1149. GB_ASSERT(0 <= hi && hi <= max_count);
  1150. GB_ASSERT(lo <= hi);
  1151. TypeAndValue tav = fv->value->tav;
  1152. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  1153. for (i64 k = lo; k < hi; k++) {
  1154. i64 offset = matrix_row_major_index_to_offset(type, k);
  1155. GB_ASSERT(values[offset] == nullptr);
  1156. values[offset] = val;
  1157. }
  1158. } else {
  1159. TypeAndValue index_tav = fv->field->tav;
  1160. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1161. i64 index = exact_value_to_i64(index_tav.value);
  1162. GB_ASSERT(index < max_count);
  1163. TypeAndValue tav = fv->value->tav;
  1164. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc).value;
  1165. i64 offset = matrix_row_major_index_to_offset(type, index);
  1166. GB_ASSERT(values[offset] == nullptr);
  1167. values[offset] = val;
  1168. }
  1169. }
  1170. for (i64 i = 0; i < total_count; i++) {
  1171. if (values[i] == nullptr) {
  1172. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1173. }
  1174. }
  1175. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1176. return res;
  1177. } else {
  1178. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  1179. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1180. for_array(i, cl->elems) {
  1181. TypeAndValue tav = cl->elems[i]->tav;
  1182. GB_ASSERT(tav.mode != Addressing_Invalid);
  1183. i64 offset = 0;
  1184. offset = matrix_row_major_index_to_offset(type, i);
  1185. values[offset] = lb_const_value(m, elem_type, tav.value, cc).value;
  1186. }
  1187. for (isize i = 0; i < total_count; i++) {
  1188. if (values[i] == nullptr) {
  1189. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1190. }
  1191. }
  1192. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1193. return res;
  1194. }
  1195. } else {
  1196. return lb_const_nil(m, original_type);
  1197. }
  1198. break;
  1199. case ExactValue_Procedure:
  1200. GB_PANIC("handled earlier");
  1201. break;
  1202. case ExactValue_Typeid:
  1203. return lb_typeid(m, value.value_typeid);
  1204. }
  1205. return lb_const_nil(m, original_type);
  1206. }