llvm_backend_const.cpp 41 KB

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