llvm_backend_const.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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(lbModule *m, LLVMValueRef val, LLVMTypeRef dst, bool *failure_) {
  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. }
  80. case LLVMStructTypeKind: {
  81. unsigned src_n = LLVMCountStructElementTypes(src);
  82. unsigned dst_n = LLVMCountStructElementTypes(dst);
  83. if (src_n != dst_n) goto failure;
  84. LLVMValueRef *field_vals = temporary_alloc_array<LLVMValueRef>(dst_n);
  85. for (unsigned i = 0; i < dst_n; i++) {
  86. LLVMValueRef field_val = llvm_const_extract_value(m, val, i);
  87. if (field_val == nullptr) goto failure;
  88. LLVMTypeRef dst_elem_ty = LLVMStructGetTypeAtIndex(dst, i);
  89. field_vals[i] = llvm_const_cast(m, field_val, dst_elem_ty, failure_);
  90. if (failure_ && *failure_) goto failure;
  91. }
  92. if (!LLVMIsLiteralStruct(dst)) {
  93. return LLVMConstNamedStruct(dst, field_vals, dst_n);
  94. } else {
  95. return LLVMConstStructInContext(m->ctx, field_vals, dst_n, LLVMIsPackedStruct(dst));
  96. }
  97. }
  98. }
  99. failure:
  100. if (failure_) *failure_ = true;
  101. return val;
  102. }
  103. gb_internal lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) {
  104. GB_ASSERT(is_type_internally_pointer_like(value.type));
  105. GB_ASSERT(is_type_internally_pointer_like(t));
  106. GB_ASSERT(lb_is_const(value));
  107. lbValue res = {};
  108. res.value = LLVMConstPointerCast(value.value, lb_type(m, t));
  109. res.type = t;
  110. return res;
  111. }
  112. gb_internal LLVMValueRef llvm_const_string_internal(lbModule *m, Type *t, LLVMValueRef data, LLVMValueRef len) {
  113. GB_ASSERT(!is_type_string16(t));
  114. if (build_context.metrics.ptr_size < build_context.metrics.int_size) {
  115. LLVMValueRef values[3] = {
  116. data,
  117. LLVMConstNull(lb_type(m, t_i32)),
  118. len,
  119. };
  120. return llvm_const_named_struct_internal(m, lb_type(m, t), values, 3);
  121. } else {
  122. LLVMValueRef values[2] = {
  123. data,
  124. len,
  125. };
  126. return llvm_const_named_struct_internal(m, lb_type(m, t), values, 2);
  127. }
  128. }
  129. gb_internal LLVMValueRef llvm_const_string16_internal(lbModule *m, Type *t, LLVMValueRef data, LLVMValueRef len) {
  130. GB_ASSERT(is_type_string16(t));
  131. if (build_context.metrics.ptr_size < build_context.metrics.int_size) {
  132. LLVMValueRef values[3] = {
  133. data,
  134. LLVMConstNull(lb_type(m, t_i32)),
  135. len,
  136. };
  137. return llvm_const_named_struct_internal(m, lb_type(m, t), values, 3);
  138. } else {
  139. LLVMValueRef values[2] = {
  140. data,
  141. len,
  142. };
  143. return llvm_const_named_struct_internal(m, lb_type(m, t), values, 2);
  144. }
  145. }
  146. gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) {
  147. LLVMTypeRef struct_type = lb_type(m, t);
  148. GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind);
  149. unsigned value_count = cast(unsigned)value_count_;
  150. unsigned elem_count = LLVMCountStructElementTypes(struct_type);
  151. if (elem_count == value_count) {
  152. return llvm_const_named_struct_internal(m, struct_type, values, value_count_);
  153. }
  154. Type *bt = base_type(t);
  155. GB_ASSERT(bt->kind == Type_Struct || bt->kind == Type_Union);
  156. GB_ASSERT(value_count_ == bt->Struct.fields.count);
  157. auto field_remapping = lb_get_struct_remapping(m, t);
  158. unsigned values_with_padding_count = elem_count;
  159. LLVMValueRef *values_with_padding = gb_alloc_array(permanent_allocator(), LLVMValueRef, values_with_padding_count);
  160. for (unsigned i = 0; i < value_count; i++) {
  161. values_with_padding[field_remapping[i]] = values[i];
  162. }
  163. for (unsigned i = 0; i < values_with_padding_count; i++) {
  164. if (values_with_padding[i] == nullptr) {
  165. values_with_padding[i] = LLVMConstNull(LLVMStructGetTypeAtIndex(struct_type, i));
  166. }
  167. }
  168. return llvm_const_named_struct_internal(m, struct_type, values_with_padding, values_with_padding_count);
  169. }
  170. gb_internal LLVMValueRef llvm_const_named_struct_internal(lbModule *m, LLVMTypeRef t, LLVMValueRef *values, isize value_count_) {
  171. unsigned value_count = cast(unsigned)value_count_;
  172. unsigned elem_count = LLVMCountStructElementTypes(t);
  173. GB_ASSERT_MSG(value_count == elem_count, "%s %u %u", LLVMPrintTypeToString(t), value_count, elem_count);
  174. bool failure = false;
  175. for (unsigned i = 0; i < elem_count; i++) {
  176. LLVMTypeRef elem_type = LLVMStructGetTypeAtIndex(t, i);
  177. values[i] = llvm_const_cast(m, values[i], elem_type, &failure);
  178. }
  179. if (failure) {
  180. return LLVMConstStructInContext(m->ctx, values, value_count, true);
  181. }
  182. return LLVMConstNamedStruct(t, values, value_count);
  183. }
  184. gb_internal LLVMValueRef llvm_const_array(lbModule *m, LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) {
  185. unsigned value_count = cast(unsigned)value_count_;
  186. bool failure = false;
  187. for (unsigned i = 0; i < value_count; i++) {
  188. values[i] = llvm_const_cast(m, values[i], elem_type, &failure);
  189. }
  190. if (failure) {
  191. return LLVMConstStructInContext(m->ctx, values, value_count, false);
  192. }
  193. for (unsigned i = 0; i < value_count; i++) {
  194. if (elem_type != LLVMTypeOf(values[i])) {
  195. return LLVMConstStructInContext(m->ctx, values, value_count, false);
  196. }
  197. }
  198. return LLVMConstArray(elem_type, values, value_count);
  199. }
  200. gb_internal LLVMValueRef llvm_const_slice_internal(lbModule *m, LLVMValueRef data, LLVMValueRef len) {
  201. if (build_context.metrics.ptr_size < build_context.metrics.int_size) {
  202. GB_ASSERT(build_context.metrics.ptr_size == 4);
  203. GB_ASSERT(build_context.metrics.int_size == 8);
  204. LLVMValueRef vals[3] = {
  205. data,
  206. LLVMConstNull(lb_type(m, t_u32)),
  207. len,
  208. };
  209. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  210. } else {
  211. LLVMValueRef vals[2] = {
  212. data,
  213. len,
  214. };
  215. return LLVMConstStructInContext(m->ctx, vals, gb_count_of(vals), false);
  216. }
  217. }
  218. gb_internal LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) {
  219. GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type));
  220. GB_ASSERT(are_types_identical(len.type, t_int));
  221. return llvm_const_slice_internal(m, data.value, len.value);
  222. }
  223. gb_internal lbValue lb_const_nil(lbModule *m, Type *type) {
  224. LLVMValueRef v = LLVMConstNull(lb_type(m, type));
  225. return lbValue{v, type};
  226. }
  227. gb_internal lbValue lb_const_undef(lbModule *m, Type *type) {
  228. LLVMValueRef v = LLVMGetUndef(lb_type(m, type));
  229. return lbValue{v, type};
  230. }
  231. gb_internal lbValue lb_const_int(lbModule *m, Type *type, u64 value) {
  232. lbValue res = {};
  233. res.value = LLVMConstInt(lb_type(m, type), cast(unsigned long long)value, !is_type_unsigned(type));
  234. res.type = type;
  235. return res;
  236. }
  237. gb_internal lbValue lb_const_string(lbModule *m, String const &value) {
  238. return lb_const_value(m, t_string, exact_value_string(value));
  239. }
  240. gb_internal lbValue lb_const_string(lbModule *m, String16 const &value) {
  241. return lb_const_value(m, t_string16, exact_value_string16(value));
  242. }
  243. gb_internal lbValue lb_const_bool(lbModule *m, Type *type, bool value) {
  244. lbValue res = {};
  245. res.value = LLVMConstInt(lb_type(m, type), value, false);
  246. res.type = type;
  247. return res;
  248. }
  249. gb_internal LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) {
  250. GB_ASSERT(type_size_of(type) == 2);
  251. u16 u = f32_to_f16(f);
  252. if (is_type_different_to_arch_endianness(type)) {
  253. u = gb_endian_swap16(u);
  254. }
  255. LLVMValueRef i = LLVMConstInt(LLVMInt16TypeInContext(m->ctx), u, false);
  256. return LLVMConstBitCast(i, lb_type(m, type));
  257. }
  258. gb_internal LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) {
  259. GB_ASSERT(type_size_of(type) == 4);
  260. u32 u = bit_cast<u32>(f);
  261. if (is_type_different_to_arch_endianness(type)) {
  262. u = gb_endian_swap32(u);
  263. }
  264. LLVMValueRef i = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), u, false);
  265. return LLVMConstBitCast(i, lb_type(m, type));
  266. }
  267. gb_internal bool lb_is_expr_untyped_const(Ast *expr) {
  268. auto const &tv = type_and_value_of_expr(expr);
  269. if (is_type_untyped(tv.type)) {
  270. return tv.value.kind != ExactValue_Invalid;
  271. }
  272. return false;
  273. }
  274. gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
  275. GB_ASSERT(is_type_typed(t));
  276. auto const &tv = type_and_value_of_expr(expr);
  277. return lb_const_value(m, t, tv.value);
  278. }
  279. gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure_, TokenPos const &pos) {
  280. String file = get_file_path_string(pos.file_id);
  281. String procedure = procedure_;
  282. i32 line = pos.line;
  283. i32 column = pos.column;
  284. switch (build_context.source_code_location_info) {
  285. case SourceCodeLocationInfo_Normal:
  286. break;
  287. case SourceCodeLocationInfo_Obfuscated:
  288. file = obfuscate_string(file, "F");
  289. procedure = obfuscate_string(procedure, "P");
  290. line = obfuscate_i32(line);
  291. column = obfuscate_i32(column);
  292. break;
  293. case SourceCodeLocationInfo_Filename:
  294. file = last_path_element(file);
  295. break;
  296. case SourceCodeLocationInfo_None:
  297. file = str_lit("");
  298. procedure = str_lit("");
  299. line = 0;
  300. column = 0;
  301. break;
  302. }
  303. LLVMValueRef fields[4] = {};
  304. fields[0]/*file*/ = lb_find_or_add_entity_string(m, file, false).value;
  305. fields[1]/*line*/ = lb_const_int(m, t_i32, line).value;
  306. fields[2]/*column*/ = lb_const_int(m, t_i32, column).value;
  307. fields[3]/*procedure*/ = lb_find_or_add_entity_string(m, procedure, false).value;
  308. lbValue res = {};
  309. res.value = llvm_const_named_struct(m, t_source_code_location, fields, gb_count_of(fields));
  310. res.type = t_source_code_location;
  311. return res;
  312. }
  313. gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  314. lbModule *m = p->module;
  315. return lb_const_source_code_location_const(m, procedure, pos);
  316. }
  317. gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) {
  318. String proc_name = {};
  319. if (p->entity) {
  320. proc_name = p->entity->token.string;
  321. }
  322. TokenPos pos = {};
  323. if (node) {
  324. pos = ast_token(node).pos;
  325. }
  326. return lb_emit_source_code_location_const(p, proc_name, pos);
  327. }
  328. gb_internal String lb_source_code_location_gen_name(String const &procedure, TokenPos const &pos) {
  329. gbString s = gb_string_make(permanent_allocator(), "scl$[");
  330. s = gb_string_append_length(s, procedure.text, procedure.len);
  331. if (pos.offset != 0) {
  332. s = gb_string_append_fmt(s, "%d", pos.offset);
  333. } else {
  334. s = gb_string_append_fmt(s, "%d_%d", pos.line, pos.column);
  335. }
  336. s = gb_string_appendc(s, "]");
  337. return make_string(cast(u8 const *)s, gb_string_length(s));
  338. }
  339. gb_internal String lb_source_code_location_gen_name(lbProcedure *p, Ast *node) {
  340. String proc_name = {};
  341. if (p->entity) {
  342. proc_name = p->entity->token.string;
  343. }
  344. TokenPos pos = {};
  345. if (node) {
  346. pos = ast_token(node).pos;
  347. }
  348. return lb_source_code_location_gen_name(proc_name, pos);
  349. }
  350. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  351. lbValue loc = lb_emit_source_code_location_const(p, procedure, pos);
  352. lbAddr addr = lb_add_global_generated_with_name(p->module, loc.type, loc, lb_source_code_location_gen_name(procedure, pos));
  353. lb_make_global_private_const(addr);
  354. return addr.addr;
  355. }
  356. gb_internal lbValue lb_const_source_code_location_as_global_ptr(lbModule *m, String const &procedure, TokenPos const &pos) {
  357. lbValue loc = lb_const_source_code_location_const(m, procedure, pos);
  358. lbAddr addr = lb_add_global_generated_with_name(m, loc.type, loc, lb_source_code_location_gen_name(procedure, pos));
  359. lb_make_global_private_const(addr);
  360. return addr.addr;
  361. }
  362. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, Ast *node) {
  363. lbValue loc = lb_emit_source_code_location_const(p, node);
  364. lbAddr addr = lb_add_global_generated_with_name(p->module, loc.type, loc, lb_source_code_location_gen_name(p, node));
  365. lb_make_global_private_const(addr);
  366. return addr.addr;
  367. }
  368. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) {
  369. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, procedure, pos));
  370. }
  371. gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) {
  372. return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, node));
  373. }
  374. gb_internal LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, lbConstContext cc) {
  375. if (cc.allow_local) {
  376. cc.is_rodata = false;
  377. }
  378. bool is_local = cc.allow_local && m->curr_procedure != nullptr;
  379. bool is_const = true;
  380. if (is_local) {
  381. for (isize i = 0; i < count; i++) {
  382. GB_ASSERT(values[i] != nullptr);
  383. if (!LLVMIsConstant(values[i])) {
  384. is_const = false;
  385. break;
  386. }
  387. }
  388. }
  389. if (!is_const) {
  390. LLVMTypeRef llvm_elem_type = lb_type(m, elem_type);
  391. lbProcedure *p = m->curr_procedure;
  392. GB_ASSERT(p != nullptr);
  393. lbAddr v = lb_add_local_generated(p, type, false);
  394. lbValue ptr = lb_addr_get_ptr(p, v);
  395. for (isize i = 0; i < count; i++) {
  396. lbValue elem = lb_emit_array_epi(p, ptr, i);
  397. if (is_type_proc(elem_type)) {
  398. values[i] = LLVMConstPointerCast(values[i], llvm_elem_type);
  399. }
  400. LLVMBuildStore(p->builder, values[i], elem.value);
  401. }
  402. return lb_addr_load(p, v).value;
  403. }
  404. return llvm_const_array(m, lb_type(m, elem_type), values, cast(unsigned int)count);
  405. }
  406. gb_internal LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) {
  407. if (big_int_is_zero(a)) {
  408. return LLVMConstNull(lb_type(m, original_type));
  409. }
  410. size_t sz = cast(size_t)type_size_of(original_type);
  411. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will be fine :P
  412. u8 *rop = cast(u8 *)rop64;
  413. size_t max_count = 0;
  414. size_t written = 0;
  415. size_t size = 1;
  416. size_t nails = 0;
  417. mp_endian endian = MP_LITTLE_ENDIAN;
  418. max_count = mp_pack_count(a, nails, size);
  419. if (sz < max_count) {
  420. debug_print_big_int(a);
  421. gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
  422. }
  423. GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
  424. GB_ASSERT(gb_size_of(rop64) >= sz);
  425. mp_err err = mp_pack(rop, sz, &written,
  426. MP_LSB_FIRST,
  427. size, endian, nails,
  428. a);
  429. GB_ASSERT(err == MP_OKAY);
  430. if (!is_type_endian_little(original_type)) {
  431. for (size_t i = 0; i < sz/2; i++) {
  432. u8 tmp = rop[i];
  433. rop[i] = rop[sz-1-i];
  434. rop[sz-1-i] = tmp;
  435. }
  436. }
  437. GB_ASSERT(!is_type_array(original_type));
  438. LLVMValueRef value = LLVMConstIntOfArbitraryPrecision(lb_type(m, original_type), cast(unsigned)((sz+7)/8), cast(u64 *)rop);
  439. if (big_int_is_neg(a)) {
  440. value = LLVMConstNeg(value);
  441. }
  442. return value;
  443. }
  444. gb_internal bool lb_is_nested_possibly_constant(Type *ft, Selection const &sel, Ast *elem) {
  445. GB_ASSERT(!sel.indirect);
  446. for (i32 index : sel.index) {
  447. Type *bt = base_type(ft);
  448. switch (bt->kind) {
  449. case Type_Struct:
  450. if (bt->Struct.is_raw_union) {
  451. return false;
  452. }
  453. ft = bt->Struct.fields[index]->type;
  454. break;
  455. case Type_Array:
  456. ft = bt->Array.elem;
  457. break;
  458. default:
  459. return false;
  460. }
  461. }
  462. if (is_type_raw_union(ft)) {
  463. return false;
  464. }
  465. return lb_is_elem_const(elem, ft);
  466. }
  467. #if LLVM_VERSION_MAJOR == 14
  468. LLVMValueRef llvm_const_pad_to_size(lbModule *m, LLVMValueRef val, LLVMTypeRef dst_ty) {
  469. LLVMContextRef ctx = m->ctx;
  470. LLVMTargetDataRef td = LLVMGetModuleDataLayout(m->mod);
  471. LLVMTypeRef src_ty = LLVMTypeOf(val);
  472. unsigned src_bits = (unsigned)LLVMSizeOfTypeInBits(td, src_ty);
  473. unsigned dst_bits = (unsigned)LLVMSizeOfTypeInBits(td, dst_ty);
  474. LLVMValueRef as_int = nullptr;
  475. LLVMTypeKind src_kind = LLVMGetTypeKind(src_ty);
  476. if (src_kind == LLVMIntegerTypeKind ||
  477. src_kind == LLVMFloatTypeKind ||
  478. src_kind == LLVMDoubleTypeKind ||
  479. src_kind == LLVMPointerTypeKind ||
  480. src_kind == LLVMVectorTypeKind) {
  481. LLVMTypeRef src_int_ty = LLVMIntTypeInContext(ctx, src_bits);
  482. as_int = LLVMConstBitCast(val, src_int_ty);
  483. } else if (src_kind == LLVMArrayTypeKind) {
  484. unsigned elem_count = LLVMGetArrayLength(src_ty);
  485. LLVMTypeRef elem_ty = LLVMGetElementType(src_ty);
  486. unsigned elem_bits = (unsigned)LLVMSizeOfTypeInBits(td, elem_ty);
  487. LLVMTypeRef src_int_ty = LLVMIntTypeInContext(ctx, src_bits);
  488. as_int = LLVMConstInt(src_int_ty, 0, false);
  489. for (unsigned i = 0; i < elem_count; i++) {
  490. LLVMValueRef elem = llvm_const_extract_value(m, val, i);
  491. LLVMTypeRef elem_int_ty = LLVMIntTypeInContext(ctx, elem_bits);
  492. LLVMValueRef elem_int = llvm_const_pad_to_size(m, elem, elem_int_ty);
  493. LLVMValueRef shifted = LLVMConstShl(LLVMConstZExt(elem_int, src_int_ty), LLVMConstInt(src_int_ty, i * elem_bits, false));
  494. as_int = LLVMConstOr(as_int, shifted);
  495. }
  496. } else if (src_kind == LLVMStructTypeKind) {
  497. unsigned field_count = LLVMCountStructElementTypes(src_ty);
  498. LLVMTypeRef src_int_ty = LLVMIntTypeInContext(ctx, src_bits);
  499. as_int = LLVMConstInt(src_int_ty, 0, false);
  500. for (unsigned i = 0; i < field_count; i++) {
  501. LLVMTypeRef field_ty = LLVMStructGetTypeAtIndex(src_ty, i);
  502. unsigned field_bits = (unsigned)LLVMSizeOfTypeInBits(td, field_ty);
  503. LLVMValueRef field = llvm_const_extract_value(m, val, i);
  504. LLVMTypeRef field_int_ty = LLVMIntTypeInContext(ctx, field_bits);
  505. LLVMValueRef field_int = llvm_const_pad_to_size(m, field, field_int_ty);
  506. uint64_t field_offset_bytes = LLVMOffsetOfElement(td, src_ty, i);
  507. uint64_t field_offset_bits = field_offset_bytes * 8;
  508. LLVMValueRef shifted = LLVMConstShl(LLVMConstZExt(field_int, src_int_ty), LLVMConstInt(src_int_ty, field_offset_bits, false));
  509. as_int = LLVMConstOr(as_int, shifted);
  510. }
  511. } else {
  512. gb_printf_err("unsupported const_pad source type: %s\n", LLVMPrintTypeToString(src_ty));
  513. return nullptr;
  514. }
  515. if (src_bits != dst_bits) {
  516. LLVMTypeRef dst_int_ty = LLVMIntTypeInContext(ctx, dst_bits);
  517. if (src_bits < dst_bits) {
  518. as_int = LLVMConstZExt(as_int, dst_int_ty);
  519. } else {
  520. as_int = LLVMConstTrunc(as_int, dst_int_ty);
  521. }
  522. }
  523. LLVMTypeKind dst_kind = LLVMGetTypeKind(dst_ty);
  524. if (dst_kind == LLVMIntegerTypeKind ||
  525. dst_kind == LLVMFloatTypeKind ||
  526. dst_kind == LLVMDoubleTypeKind ||
  527. dst_kind == LLVMPointerTypeKind ||
  528. dst_kind == LLVMVectorTypeKind) {
  529. return LLVMConstBitCast(as_int, dst_ty);
  530. } else if (dst_kind == LLVMArrayTypeKind) {
  531. unsigned elem_count = LLVMGetArrayLength(dst_ty);
  532. LLVMTypeRef elem_ty = LLVMGetElementType(dst_ty);
  533. unsigned elem_bits = (unsigned)LLVMSizeOfTypeInBits(td, elem_ty);
  534. LLVMValueRef *elems = temporary_alloc_array<LLVMValueRef>(elem_count);
  535. LLVMTypeRef as_int_ty = LLVMTypeOf(as_int);
  536. for (unsigned i = 0; i < elem_count; i++) {
  537. LLVMValueRef shifted = LLVMConstLShr(as_int, LLVMConstInt(as_int_ty, i * elem_bits, false));
  538. LLVMTypeRef elem_int_ty = LLVMIntTypeInContext(ctx, elem_bits);
  539. LLVMValueRef trunc = LLVMConstTrunc(shifted, elem_int_ty);
  540. elems[i] = llvm_const_pad_to_size(m, trunc, elem_ty);
  541. }
  542. return LLVMConstArray(elem_ty, elems, elem_count);
  543. }
  544. gb_printf_err("unsupported const_pad destination type: %s\n", LLVMPrintTypeToString(dst_ty));
  545. return nullptr;
  546. }
  547. #endif
  548. gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lbConstContext cc, Type *value_type) {
  549. if (cc.allow_local) {
  550. cc.is_rodata = false;
  551. }
  552. LLVMContextRef ctx = m->ctx;
  553. type = default_type(type);
  554. Type *original_type = type;
  555. lbValue res = {};
  556. res.type = original_type;
  557. type = core_type(type);
  558. value = convert_exact_value_for_type(value, type);
  559. bool is_local = cc.allow_local && m->curr_procedure != nullptr;
  560. if (is_type_union(type) && is_type_union_constantable(type)) {
  561. Type *bt = base_type(type);
  562. GB_ASSERT(bt->kind == Type_Union);
  563. if (bt->Union.variants.count == 0) {
  564. return lb_const_nil(m, original_type);
  565. } else if (bt->Union.variants.count == 1) {
  566. Type *t = bt->Union.variants[0];
  567. lbValue cv = lb_const_value(m, t, value, cc);
  568. GB_ASSERT(LLVMIsConstant(cv.value));
  569. LLVMTypeRef llvm_type = lb_type(m, original_type);
  570. if (is_type_union_maybe_pointer(type)) {
  571. LLVMValueRef values[1] = {cv.value};
  572. res.value = llvm_const_named_struct_internal(m, llvm_type, values, 1);
  573. res.type = original_type;
  574. return res;
  575. } else {
  576. unsigned tag_value = 1;
  577. if (bt->Union.kind == UnionType_no_nil) {
  578. tag_value = 0;
  579. }
  580. LLVMValueRef tag = LLVMConstInt(LLVMStructGetTypeAtIndex(llvm_type, 1), tag_value, false);
  581. LLVMValueRef padding = nullptr;
  582. LLVMValueRef values[3] = {cv.value, tag, padding};
  583. isize value_count = 2;
  584. if (LLVMCountStructElementTypes(llvm_type) > 2) {
  585. value_count = 3;
  586. padding = LLVMConstNull(LLVMStructGetTypeAtIndex(llvm_type, 2));
  587. }
  588. res.value = llvm_const_named_struct_internal(m, llvm_type, values, value_count);
  589. res.type = original_type;
  590. return res;
  591. }
  592. } else {
  593. if (value_type == nullptr) {
  594. if (value.kind == ExactValue_Compound) {
  595. ast_node(cl, CompoundLit, value.value_compound);
  596. if (cl->elems.count == 0) {
  597. return lb_const_nil(m, original_type);
  598. }
  599. } else if (value.kind == ExactValue_Invalid) {
  600. return lb_const_nil(m, original_type);
  601. }
  602. }
  603. GB_ASSERT_MSG(value_type != nullptr, "%s :: %s", type_to_string(original_type), exact_value_to_string(value));
  604. i64 block_size = bt->Union.variant_block_size;
  605. if (are_types_identical(value_type, original_type)) {
  606. if (value.kind == ExactValue_Compound) {
  607. ast_node(cl, CompoundLit, value.value_compound);
  608. if (cl->elems.count == 0) {
  609. return lb_const_nil(m, original_type);
  610. }
  611. } else if (value.kind == ExactValue_Invalid) {
  612. return lb_const_nil(m, original_type);
  613. }
  614. GB_PANIC("%s vs %s", type_to_string(value_type), type_to_string(original_type));
  615. }
  616. lbValue cv = lb_const_value(m, value_type, value, cc, value_type);
  617. Type *variant_type = cv.type;
  618. LLVMValueRef values[4] = {};
  619. unsigned value_count = 0;
  620. #if LLVM_VERSION_MAJOR == 14
  621. LLVMTypeRef block_type = lb_type_internal_union_block_type(m, bt);
  622. values[value_count++] = llvm_const_pad_to_size(m, cv.value, block_type);
  623. #else
  624. values[value_count++] = cv.value;
  625. if (type_size_of(variant_type) != block_size) {
  626. LLVMTypeRef padding_type = lb_type_padding_filler(m, block_size - type_size_of(variant_type), 1);
  627. values[value_count++] = LLVMConstNull(padding_type);
  628. }
  629. #endif
  630. Type *tag_type = union_tag_type(bt);
  631. LLVMTypeRef llvm_tag_type = lb_type(m, tag_type);
  632. i64 tag_index = union_variant_index(bt, variant_type);
  633. GB_ASSERT(tag_index >= 0);
  634. values[value_count++] = LLVMConstInt(llvm_tag_type, tag_index, false);
  635. i64 used_size = block_size + type_size_of(tag_type);
  636. i64 union_size = type_size_of(bt);
  637. i64 padding = union_size - used_size;
  638. if (padding > 0) {
  639. LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, 1);
  640. values[value_count++] = LLVMConstNull(padding_type);
  641. }
  642. res.value = LLVMConstStructInContext(m->ctx, values, value_count, true);
  643. return res;
  644. }
  645. }
  646. if (value.kind == ExactValue_Procedure) {
  647. lbValue res = {};
  648. Ast *expr = unparen_expr(value.value_procedure);
  649. GB_ASSERT(expr != nullptr);
  650. if (expr->kind == Ast_ProcLit) {
  651. res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  652. } else {
  653. Entity *e = entity_from_expr(expr);
  654. res = lb_find_procedure_value_from_entity(m, e);
  655. }
  656. if (res.value == nullptr) {
  657. // This is an unspecialized polymorphic procedure, return nil or dummy value
  658. return lb_const_nil(m, original_type);
  659. }
  660. GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
  661. if (LLVMGetIntrinsicID(res.value) == 0) {
  662. // NOTE(bill): do not cast intrinsics as they are not really procedures that can be casted
  663. res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
  664. }
  665. return res;
  666. }
  667. // NOTE(bill): This has to be done AFTER the union stuff
  668. if (value.kind == ExactValue_Invalid) {
  669. return lb_const_nil(m, original_type);
  670. }
  671. if (value.kind == ExactValue_Typeid) {
  672. return lb_typeid(m, value.value_typeid);
  673. }
  674. if (value.kind == ExactValue_Compound) {
  675. ast_node(cl, CompoundLit, value.value_compound);
  676. if (cl->elems.count == 0) {
  677. return lb_const_nil(m, original_type);
  678. }
  679. }
  680. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  681. if (is_type_slice(type)) {
  682. if (value.kind == ExactValue_String) {
  683. GB_ASSERT(is_type_slice(type));
  684. res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value;
  685. return res;
  686. } else if (value.kind == ExactValue_String16) {
  687. GB_ASSERT(is_type_slice(type));
  688. res.value = lb_find_or_add_entity_string16_slice_with_type(m, value.value_string16, original_type).value;
  689. return res;
  690. } else {
  691. ast_node(cl, CompoundLit, value.value_compound);
  692. isize count = cl->elems.count;
  693. if (count == 0) {
  694. return lb_const_nil(m, type);
  695. }
  696. count = gb_max(cast(isize)cl->max_count, count);
  697. Type *elem = base_type(type)->Slice.elem;
  698. Type *t = alloc_type_array(elem, count);
  699. lbValue backing_array = lb_const_value(m, t, value, cc, nullptr);
  700. LLVMValueRef array_data = nullptr;
  701. if (is_local) {
  702. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  703. // its backing data on the stack
  704. lbProcedure *p = m->curr_procedure;
  705. LLVMTypeRef llvm_type = lb_type(m, t);
  706. unsigned alignment = cast(unsigned)gb_max(type_align_of(t), 16);
  707. bool do_local_copy = false;
  708. if (do_local_copy) {
  709. array_data = llvm_alloca(p, llvm_type, alignment);
  710. LLVMValueRef local_copy = llvm_alloca(p, LLVMTypeOf(backing_array.value), alignment);
  711. LLVMBuildStore(p->builder, backing_array.value, local_copy);
  712. LLVMBuildMemCpy(p->builder,
  713. array_data, alignment,
  714. local_copy, alignment,
  715. LLVMConstInt(lb_type(m, t_int), type_size_of(t), false)
  716. );
  717. } else {
  718. array_data = llvm_alloca(p, LLVMTypeOf(backing_array.value), alignment);
  719. LLVMBuildStore(p->builder, backing_array.value, array_data);
  720. array_data = LLVMBuildPointerCast(p->builder, array_data, LLVMPointerType(llvm_type, 0), "");
  721. }
  722. {
  723. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  724. LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
  725. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  726. lbAddr slice = lb_add_local_generated(p, original_type, false);
  727. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
  728. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  729. return lb_addr_load(p, slice);
  730. }
  731. } else {
  732. u32 id = m->global_array_index.fetch_add(1);
  733. gbString str = gb_string_make(temporary_allocator(), "csba$");
  734. str = gb_string_appendc(str, m->module_name);
  735. str = gb_string_append_fmt(str, "$%x", id);
  736. String name = make_string(cast(u8 const *)str, gb_string_length(str));
  737. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  738. array_data = LLVMAddGlobal(m->mod, LLVMTypeOf(backing_array.value), str);
  739. LLVMSetInitializer(array_data, backing_array.value);
  740. if (cc.link_section.len > 0) {
  741. LLVMSetSection(array_data, alloc_cstring(permanent_allocator(), cc.link_section));
  742. }
  743. if (cc.is_rodata) {
  744. LLVMSetGlobalConstant(array_data, true);
  745. }
  746. lbValue g = {};
  747. g.value = LLVMConstPointerCast(array_data, LLVMPointerType(lb_type(m, t), 0));
  748. g.type = t;
  749. lb_add_entity(m, e, g);
  750. lb_add_member(m, name, g);
  751. {
  752. LLVMValueRef ptr = g.value;
  753. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  754. LLVMValueRef values[2] = {ptr, len};
  755. res.value = llvm_const_named_struct(m, original_type, values, 2);
  756. return res;
  757. }
  758. }
  759. }
  760. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  761. if (is_type_rune_array(type)) {
  762. i64 count = type->Array.count;
  763. Type *elem = type->Array.elem;
  764. LLVMTypeRef et = lb_type(m, elem);
  765. Rune rune;
  766. isize offset = 0;
  767. isize width = 1;
  768. String s = value.value_string;
  769. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  770. for (i64 i = 0; i < count && offset < s.len; i++) {
  771. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  772. offset += width;
  773. elems[i] = LLVMConstInt(et, rune, true);
  774. }
  775. GB_ASSERT(offset == s.len);
  776. res.value = llvm_const_array(m, et, elems, cast(unsigned)count);
  777. return res;
  778. }
  779. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  780. Type *core_elem = core_array_type(type);
  781. return lb_const_value(m, core_elem, value, cc);
  782. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  783. GB_ASSERT(type->Array.count == value.value_string.len);
  784. LLVMValueRef data = LLVMConstStringInContext(ctx,
  785. cast(char const *)value.value_string.text,
  786. cast(unsigned)value.value_string.len,
  787. true /*DontNullTerminate*/);
  788. res.value = data;
  789. return res;
  790. } else if (is_type_array(type) &&
  791. value.kind != ExactValue_Invalid &&
  792. value.kind != ExactValue_String &&
  793. value.kind != ExactValue_Compound) {
  794. i64 count = type->Array.count;
  795. Type *elem = type->Array.elem;
  796. lbValue single_elem = lb_const_value(m, elem, value, cc);
  797. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  798. for (i64 i = 0; i < count; i++) {
  799. elems[i] = single_elem.value;
  800. }
  801. res.value = llvm_const_array(m, lb_type(m, elem), elems, cast(unsigned)count);
  802. return res;
  803. } else if (is_type_matrix(type) &&
  804. value.kind != ExactValue_Invalid &&
  805. value.kind != ExactValue_Compound) {
  806. i64 row = type->Matrix.row_count;
  807. i64 column = type->Matrix.column_count;
  808. GB_ASSERT(row == column);
  809. Type *elem = type->Matrix.elem;
  810. lbValue single_elem = lb_const_value(m, elem, value, cc);
  811. single_elem.value = llvm_const_cast(m, single_elem.value, lb_type(m, elem), /*failure_*/nullptr);
  812. i64 total_elem_count = matrix_type_total_internal_elems(type);
  813. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  814. for (i64 i = 0; i < row; i++) {
  815. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  816. }
  817. for (i64 i = 0; i < total_elem_count; i++) {
  818. if (elems[i] == nullptr) {
  819. elems[i] = LLVMConstNull(lb_type(m, elem));
  820. }
  821. }
  822. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  823. return res;
  824. } else if (is_type_simd_vector(type) &&
  825. value.kind != ExactValue_Invalid &&
  826. value.kind != ExactValue_Compound) {
  827. i64 count = type->SimdVector.count;
  828. Type *elem = type->SimdVector.elem;
  829. lbValue single_elem = lb_const_value(m, elem, value, cc);
  830. single_elem.value = llvm_const_cast(m, single_elem.value, lb_type(m, elem), /*failure_*/nullptr);
  831. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  832. for (i64 i = 0; i < count; i++) {
  833. elems[i] = single_elem.value;
  834. }
  835. res.value = LLVMConstVector(elems, cast(unsigned)count);
  836. return res;
  837. }
  838. switch (value.kind) {
  839. case ExactValue_Invalid:
  840. res.value = LLVMConstNull(lb_type(m, original_type));
  841. return res;
  842. case ExactValue_Bool:
  843. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  844. return res;
  845. case ExactValue_String:
  846. {
  847. bool custom_link_section = cc.link_section.len > 0;
  848. LLVMValueRef ptr = nullptr;
  849. lbValue res = {};
  850. res.type = default_type(original_type);
  851. isize len = value.value_string.len;
  852. if (is_type_string16(res.type) || is_type_cstring16(res.type)) {
  853. TEMPORARY_ALLOCATOR_GUARD();
  854. String16 s16 = string_to_string16(temporary_allocator(), value.value_string);
  855. len = s16.len;
  856. ptr = lb_find_or_add_entity_string16_ptr(m, s16, custom_link_section);
  857. } else {
  858. ptr = lb_find_or_add_entity_string_ptr(m, value.value_string, custom_link_section);
  859. }
  860. if (custom_link_section) {
  861. LLVMSetSection(ptr, alloc_cstring(permanent_allocator(), cc.link_section));
  862. }
  863. if (is_type_cstring(res.type) || is_type_cstring16(res.type)) {
  864. res.value = ptr;
  865. } else {
  866. if (len == 0) {
  867. if (is_type_string16(res.type)) {
  868. ptr = LLVMConstNull(lb_type(m, t_u16_ptr));
  869. } else {
  870. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  871. }
  872. }
  873. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), len, true);
  874. GB_ASSERT(is_type_string(original_type));
  875. if (is_type_string16(res.type)) {
  876. res.value = llvm_const_string16_internal(m, original_type, ptr, str_len);
  877. } else {
  878. res.value = llvm_const_string_internal(m, original_type, ptr, str_len);
  879. }
  880. }
  881. return res;
  882. }
  883. case ExactValue_String16:
  884. {
  885. GB_ASSERT(is_type_string16(res.type) || is_type_cstring16(res.type));
  886. bool custom_link_section = cc.link_section.len > 0;
  887. LLVMValueRef ptr = lb_find_or_add_entity_string16_ptr(m, value.value_string16, custom_link_section);
  888. lbValue res = {};
  889. res.type = default_type(original_type);
  890. if (custom_link_section) {
  891. LLVMSetSection(ptr, alloc_cstring(permanent_allocator(), cc.link_section));
  892. }
  893. if (is_type_cstring16(res.type)) {
  894. res.value = ptr;
  895. } else {
  896. if (value.value_string16.len == 0) {
  897. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  898. }
  899. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string16.len, true);
  900. GB_ASSERT(is_type_string(original_type));
  901. res.value = llvm_const_string16_internal(m, original_type, ptr, str_len);
  902. }
  903. return res;
  904. }
  905. case ExactValue_Integer:
  906. if (is_type_pointer(type) || is_type_multi_pointer(type) || is_type_proc(type)) {
  907. LLVMTypeRef t = lb_type(m, original_type);
  908. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  909. res.value = LLVMConstIntToPtr(i, t);
  910. } else {
  911. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  912. }
  913. return res;
  914. case ExactValue_Float:
  915. if (is_type_different_to_arch_endianness(type)) {
  916. if (type->Basic.kind == Basic_f32le || type->Basic.kind == Basic_f32be) {
  917. f32 f = static_cast<float>(value.value_float);
  918. u32 u = bit_cast<u32>(f);
  919. u = gb_endian_swap32(u);
  920. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f32>(u));
  921. } else if (type->Basic.kind == Basic_f16le || type->Basic.kind == Basic_f16be) {
  922. f32 f = static_cast<float>(value.value_float);
  923. u16 u = f32_to_f16(f);
  924. u = gb_endian_swap16(u);
  925. res.value = LLVMConstReal(lb_type(m, original_type), f16_to_f32(u));
  926. } else {
  927. u64 u = bit_cast<u64>(value.value_float);
  928. u = gb_endian_swap64(u);
  929. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  930. }
  931. } else {
  932. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  933. }
  934. return res;
  935. case ExactValue_Complex:
  936. {
  937. LLVMValueRef values[2] = {};
  938. switch (8*type_size_of(type)) {
  939. case 32:
  940. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  941. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  942. break;
  943. case 64:
  944. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  945. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  946. break;
  947. case 128:
  948. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  949. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  950. break;
  951. }
  952. res.value = llvm_const_named_struct(m, original_type, values, 2);
  953. return res;
  954. }
  955. break;
  956. case ExactValue_Quaternion:
  957. {
  958. LLVMValueRef values[4] = {};
  959. switch (8*type_size_of(type)) {
  960. case 64:
  961. // @QuaternionLayout
  962. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  963. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  964. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  965. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  966. break;
  967. case 128:
  968. // @QuaternionLayout
  969. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  970. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  971. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  972. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  973. break;
  974. case 256:
  975. // @QuaternionLayout
  976. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  977. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  978. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  979. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  980. break;
  981. }
  982. res.value = llvm_const_named_struct(m, original_type, values, 4);
  983. return res;
  984. }
  985. break;
  986. case ExactValue_Pointer:
  987. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  988. return res;
  989. case ExactValue_Compound:
  990. if (is_type_slice(type)) {
  991. return lb_const_value(m, type, value, cc);
  992. } else if (is_type_soa_struct(type)) {
  993. GB_ASSERT(type->kind == Type_Struct);
  994. GB_ASSERT(type->Struct.soa_kind == StructSoa_Fixed);
  995. ast_node(cl, CompoundLit, value.value_compound);
  996. Type *elem_type = type->Struct.soa_elem;
  997. isize elem_count = cl->elems.count;
  998. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  999. return lb_const_nil(m, original_type);
  1000. }
  1001. if (cl->elems[0]->kind == Ast_FieldValue) {
  1002. TEMPORARY_ALLOCATOR_GUARD();
  1003. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1004. isize elem_count = cast(isize)type->Struct.soa_count;
  1005. LLVMValueRef *aos_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1006. isize value_index = 0;
  1007. for (i64 i = 0; i < elem_count; i++) {
  1008. bool found = false;
  1009. for (isize j = 0; j < elem_count; j++) {
  1010. Ast *elem = cl->elems[j];
  1011. ast_node(fv, FieldValue, elem);
  1012. if (is_ast_range(fv->field)) {
  1013. ast_node(ie, BinaryExpr, fv->field);
  1014. TypeAndValue lo_tav = ie->left->tav;
  1015. TypeAndValue hi_tav = ie->right->tav;
  1016. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1017. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1018. TokenKind op = ie->op.kind;
  1019. i64 lo = exact_value_to_i64(lo_tav.value);
  1020. i64 hi = exact_value_to_i64(hi_tav.value);
  1021. if (op != Token_RangeHalf) {
  1022. hi += 1;
  1023. }
  1024. if (lo == i) {
  1025. TypeAndValue tav = fv->value->tav;
  1026. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1027. for (i64 k = lo; k < hi; k++) {
  1028. aos_values[value_index++] = val;
  1029. }
  1030. found = true;
  1031. i += (hi-lo-1);
  1032. break;
  1033. }
  1034. } else {
  1035. TypeAndValue index_tav = fv->field->tav;
  1036. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1037. i64 index = exact_value_to_i64(index_tav.value);
  1038. if (index == i) {
  1039. TypeAndValue tav = fv->value->tav;
  1040. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1041. aos_values[value_index++] = val;
  1042. found = true;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. if (!found) {
  1048. aos_values[value_index++] = nullptr;
  1049. }
  1050. }
  1051. isize field_count = type->Struct.fields.count;
  1052. LLVMValueRef *soa_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, field_count);
  1053. for (isize i = 0; i < field_count; i++) {
  1054. TEMPORARY_ALLOCATOR_GUARD();
  1055. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1056. Entity *f = type->Struct.fields[i];
  1057. Type *array_type = f->type;
  1058. GB_ASSERT(array_type->kind == Type_Array);
  1059. Type *field_type = array_type->Array.elem;
  1060. for (isize j = 0; j < elem_count; j++) {
  1061. LLVMValueRef v = aos_values[j];
  1062. if (v != nullptr) {
  1063. values[j] = llvm_const_extract_value(m, v, cast(unsigned)i);
  1064. } else {
  1065. values[j] = LLVMConstNull(lb_type(m, field_type));
  1066. }
  1067. }
  1068. soa_values[i] = lb_build_constant_array_values(m, array_type, field_type, elem_count, values, cc);
  1069. }
  1070. res.value = llvm_const_named_struct(m, type, soa_values, field_count);
  1071. return res;
  1072. } else {
  1073. GB_ASSERT_MSG(elem_count == type->Struct.soa_count, "%td != %td", elem_count, type->Struct.soa_count);
  1074. TEMPORARY_ALLOCATOR_GUARD();
  1075. isize elem_count = cast(isize)type->Struct.soa_count;
  1076. LLVMValueRef *aos_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1077. for (isize i = 0; i < elem_count; i++) {
  1078. TypeAndValue tav = cl->elems[i]->tav;
  1079. GB_ASSERT(tav.mode != Addressing_Invalid);
  1080. aos_values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1081. }
  1082. for (isize i = elem_count; i < type->Struct.soa_count; i++) {
  1083. aos_values[i] = nullptr;
  1084. }
  1085. isize field_count = type->Struct.fields.count;
  1086. LLVMValueRef *soa_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, field_count);
  1087. for (isize i = 0; i < field_count; i++) {
  1088. TEMPORARY_ALLOCATOR_GUARD();
  1089. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1090. Entity *f = type->Struct.fields[i];
  1091. Type *array_type = f->type;
  1092. GB_ASSERT(array_type->kind == Type_Array);
  1093. Type *field_type = array_type->Array.elem;
  1094. for (isize j = 0; j < elem_count; j++) {
  1095. LLVMValueRef v = aos_values[j];
  1096. if (v != nullptr) {
  1097. values[j] = llvm_const_extract_value(m, v, cast(unsigned)i);
  1098. } else {
  1099. values[j] = LLVMConstNull(lb_type(m, field_type));
  1100. }
  1101. }
  1102. soa_values[i] = lb_build_constant_array_values(m, array_type, field_type, elem_count, values, cc);
  1103. }
  1104. res.value = llvm_const_named_struct(m, type, soa_values, field_count);
  1105. return res;
  1106. }
  1107. } else if (is_type_array(type)) {
  1108. ast_node(cl, CompoundLit, value.value_compound);
  1109. Type *elem_type = type->Array.elem;
  1110. isize elem_count = cl->elems.count;
  1111. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1112. return lb_const_nil(m, original_type);
  1113. }
  1114. if (cl->elems[0]->kind == Ast_FieldValue) {
  1115. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1116. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1117. isize value_index = 0;
  1118. for (i64 i = 0; i < type->Array.count; i++) {
  1119. bool found = false;
  1120. for (isize j = 0; j < elem_count; j++) {
  1121. Ast *elem = cl->elems[j];
  1122. ast_node(fv, FieldValue, elem);
  1123. if (is_ast_range(fv->field)) {
  1124. ast_node(ie, BinaryExpr, fv->field);
  1125. TypeAndValue lo_tav = ie->left->tav;
  1126. TypeAndValue hi_tav = ie->right->tav;
  1127. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1128. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1129. TokenKind op = ie->op.kind;
  1130. i64 lo = exact_value_to_i64(lo_tav.value);
  1131. i64 hi = exact_value_to_i64(hi_tav.value);
  1132. if (op != Token_RangeHalf) {
  1133. hi += 1;
  1134. }
  1135. if (lo == i) {
  1136. TypeAndValue tav = fv->value->tav;
  1137. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1138. for (i64 k = lo; k < hi; k++) {
  1139. values[value_index++] = val;
  1140. }
  1141. found = true;
  1142. i += (hi-lo-1);
  1143. break;
  1144. }
  1145. } else {
  1146. TypeAndValue index_tav = fv->field->tav;
  1147. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1148. i64 index = exact_value_to_i64(index_tav.value);
  1149. if (index == i) {
  1150. TypeAndValue tav = fv->value->tav;
  1151. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1152. values[value_index++] = val;
  1153. found = true;
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. if (!found) {
  1159. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1160. }
  1161. }
  1162. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1163. return res;
  1164. } else if (are_types_identical(value.value_compound->tav.type, elem_type)) {
  1165. // Compound is of array item type; expand its value to all items in array.
  1166. LLVMValueRef* values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1167. for (isize i = 0; i < type->Array.count; i++) {
  1168. values[i] = lb_const_value(m, elem_type, value, cc, elem_type).value;
  1169. }
  1170. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1171. return res;
  1172. } else {
  1173. // Assume that compound value is an array literal
  1174. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  1175. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1176. for (isize i = 0; i < elem_count; i++) {
  1177. TypeAndValue tav = cl->elems[i]->tav;
  1178. GB_ASSERT(tav.mode != Addressing_Invalid);
  1179. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1180. }
  1181. for (isize i = elem_count; i < type->Array.count; i++) {
  1182. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1183. }
  1184. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1185. return res;
  1186. }
  1187. } else if (is_type_enumerated_array(type)) {
  1188. ast_node(cl, CompoundLit, value.value_compound);
  1189. Type *elem_type = type->EnumeratedArray.elem;
  1190. isize elem_count = cl->elems.count;
  1191. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1192. return lb_const_nil(m, original_type);
  1193. }
  1194. if (cl->elems[0]->kind == Ast_FieldValue) {
  1195. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1196. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  1197. isize value_index = 0;
  1198. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  1199. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  1200. for (i64 i = total_lo; i <= total_hi; i++) {
  1201. bool found = false;
  1202. for (isize j = 0; j < elem_count; j++) {
  1203. Ast *elem = cl->elems[j];
  1204. ast_node(fv, FieldValue, elem);
  1205. if (is_ast_range(fv->field)) {
  1206. ast_node(ie, BinaryExpr, fv->field);
  1207. TypeAndValue lo_tav = ie->left->tav;
  1208. TypeAndValue hi_tav = ie->right->tav;
  1209. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1210. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1211. TokenKind op = ie->op.kind;
  1212. i64 lo = exact_value_to_i64(lo_tav.value);
  1213. i64 hi = exact_value_to_i64(hi_tav.value);
  1214. if (op != Token_RangeHalf) {
  1215. hi += 1;
  1216. }
  1217. if (lo == i) {
  1218. TypeAndValue tav = fv->value->tav;
  1219. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1220. for (i64 k = lo; k < hi; k++) {
  1221. values[value_index++] = val;
  1222. }
  1223. found = true;
  1224. i += (hi-lo-1);
  1225. break;
  1226. }
  1227. } else {
  1228. TypeAndValue index_tav = fv->field->tav;
  1229. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1230. i64 index = exact_value_to_i64(index_tav.value);
  1231. if (index == i) {
  1232. TypeAndValue tav = fv->value->tav;
  1233. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1234. values[value_index++] = val;
  1235. found = true;
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. if (!found) {
  1241. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1242. }
  1243. }
  1244. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  1245. return res;
  1246. } else {
  1247. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  1248. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  1249. for (isize i = 0; i < elem_count; i++) {
  1250. TypeAndValue tav = cl->elems[i]->tav;
  1251. GB_ASSERT(tav.mode != Addressing_Invalid);
  1252. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1253. }
  1254. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  1255. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1256. }
  1257. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  1258. return res;
  1259. }
  1260. } else if (is_type_simd_vector(type)) {
  1261. ast_node(cl, CompoundLit, value.value_compound);
  1262. Type *elem_type = type->SimdVector.elem;
  1263. isize elem_count = cl->elems.count;
  1264. if (elem_count == 0) {
  1265. return lb_const_nil(m, original_type);
  1266. }
  1267. GB_ASSERT(elem_type_can_be_constant(elem_type));
  1268. isize total_elem_count = cast(isize)type->SimdVector.count;
  1269. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  1270. if (cl->elems[0]->kind == Ast_FieldValue) {
  1271. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1272. isize value_index = 0;
  1273. for (i64 i = 0; i < total_elem_count; i++) {
  1274. bool found = false;
  1275. for (isize j = 0; j < elem_count; j++) {
  1276. Ast *elem = cl->elems[j];
  1277. ast_node(fv, FieldValue, elem);
  1278. if (is_ast_range(fv->field)) {
  1279. ast_node(ie, BinaryExpr, fv->field);
  1280. TypeAndValue lo_tav = ie->left->tav;
  1281. TypeAndValue hi_tav = ie->right->tav;
  1282. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1283. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1284. TokenKind op = ie->op.kind;
  1285. i64 lo = exact_value_to_i64(lo_tav.value);
  1286. i64 hi = exact_value_to_i64(hi_tav.value);
  1287. if (op != Token_RangeHalf) {
  1288. hi += 1;
  1289. }
  1290. if (lo == i) {
  1291. TypeAndValue tav = fv->value->tav;
  1292. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1293. for (i64 k = lo; k < hi; k++) {
  1294. values[value_index++] = val;
  1295. }
  1296. found = true;
  1297. i += (hi-lo-1);
  1298. break;
  1299. }
  1300. } else {
  1301. TypeAndValue index_tav = fv->field->tav;
  1302. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1303. i64 index = exact_value_to_i64(index_tav.value);
  1304. if (index == i) {
  1305. TypeAndValue tav = fv->value->tav;
  1306. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1307. values[value_index++] = val;
  1308. found = true;
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. if (!found) {
  1314. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1315. }
  1316. }
  1317. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  1318. return res;
  1319. } else {
  1320. for (isize i = 0; i < elem_count; i++) {
  1321. TypeAndValue tav = cl->elems[i]->tav;
  1322. GB_ASSERT(tav.mode != Addressing_Invalid);
  1323. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1324. }
  1325. LLVMTypeRef et = lb_type(m, elem_type);
  1326. for (isize i = elem_count; i < total_elem_count; i++) {
  1327. values[i] = LLVMConstNull(et);
  1328. }
  1329. for (isize i = 0; i < total_elem_count; i++) {
  1330. values[i] = llvm_const_cast(m, values[i], et, /*failure_*/nullptr);
  1331. }
  1332. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  1333. return res;
  1334. }
  1335. } else if (is_type_struct(type)) {
  1336. ast_node(cl, CompoundLit, value.value_compound);
  1337. if (cl->elems.count == 0) {
  1338. return lb_const_nil(m, original_type);
  1339. }
  1340. if (is_type_raw_union(type)) {
  1341. if (is_type_raw_union_constantable(type)) {
  1342. GB_ASSERT(cl->elems.count == 1);
  1343. GB_ASSERT(cl->elems[0]->kind == Ast_FieldValue);
  1344. ast_node(fv, FieldValue, cl->elems[0]);
  1345. Entity *f = entity_of_node(fv->field);
  1346. TypeAndValue tav = fv->value->tav;
  1347. if (tav.value.kind != ExactValue_Invalid) {
  1348. lbValue value = lb_const_value(m, f->type, tav.value, cc, f->type);
  1349. LLVMValueRef values[2];
  1350. unsigned value_count = 0;
  1351. values[value_count++] = value.value;
  1352. i64 union_alignment = type_align_of(type);
  1353. i64 value_alignment = type_align_of(f->type);
  1354. i64 alignment = gb_max(gb_min(value_alignment, union_alignment), 1);
  1355. i64 union_size = type_size_of(type);
  1356. i64 value_size = lb_sizeof(LLVMTypeOf(value.value));
  1357. i64 padding = union_size-value_size;
  1358. if (padding > 0) {
  1359. LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, alignment);
  1360. values[value_count++] = LLVMConstNull(padding_type);
  1361. }
  1362. LLVMValueRef res = LLVMConstStructInContext(m->ctx, values, value_count, /*packed*/padding > 0);
  1363. return {res, original_type};
  1364. }
  1365. }
  1366. return lb_const_nil(m, original_type);
  1367. }
  1368. LLVMTypeRef struct_type = lb_type(m, original_type);
  1369. auto field_remapping = lb_get_struct_remapping(m, type);
  1370. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  1371. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  1372. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  1373. if (cl->elems[0]->kind == Ast_FieldValue) {
  1374. isize elem_count = cl->elems.count;
  1375. for (isize i = 0; i < elem_count; i++) {
  1376. ast_node(fv, FieldValue, cl->elems[i]);
  1377. String name = fv->field->Ident.token.string;
  1378. TypeAndValue tav = fv->value->tav;
  1379. GB_ASSERT(tav.mode != Addressing_Invalid);
  1380. Selection sel = lookup_field(type, name, false);
  1381. GB_ASSERT(!sel.indirect);
  1382. Entity *f = type->Struct.fields[sel.index[0]];
  1383. i32 index = field_remapping[f->Variable.field_index];
  1384. if (elem_type_can_be_constant(f->type)) {
  1385. if (sel.index.count == 1) {
  1386. lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
  1387. LLVMTypeRef value_type = LLVMTypeOf(value.value);
  1388. GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
  1389. values[index] = value.value;
  1390. visited[index] = true;
  1391. } else {
  1392. if (!visited[index]) {
  1393. auto new_cc = cc;
  1394. new_cc.allow_local = false;
  1395. values[index] = lb_const_value(m, f->type, {}, new_cc).value;
  1396. visited[index] = true;
  1397. }
  1398. unsigned idx_list_len = cast(unsigned)sel.index.count-1;
  1399. unsigned *idx_list = gb_alloc_array(temporary_allocator(), unsigned, idx_list_len);
  1400. if (lb_is_nested_possibly_constant(type, sel, fv->value)) {
  1401. bool is_constant = true;
  1402. Type *cv_type = f->type;
  1403. for (isize j = 1; j < sel.index.count; j++) {
  1404. i32 index = sel.index[j];
  1405. Type *cvt = base_type(cv_type);
  1406. if (cvt->kind == Type_Struct) {
  1407. if (cvt->Struct.is_raw_union) {
  1408. // sanity check which should have been caught by `lb_is_nested_possibly_constant`
  1409. is_constant = false;
  1410. break;
  1411. }
  1412. cv_type = cvt->Struct.fields[index]->type;
  1413. if (is_type_struct(cvt)) {
  1414. auto cv_field_remapping = lb_get_struct_remapping(m, cvt);
  1415. unsigned remapped_index = cast(unsigned)cv_field_remapping[index];
  1416. idx_list[j-1] = remapped_index;
  1417. } else {
  1418. idx_list[j-1] = cast(unsigned)index;
  1419. }
  1420. } else if (cvt->kind == Type_Array) {
  1421. cv_type = cvt->Array.elem;
  1422. idx_list[j-1] = cast(unsigned)index;
  1423. } else {
  1424. GB_PANIC("UNKNOWN TYPE: %s", type_to_string(cv_type));
  1425. }
  1426. }
  1427. if (is_constant) {
  1428. LLVMValueRef elem_value = lb_const_value(m, tav.type, tav.value, cc, tav.type).value;
  1429. if (LLVMIsConstant(elem_value) && LLVMIsConstant(values[index])) {
  1430. values[index] = llvm_const_insert_value(m, values[index], elem_value, idx_list, idx_list_len);
  1431. } else if (is_local) {
  1432. #if 1
  1433. lbProcedure *p = m->curr_procedure;
  1434. GB_ASSERT(p != nullptr);
  1435. if (LLVMIsConstant(values[index])) {
  1436. lbAddr addr = lb_add_local_generated(p, f->type, false);
  1437. lb_addr_store(p, addr, lbValue{values[index], f->type});
  1438. values[index] = lb_addr_load(p, addr).value;
  1439. }
  1440. GB_ASSERT(LLVMIsALoadInst(values[index]));
  1441. LLVMValueRef ptr = LLVMGetOperand(values[index], 0);
  1442. LLVMValueRef *indices = gb_alloc_array(temporary_allocator(), LLVMValueRef, idx_list_len);
  1443. LLVMTypeRef lt_u32 = lb_type(m, t_u32);
  1444. for (unsigned i = 0; i < idx_list_len; i++) {
  1445. indices[i] = LLVMConstInt(lt_u32, idx_list[i], false);
  1446. }
  1447. ptr = LLVMBuildGEP2(p->builder, lb_type(m, f->type), ptr, indices, idx_list_len, "");
  1448. ptr = LLVMBuildPointerCast(p->builder, ptr, lb_type(m, alloc_type_pointer(tav.type)), "");
  1449. if (LLVMIsALoadInst(elem_value)) {
  1450. i64 sz = type_size_of(tav.type);
  1451. LLVMValueRef src = LLVMGetOperand(elem_value, 0);
  1452. lb_mem_copy_non_overlapping(p, {ptr, t_rawptr}, {src, t_rawptr}, lb_const_int(m, t_int, sz), false);
  1453. } else {
  1454. LLVMBuildStore(p->builder, elem_value, ptr);
  1455. }
  1456. #endif
  1457. is_constant = false;
  1458. } else {
  1459. is_constant = false;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. }
  1465. }
  1466. } else {
  1467. for_array(i, cl->elems) {
  1468. Entity *f = type->Struct.fields[i];
  1469. TypeAndValue tav = cl->elems[i]->tav;
  1470. ExactValue val = {};
  1471. if (tav.mode != Addressing_Invalid) {
  1472. val = tav.value;
  1473. }
  1474. i32 index = field_remapping[f->Variable.field_index];
  1475. if (elem_type_can_be_constant(f->type)) {
  1476. lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
  1477. LLVMTypeRef value_type = LLVMTypeOf(value.value);
  1478. GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
  1479. values[index] = value.value;
  1480. visited[index] = true;
  1481. }
  1482. }
  1483. }
  1484. for (isize i = 0; i < value_count; i++) {
  1485. if (!visited[i]) {
  1486. GB_ASSERT(values[i] == nullptr);
  1487. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  1488. values[i] = LLVMConstNull(type);
  1489. }
  1490. }
  1491. bool is_constant = true;
  1492. for (isize i = 0; i < value_count; i++) {
  1493. LLVMValueRef val = values[i];
  1494. if (!LLVMIsConstant(val)) {
  1495. GB_ASSERT(is_local);
  1496. GB_ASSERT(LLVMIsALoadInst(val));
  1497. is_constant = false;
  1498. }
  1499. }
  1500. if (is_constant) {
  1501. res.value = llvm_const_named_struct_internal(m, struct_type, values, cast(unsigned)value_count);
  1502. LLVMTypeRef res_type = LLVMTypeOf(res.value);
  1503. GB_ASSERT(lb_sizeof(res_type) == lb_sizeof(struct_type));
  1504. return res;
  1505. } else {
  1506. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  1507. LLVMValueRef *old_values = values;
  1508. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  1509. for (isize i = 0; i < value_count; i++) {
  1510. LLVMValueRef old_value = old_values[i];
  1511. if (LLVMIsConstant(old_value)) {
  1512. new_values[i] = old_value;
  1513. } else {
  1514. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  1515. }
  1516. }
  1517. LLVMValueRef constant_value = llvm_const_named_struct_internal(m, struct_type, new_values, cast(unsigned)value_count);
  1518. GB_ASSERT(is_local);
  1519. lbProcedure *p = m->curr_procedure;
  1520. lbAddr v = lb_add_local_generated(p, res.type, true);
  1521. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, v);
  1522. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  1523. for (isize i = 0; i < value_count; i++) {
  1524. LLVMValueRef val = old_values[i];
  1525. if (!LLVMIsConstant(val)) {
  1526. LLVMValueRef dst = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), v.addr.value, cast(unsigned)i, "");
  1527. // if (LLVMIsALoadInst(val)) {
  1528. // Type *ptr_type = v.addr.type;
  1529. // i64 sz = type_size_of(type_deref(ptr_type));
  1530. // LLVMValueRef src = LLVMGetOperand(val, 0);
  1531. // lb_mem_copy_non_overlapping(p, {dst, ptr_type}, {src, ptr_type}, lb_const_int(m, t_int, sz), false);
  1532. // } else {
  1533. LLVMBuildStore(p->builder, val, dst);
  1534. // }
  1535. }
  1536. }
  1537. return lb_addr_load(p, v);
  1538. }
  1539. } else if (is_type_bit_set(type)) {
  1540. ast_node(cl, CompoundLit, value.value_compound);
  1541. if (cl->elems.count == 0) {
  1542. return lb_const_nil(m, original_type);
  1543. }
  1544. i64 sz = type_size_of(type);
  1545. if (sz == 0) {
  1546. return lb_const_nil(m, original_type);
  1547. }
  1548. BigInt bits = {};
  1549. BigInt one = {};
  1550. big_int_from_u64(&one, 1);
  1551. for_array(i, cl->elems) {
  1552. Ast *e = cl->elems[i];
  1553. GB_ASSERT(e->kind != Ast_FieldValue);
  1554. TypeAndValue tav = e->tav;
  1555. if (tav.mode != Addressing_Constant) {
  1556. continue;
  1557. }
  1558. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  1559. i64 v = big_int_to_i64(&tav.value.value_integer);
  1560. i64 lower = type->BitSet.lower;
  1561. u64 index = cast(u64)(v-lower);
  1562. BigInt bit = {};
  1563. big_int_from_u64(&bit, index);
  1564. big_int_shl(&bit, &one, &bit);
  1565. big_int_or(&bits, &bits, &bit);
  1566. }
  1567. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  1568. return res;
  1569. } else if (is_type_matrix(type)) {
  1570. ast_node(cl, CompoundLit, value.value_compound);
  1571. Type *elem_type = type->Matrix.elem;
  1572. isize elem_count = cl->elems.count;
  1573. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1574. return lb_const_nil(m, original_type);
  1575. }
  1576. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  1577. i64 total_count = matrix_type_total_internal_elems(type);
  1578. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1579. if (cl->elems[0]->kind == Ast_FieldValue) {
  1580. for_array(j, cl->elems) {
  1581. Ast *elem = cl->elems[j];
  1582. ast_node(fv, FieldValue, elem);
  1583. if (is_ast_range(fv->field)) {
  1584. ast_node(ie, BinaryExpr, fv->field);
  1585. TypeAndValue lo_tav = ie->left->tav;
  1586. TypeAndValue hi_tav = ie->right->tav;
  1587. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1588. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1589. TokenKind op = ie->op.kind;
  1590. i64 lo = exact_value_to_i64(lo_tav.value);
  1591. i64 hi = exact_value_to_i64(hi_tav.value);
  1592. if (op != Token_RangeHalf) {
  1593. hi += 1;
  1594. }
  1595. GB_ASSERT(0 <= lo && lo <= max_count);
  1596. GB_ASSERT(0 <= hi && hi <= max_count);
  1597. GB_ASSERT(lo <= hi);
  1598. TypeAndValue tav = fv->value->tav;
  1599. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1600. for (i64 k = lo; k < hi; k++) {
  1601. i64 offset = matrix_row_major_index_to_offset(type, k);
  1602. GB_ASSERT(values[offset] == nullptr);
  1603. values[offset] = val;
  1604. }
  1605. } else {
  1606. TypeAndValue index_tav = fv->field->tav;
  1607. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1608. i64 index = exact_value_to_i64(index_tav.value);
  1609. GB_ASSERT(index < max_count);
  1610. TypeAndValue tav = fv->value->tav;
  1611. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1612. i64 offset = matrix_row_major_index_to_offset(type, index);
  1613. GB_ASSERT(values[offset] == nullptr);
  1614. values[offset] = val;
  1615. }
  1616. }
  1617. for (i64 i = 0; i < total_count; i++) {
  1618. if (values[i] == nullptr) {
  1619. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1620. }
  1621. }
  1622. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1623. return res;
  1624. } else {
  1625. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  1626. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1627. for_array(i, cl->elems) {
  1628. TypeAndValue tav = cl->elems[i]->tav;
  1629. GB_ASSERT(tav.mode != Addressing_Invalid);
  1630. i64 offset = 0;
  1631. offset = matrix_row_major_index_to_offset(type, i);
  1632. values[offset] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1633. }
  1634. for (isize i = 0; i < total_count; i++) {
  1635. if (values[i] == nullptr) {
  1636. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1637. }
  1638. }
  1639. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1640. return res;
  1641. }
  1642. } else {
  1643. return lb_const_nil(m, original_type);
  1644. }
  1645. break;
  1646. case ExactValue_Procedure:
  1647. GB_PANIC("handled earlier");
  1648. break;
  1649. case ExactValue_Typeid:
  1650. return lb_typeid(m, value.value_typeid);
  1651. }
  1652. return lb_const_nil(m, original_type);
  1653. }