2
0

llvm_backend_const.cpp 35 KB

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