llvm_backend_const.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. LLVMValueRef llvm_const_pad_to_size(lbModule *m, LLVMValueRef val, LLVMTypeRef dst_ty) {
  468. LLVMContextRef ctx = m->ctx;
  469. LLVMTargetDataRef td = LLVMGetModuleDataLayout(m->mod);
  470. LLVMTypeRef src_ty = LLVMTypeOf(val);
  471. unsigned src_bits = (unsigned)LLVMSizeOfTypeInBits(td, src_ty);
  472. unsigned dst_bits = (unsigned)LLVMSizeOfTypeInBits(td, dst_ty);
  473. LLVMValueRef as_int = nullptr;
  474. LLVMTypeKind src_kind = LLVMGetTypeKind(src_ty);
  475. if (src_kind == LLVMIntegerTypeKind ||
  476. src_kind == LLVMFloatTypeKind ||
  477. src_kind == LLVMDoubleTypeKind ||
  478. src_kind == LLVMPointerTypeKind ||
  479. src_kind == LLVMVectorTypeKind) {
  480. LLVMTypeRef src_int_ty = LLVMIntTypeInContext(ctx, src_bits);
  481. as_int = LLVMConstBitCast(val, src_int_ty);
  482. } else if (src_kind == LLVMArrayTypeKind) {
  483. unsigned elem_count = LLVMGetArrayLength(src_ty);
  484. LLVMTypeRef elem_ty = LLVMGetElementType(src_ty);
  485. unsigned elem_bits = (unsigned)LLVMSizeOfTypeInBits(td, elem_ty);
  486. LLVMTypeRef src_int_ty = LLVMIntTypeInContext(ctx, src_bits);
  487. as_int = LLVMConstInt(src_int_ty, 0, false);
  488. for (unsigned i = 0; i < elem_count; i++) {
  489. LLVMValueRef elem = llvm_const_extract_value(m, val, i);
  490. LLVMTypeRef elem_int_ty = LLVMIntTypeInContext(ctx, elem_bits);
  491. LLVMValueRef elem_int = llvm_const_pad_to_size(m, elem, elem_int_ty);
  492. LLVMValueRef shifted = llvm_const_shl(m, llvm_const_zext(m, elem_int, src_int_ty), LLVMConstInt(src_int_ty, i * elem_bits, false));
  493. as_int = llvm_const_or(m, as_int, shifted);
  494. }
  495. } else {
  496. gb_printf_err("unsupported const_pad source type: %s\n", LLVMPrintTypeToString(src_ty));
  497. return nullptr;
  498. }
  499. if (src_bits != dst_bits) {
  500. LLVMTypeRef dst_int_ty = LLVMIntTypeInContext(ctx, dst_bits);
  501. if (src_bits < dst_bits) {
  502. as_int = llvm_const_zext(m, as_int, dst_int_ty);
  503. } else {
  504. as_int = LLVMConstTrunc(as_int, dst_int_ty);
  505. }
  506. }
  507. LLVMTypeKind dst_kind = LLVMGetTypeKind(dst_ty);
  508. if (dst_kind == LLVMIntegerTypeKind ||
  509. dst_kind == LLVMFloatTypeKind ||
  510. dst_kind == LLVMDoubleTypeKind ||
  511. dst_kind == LLVMPointerTypeKind ||
  512. dst_kind == LLVMVectorTypeKind) {
  513. return LLVMConstBitCast(as_int, dst_ty);
  514. } else if (dst_kind == LLVMArrayTypeKind) {
  515. unsigned elem_count = LLVMGetArrayLength(dst_ty);
  516. LLVMTypeRef elem_ty = LLVMGetElementType(dst_ty);
  517. unsigned elem_bits = (unsigned)LLVMSizeOfTypeInBits(td, elem_ty);
  518. LLVMValueRef *elems = temporary_alloc_array<LLVMValueRef>(elem_count);
  519. LLVMTypeRef as_int_ty = LLVMTypeOf(as_int);
  520. for (unsigned i = 0; i < elem_count; i++) {
  521. LLVMValueRef shifted = llvm_const_lshr(m, as_int, LLVMConstInt(as_int_ty, i * elem_bits, false));
  522. LLVMTypeRef elem_int_ty = LLVMIntTypeInContext(ctx, elem_bits);
  523. LLVMValueRef trunc = LLVMConstTrunc(shifted, elem_int_ty);
  524. elems[i] = llvm_const_pad_to_size(m, trunc, elem_ty);
  525. }
  526. return LLVMConstArray(elem_ty, elems, elem_count);
  527. }
  528. gb_printf_err("unsupported const_pad destination type: %s\n", LLVMPrintTypeToString(dst_ty));
  529. return nullptr;
  530. }
  531. gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lbConstContext cc, Type *value_type) {
  532. if (cc.allow_local) {
  533. cc.is_rodata = false;
  534. }
  535. LLVMContextRef ctx = m->ctx;
  536. type = default_type(type);
  537. Type *original_type = type;
  538. lbValue res = {};
  539. res.type = original_type;
  540. type = core_type(type);
  541. value = convert_exact_value_for_type(value, type);
  542. bool is_local = cc.allow_local && m->curr_procedure != nullptr;
  543. if (is_type_union(type) && is_type_union_constantable(type)) {
  544. Type *bt = base_type(type);
  545. GB_ASSERT(bt->kind == Type_Union);
  546. if (bt->Union.variants.count == 0) {
  547. return lb_const_nil(m, original_type);
  548. } else if (bt->Union.variants.count == 1) {
  549. Type *t = bt->Union.variants[0];
  550. lbValue cv = lb_const_value(m, t, value, cc);
  551. GB_ASSERT(LLVMIsConstant(cv.value));
  552. LLVMTypeRef llvm_type = lb_type(m, original_type);
  553. if (is_type_union_maybe_pointer(type)) {
  554. LLVMValueRef values[1] = {cv.value};
  555. res.value = llvm_const_named_struct_internal(m, llvm_type, values, 1);
  556. res.type = original_type;
  557. return res;
  558. } else {
  559. unsigned tag_value = 1;
  560. if (bt->Union.kind == UnionType_no_nil) {
  561. tag_value = 0;
  562. }
  563. LLVMValueRef tag = LLVMConstInt(LLVMStructGetTypeAtIndex(llvm_type, 1), tag_value, false);
  564. LLVMValueRef padding = nullptr;
  565. LLVMValueRef values[3] = {cv.value, tag, padding};
  566. isize value_count = 2;
  567. if (LLVMCountStructElementTypes(llvm_type) > 2) {
  568. value_count = 3;
  569. padding = LLVMConstNull(LLVMStructGetTypeAtIndex(llvm_type, 2));
  570. }
  571. res.value = llvm_const_named_struct_internal(m, llvm_type, values, value_count);
  572. res.type = original_type;
  573. return res;
  574. }
  575. } else {
  576. if (value_type == nullptr) {
  577. if (value.kind == ExactValue_Compound) {
  578. ast_node(cl, CompoundLit, value.value_compound);
  579. if (cl->elems.count == 0) {
  580. return lb_const_nil(m, original_type);
  581. }
  582. } else if (value.kind == ExactValue_Invalid) {
  583. return lb_const_nil(m, original_type);
  584. }
  585. }
  586. GB_ASSERT_MSG(value_type != nullptr, "%s :: %s", type_to_string(original_type), exact_value_to_string(value));
  587. i64 block_size = bt->Union.variant_block_size;
  588. if (are_types_identical(value_type, original_type)) {
  589. if (value.kind == ExactValue_Compound) {
  590. ast_node(cl, CompoundLit, value.value_compound);
  591. if (cl->elems.count == 0) {
  592. return lb_const_nil(m, original_type);
  593. }
  594. } else if (value.kind == ExactValue_Invalid) {
  595. return lb_const_nil(m, original_type);
  596. }
  597. GB_PANIC("%s vs %s", type_to_string(value_type), type_to_string(original_type));
  598. }
  599. lbValue cv = lb_const_value(m, value_type, value, cc, value_type);
  600. Type *variant_type = cv.type;
  601. LLVMValueRef values[3] = {};
  602. unsigned value_count = 0;
  603. LLVMTypeRef block_type = lb_type_internal_union_block_type(m, bt);
  604. values[value_count++] = llvm_const_pad_to_size(m, cv.value, block_type);
  605. Type *tag_type = union_tag_type(bt);
  606. LLVMTypeRef llvm_tag_type = lb_type(m, tag_type);
  607. i64 tag_index = union_variant_index(bt, variant_type);
  608. GB_ASSERT(tag_index >= 0);
  609. values[value_count++] = LLVMConstInt(llvm_tag_type, tag_index, false);
  610. i64 used_size = block_size + type_size_of(tag_type);
  611. i64 union_size = type_size_of(bt);
  612. i64 padding = union_size - used_size;
  613. if (padding > 0) {
  614. LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, 1);
  615. values[value_count++] = LLVMConstNull(padding_type);
  616. }
  617. res.value = LLVMConstStructInContext(m->ctx, values, value_count, true);
  618. return res;
  619. }
  620. }
  621. if (value.kind == ExactValue_Procedure) {
  622. lbValue res = {};
  623. Ast *expr = unparen_expr(value.value_procedure);
  624. GB_ASSERT(expr != nullptr);
  625. if (expr->kind == Ast_ProcLit) {
  626. res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
  627. } else {
  628. Entity *e = entity_from_expr(expr);
  629. res = lb_find_procedure_value_from_entity(m, e);
  630. }
  631. if (res.value == nullptr) {
  632. // This is an unspecialized polymorphic procedure, return nil or dummy value
  633. return lb_const_nil(m, original_type);
  634. }
  635. GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
  636. if (LLVMGetIntrinsicID(res.value) == 0) {
  637. // NOTE(bill): do not cast intrinsics as they are not really procedures that can be casted
  638. res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
  639. }
  640. return res;
  641. }
  642. // NOTE(bill): This has to be done AFTER the union stuff
  643. if (value.kind == ExactValue_Invalid) {
  644. return lb_const_nil(m, original_type);
  645. }
  646. if (value.kind == ExactValue_Typeid) {
  647. return lb_typeid(m, value.value_typeid);
  648. }
  649. if (value.kind == ExactValue_Compound) {
  650. ast_node(cl, CompoundLit, value.value_compound);
  651. if (cl->elems.count == 0) {
  652. return lb_const_nil(m, original_type);
  653. }
  654. }
  655. // GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
  656. if (is_type_slice(type)) {
  657. if (value.kind == ExactValue_String) {
  658. GB_ASSERT(is_type_slice(type));
  659. res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value;
  660. return res;
  661. } else if (value.kind == ExactValue_String16) {
  662. GB_ASSERT(is_type_slice(type));
  663. res.value = lb_find_or_add_entity_string16_slice_with_type(m, value.value_string16, original_type).value;
  664. return res;
  665. } else {
  666. ast_node(cl, CompoundLit, value.value_compound);
  667. isize count = cl->elems.count;
  668. if (count == 0) {
  669. return lb_const_nil(m, type);
  670. }
  671. count = gb_max(cast(isize)cl->max_count, count);
  672. Type *elem = base_type(type)->Slice.elem;
  673. Type *t = alloc_type_array(elem, count);
  674. lbValue backing_array = lb_const_value(m, t, value, cc, nullptr);
  675. LLVMValueRef array_data = nullptr;
  676. if (is_local) {
  677. // NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
  678. // its backing data on the stack
  679. lbProcedure *p = m->curr_procedure;
  680. LLVMTypeRef llvm_type = lb_type(m, t);
  681. unsigned alignment = cast(unsigned)gb_max(type_align_of(t), 16);
  682. bool do_local_copy = false;
  683. if (do_local_copy) {
  684. array_data = llvm_alloca(p, llvm_type, alignment);
  685. LLVMValueRef local_copy = llvm_alloca(p, LLVMTypeOf(backing_array.value), alignment);
  686. LLVMBuildStore(p->builder, backing_array.value, local_copy);
  687. LLVMBuildMemCpy(p->builder,
  688. array_data, alignment,
  689. local_copy, alignment,
  690. LLVMConstInt(lb_type(m, t_int), type_size_of(t), false)
  691. );
  692. } else {
  693. array_data = llvm_alloca(p, LLVMTypeOf(backing_array.value), alignment);
  694. LLVMBuildStore(p->builder, backing_array.value, array_data);
  695. array_data = LLVMBuildPointerCast(p->builder, array_data, LLVMPointerType(llvm_type, 0), "");
  696. }
  697. {
  698. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  699. LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
  700. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  701. lbAddr slice = lb_add_local_generated(p, original_type, false);
  702. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
  703. lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
  704. return lb_addr_load(p, slice);
  705. }
  706. } else {
  707. u32 id = m->global_array_index.fetch_add(1);
  708. gbString str = gb_string_make(temporary_allocator(), "csba$");
  709. str = gb_string_appendc(str, m->module_name);
  710. str = gb_string_append_fmt(str, "$%x", id);
  711. String name = make_string(cast(u8 const *)str, gb_string_length(str));
  712. Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
  713. array_data = LLVMAddGlobal(m->mod, LLVMTypeOf(backing_array.value), str);
  714. LLVMSetInitializer(array_data, backing_array.value);
  715. if (cc.link_section.len > 0) {
  716. LLVMSetSection(array_data, alloc_cstring(permanent_allocator(), cc.link_section));
  717. }
  718. if (cc.is_rodata) {
  719. LLVMSetGlobalConstant(array_data, true);
  720. }
  721. lbValue g = {};
  722. g.value = LLVMConstPointerCast(array_data, LLVMPointerType(lb_type(m, t), 0));
  723. g.type = t;
  724. lb_add_entity(m, e, g);
  725. lb_add_member(m, name, g);
  726. {
  727. LLVMValueRef ptr = g.value;
  728. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
  729. LLVMValueRef values[2] = {ptr, len};
  730. res.value = llvm_const_named_struct(m, original_type, values, 2);
  731. return res;
  732. }
  733. }
  734. }
  735. } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
  736. if (is_type_rune_array(type)) {
  737. i64 count = type->Array.count;
  738. Type *elem = type->Array.elem;
  739. LLVMTypeRef et = lb_type(m, elem);
  740. Rune rune;
  741. isize offset = 0;
  742. isize width = 1;
  743. String s = value.value_string;
  744. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  745. for (i64 i = 0; i < count && offset < s.len; i++) {
  746. width = utf8_decode(s.text+offset, s.len-offset, &rune);
  747. offset += width;
  748. elems[i] = LLVMConstInt(et, rune, true);
  749. }
  750. GB_ASSERT(offset == s.len);
  751. res.value = llvm_const_array(m, et, elems, cast(unsigned)count);
  752. return res;
  753. }
  754. // NOTE(bill, 2021-10-07): Allow for array programming value constants
  755. Type *core_elem = core_array_type(type);
  756. return lb_const_value(m, core_elem, value, cc);
  757. } else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
  758. GB_ASSERT(type->Array.count == value.value_string.len);
  759. LLVMValueRef data = LLVMConstStringInContext(ctx,
  760. cast(char const *)value.value_string.text,
  761. cast(unsigned)value.value_string.len,
  762. true /*DontNullTerminate*/);
  763. res.value = data;
  764. return res;
  765. } else if (is_type_array(type) &&
  766. value.kind != ExactValue_Invalid &&
  767. value.kind != ExactValue_String &&
  768. value.kind != ExactValue_Compound) {
  769. i64 count = type->Array.count;
  770. Type *elem = type->Array.elem;
  771. lbValue single_elem = lb_const_value(m, elem, value, cc);
  772. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
  773. for (i64 i = 0; i < count; i++) {
  774. elems[i] = single_elem.value;
  775. }
  776. res.value = llvm_const_array(m, lb_type(m, elem), elems, cast(unsigned)count);
  777. return res;
  778. } else if (is_type_matrix(type) &&
  779. value.kind != ExactValue_Invalid &&
  780. value.kind != ExactValue_Compound) {
  781. i64 row = type->Matrix.row_count;
  782. i64 column = type->Matrix.column_count;
  783. GB_ASSERT(row == column);
  784. Type *elem = type->Matrix.elem;
  785. lbValue single_elem = lb_const_value(m, elem, value, cc);
  786. single_elem.value = llvm_const_cast(m, single_elem.value, lb_type(m, elem), /*failure_*/nullptr);
  787. i64 total_elem_count = matrix_type_total_internal_elems(type);
  788. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)total_elem_count);
  789. for (i64 i = 0; i < row; i++) {
  790. elems[matrix_indices_to_offset(type, i, i)] = single_elem.value;
  791. }
  792. for (i64 i = 0; i < total_elem_count; i++) {
  793. if (elems[i] == nullptr) {
  794. elems[i] = LLVMConstNull(lb_type(m, elem));
  795. }
  796. }
  797. res.value = LLVMConstArray(lb_type(m, elem), elems, cast(unsigned)total_elem_count);
  798. return res;
  799. } else if (is_type_simd_vector(type) &&
  800. value.kind != ExactValue_Invalid &&
  801. value.kind != ExactValue_Compound) {
  802. i64 count = type->SimdVector.count;
  803. Type *elem = type->SimdVector.elem;
  804. lbValue single_elem = lb_const_value(m, elem, value, cc);
  805. single_elem.value = llvm_const_cast(m, single_elem.value, lb_type(m, elem), /*failure_*/nullptr);
  806. LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
  807. for (i64 i = 0; i < count; i++) {
  808. elems[i] = single_elem.value;
  809. }
  810. res.value = LLVMConstVector(elems, cast(unsigned)count);
  811. return res;
  812. }
  813. switch (value.kind) {
  814. case ExactValue_Invalid:
  815. res.value = LLVMConstNull(lb_type(m, original_type));
  816. return res;
  817. case ExactValue_Bool:
  818. res.value = LLVMConstInt(lb_type(m, original_type), value.value_bool, false);
  819. return res;
  820. case ExactValue_String:
  821. {
  822. bool custom_link_section = cc.link_section.len > 0;
  823. LLVMValueRef ptr = nullptr;
  824. lbValue res = {};
  825. res.type = default_type(original_type);
  826. isize len = value.value_string.len;
  827. if (is_type_string16(res.type) || is_type_cstring16(res.type)) {
  828. TEMPORARY_ALLOCATOR_GUARD();
  829. String16 s16 = string_to_string16(temporary_allocator(), value.value_string);
  830. len = s16.len;
  831. ptr = lb_find_or_add_entity_string16_ptr(m, s16, custom_link_section);
  832. } else {
  833. ptr = lb_find_or_add_entity_string_ptr(m, value.value_string, custom_link_section);
  834. }
  835. if (custom_link_section) {
  836. LLVMSetSection(ptr, alloc_cstring(permanent_allocator(), cc.link_section));
  837. }
  838. if (is_type_cstring(res.type) || is_type_cstring16(res.type)) {
  839. res.value = ptr;
  840. } else {
  841. if (len == 0) {
  842. if (is_type_string16(res.type)) {
  843. ptr = LLVMConstNull(lb_type(m, t_u16_ptr));
  844. } else {
  845. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  846. }
  847. }
  848. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), len, true);
  849. GB_ASSERT(is_type_string(original_type));
  850. if (is_type_string16(res.type)) {
  851. res.value = llvm_const_string16_internal(m, original_type, ptr, str_len);
  852. } else {
  853. res.value = llvm_const_string_internal(m, original_type, ptr, str_len);
  854. }
  855. }
  856. return res;
  857. }
  858. case ExactValue_String16:
  859. {
  860. GB_ASSERT(is_type_string16(res.type) || is_type_cstring16(res.type));
  861. bool custom_link_section = cc.link_section.len > 0;
  862. LLVMValueRef ptr = lb_find_or_add_entity_string16_ptr(m, value.value_string16, custom_link_section);
  863. lbValue res = {};
  864. res.type = default_type(original_type);
  865. if (custom_link_section) {
  866. LLVMSetSection(ptr, alloc_cstring(permanent_allocator(), cc.link_section));
  867. }
  868. if (is_type_cstring16(res.type)) {
  869. res.value = ptr;
  870. } else {
  871. if (value.value_string16.len == 0) {
  872. ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
  873. }
  874. LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string16.len, true);
  875. GB_ASSERT(is_type_string(original_type));
  876. res.value = llvm_const_string16_internal(m, original_type, ptr, str_len);
  877. }
  878. return res;
  879. }
  880. case ExactValue_Integer:
  881. if (is_type_pointer(type) || is_type_multi_pointer(type) || is_type_proc(type)) {
  882. LLVMTypeRef t = lb_type(m, original_type);
  883. LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
  884. res.value = LLVMConstIntToPtr(i, t);
  885. } else {
  886. res.value = lb_big_int_to_llvm(m, original_type, &value.value_integer);
  887. }
  888. return res;
  889. case ExactValue_Float:
  890. if (is_type_different_to_arch_endianness(type)) {
  891. if (type->Basic.kind == Basic_f32le || type->Basic.kind == Basic_f32be) {
  892. f32 f = static_cast<float>(value.value_float);
  893. u32 u = bit_cast<u32>(f);
  894. u = gb_endian_swap32(u);
  895. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f32>(u));
  896. } else if (type->Basic.kind == Basic_f16le || type->Basic.kind == Basic_f16be) {
  897. f32 f = static_cast<float>(value.value_float);
  898. u16 u = f32_to_f16(f);
  899. u = gb_endian_swap16(u);
  900. res.value = LLVMConstReal(lb_type(m, original_type), f16_to_f32(u));
  901. } else {
  902. u64 u = bit_cast<u64>(value.value_float);
  903. u = gb_endian_swap64(u);
  904. res.value = LLVMConstReal(lb_type(m, original_type), bit_cast<f64>(u));
  905. }
  906. } else {
  907. res.value = LLVMConstReal(lb_type(m, original_type), value.value_float);
  908. }
  909. return res;
  910. case ExactValue_Complex:
  911. {
  912. LLVMValueRef values[2] = {};
  913. switch (8*type_size_of(type)) {
  914. case 32:
  915. values[0] = lb_const_f16(m, cast(f32)value.value_complex->real);
  916. values[1] = lb_const_f16(m, cast(f32)value.value_complex->imag);
  917. break;
  918. case 64:
  919. values[0] = lb_const_f32(m, cast(f32)value.value_complex->real);
  920. values[1] = lb_const_f32(m, cast(f32)value.value_complex->imag);
  921. break;
  922. case 128:
  923. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->real);
  924. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_complex->imag);
  925. break;
  926. }
  927. res.value = llvm_const_named_struct(m, original_type, values, 2);
  928. return res;
  929. }
  930. break;
  931. case ExactValue_Quaternion:
  932. {
  933. LLVMValueRef values[4] = {};
  934. switch (8*type_size_of(type)) {
  935. case 64:
  936. // @QuaternionLayout
  937. values[3] = lb_const_f16(m, cast(f32)value.value_quaternion->real);
  938. values[0] = lb_const_f16(m, cast(f32)value.value_quaternion->imag);
  939. values[1] = lb_const_f16(m, cast(f32)value.value_quaternion->jmag);
  940. values[2] = lb_const_f16(m, cast(f32)value.value_quaternion->kmag);
  941. break;
  942. case 128:
  943. // @QuaternionLayout
  944. values[3] = lb_const_f32(m, cast(f32)value.value_quaternion->real);
  945. values[0] = lb_const_f32(m, cast(f32)value.value_quaternion->imag);
  946. values[1] = lb_const_f32(m, cast(f32)value.value_quaternion->jmag);
  947. values[2] = lb_const_f32(m, cast(f32)value.value_quaternion->kmag);
  948. break;
  949. case 256:
  950. // @QuaternionLayout
  951. values[3] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->real);
  952. values[0] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->imag);
  953. values[1] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->jmag);
  954. values[2] = LLVMConstReal(lb_type(m, t_f64), value.value_quaternion->kmag);
  955. break;
  956. }
  957. res.value = llvm_const_named_struct(m, original_type, values, 4);
  958. return res;
  959. }
  960. break;
  961. case ExactValue_Pointer:
  962. res.value = LLVMConstIntToPtr(LLVMConstInt(lb_type(m, t_uintptr), value.value_pointer, false), lb_type(m, original_type));
  963. return res;
  964. case ExactValue_Compound:
  965. if (is_type_slice(type)) {
  966. return lb_const_value(m, type, value, cc);
  967. } else if (is_type_soa_struct(type)) {
  968. GB_ASSERT(type->kind == Type_Struct);
  969. GB_ASSERT(type->Struct.soa_kind == StructSoa_Fixed);
  970. ast_node(cl, CompoundLit, value.value_compound);
  971. Type *elem_type = type->Struct.soa_elem;
  972. isize elem_count = cl->elems.count;
  973. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  974. return lb_const_nil(m, original_type);
  975. }
  976. if (cl->elems[0]->kind == Ast_FieldValue) {
  977. TEMPORARY_ALLOCATOR_GUARD();
  978. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  979. isize elem_count = cast(isize)type->Struct.soa_count;
  980. LLVMValueRef *aos_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  981. isize value_index = 0;
  982. for (i64 i = 0; i < elem_count; i++) {
  983. bool found = false;
  984. for (isize j = 0; j < elem_count; j++) {
  985. Ast *elem = cl->elems[j];
  986. ast_node(fv, FieldValue, elem);
  987. if (is_ast_range(fv->field)) {
  988. ast_node(ie, BinaryExpr, fv->field);
  989. TypeAndValue lo_tav = ie->left->tav;
  990. TypeAndValue hi_tav = ie->right->tav;
  991. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  992. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  993. TokenKind op = ie->op.kind;
  994. i64 lo = exact_value_to_i64(lo_tav.value);
  995. i64 hi = exact_value_to_i64(hi_tav.value);
  996. if (op != Token_RangeHalf) {
  997. hi += 1;
  998. }
  999. if (lo == i) {
  1000. TypeAndValue tav = fv->value->tav;
  1001. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1002. for (i64 k = lo; k < hi; k++) {
  1003. aos_values[value_index++] = val;
  1004. }
  1005. found = true;
  1006. i += (hi-lo-1);
  1007. break;
  1008. }
  1009. } else {
  1010. TypeAndValue index_tav = fv->field->tav;
  1011. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1012. i64 index = exact_value_to_i64(index_tav.value);
  1013. if (index == i) {
  1014. TypeAndValue tav = fv->value->tav;
  1015. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1016. aos_values[value_index++] = val;
  1017. found = true;
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. if (!found) {
  1023. aos_values[value_index++] = nullptr;
  1024. }
  1025. }
  1026. isize field_count = type->Struct.fields.count;
  1027. LLVMValueRef *soa_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, field_count);
  1028. for (isize i = 0; i < field_count; i++) {
  1029. TEMPORARY_ALLOCATOR_GUARD();
  1030. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1031. Entity *f = type->Struct.fields[i];
  1032. Type *array_type = f->type;
  1033. GB_ASSERT(array_type->kind == Type_Array);
  1034. Type *field_type = array_type->Array.elem;
  1035. for (isize j = 0; j < elem_count; j++) {
  1036. LLVMValueRef v = aos_values[j];
  1037. if (v != nullptr) {
  1038. values[j] = llvm_const_extract_value(m, v, cast(unsigned)i);
  1039. } else {
  1040. values[j] = LLVMConstNull(lb_type(m, field_type));
  1041. }
  1042. }
  1043. soa_values[i] = lb_build_constant_array_values(m, array_type, field_type, elem_count, values, cc);
  1044. }
  1045. res.value = llvm_const_named_struct(m, type, soa_values, field_count);
  1046. return res;
  1047. } else {
  1048. GB_ASSERT_MSG(elem_count == type->Struct.soa_count, "%td != %td", elem_count, type->Struct.soa_count);
  1049. TEMPORARY_ALLOCATOR_GUARD();
  1050. isize elem_count = cast(isize)type->Struct.soa_count;
  1051. LLVMValueRef *aos_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1052. for (isize i = 0; i < elem_count; i++) {
  1053. TypeAndValue tav = cl->elems[i]->tav;
  1054. GB_ASSERT(tav.mode != Addressing_Invalid);
  1055. aos_values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1056. }
  1057. for (isize i = elem_count; i < type->Struct.soa_count; i++) {
  1058. aos_values[i] = nullptr;
  1059. }
  1060. isize field_count = type->Struct.fields.count;
  1061. LLVMValueRef *soa_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, field_count);
  1062. for (isize i = 0; i < field_count; i++) {
  1063. TEMPORARY_ALLOCATOR_GUARD();
  1064. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, elem_count);
  1065. Entity *f = type->Struct.fields[i];
  1066. Type *array_type = f->type;
  1067. GB_ASSERT(array_type->kind == Type_Array);
  1068. Type *field_type = array_type->Array.elem;
  1069. for (isize j = 0; j < elem_count; j++) {
  1070. LLVMValueRef v = aos_values[j];
  1071. if (v != nullptr) {
  1072. values[j] = llvm_const_extract_value(m, v, cast(unsigned)i);
  1073. } else {
  1074. values[j] = LLVMConstNull(lb_type(m, field_type));
  1075. }
  1076. }
  1077. soa_values[i] = lb_build_constant_array_values(m, array_type, field_type, elem_count, values, cc);
  1078. }
  1079. res.value = llvm_const_named_struct(m, type, soa_values, field_count);
  1080. return res;
  1081. }
  1082. } else if (is_type_array(type)) {
  1083. ast_node(cl, CompoundLit, value.value_compound);
  1084. Type *elem_type = type->Array.elem;
  1085. isize elem_count = cl->elems.count;
  1086. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1087. return lb_const_nil(m, original_type);
  1088. }
  1089. if (cl->elems[0]->kind == Ast_FieldValue) {
  1090. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1091. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1092. isize value_index = 0;
  1093. for (i64 i = 0; i < type->Array.count; i++) {
  1094. bool found = false;
  1095. for (isize j = 0; j < elem_count; j++) {
  1096. Ast *elem = cl->elems[j];
  1097. ast_node(fv, FieldValue, elem);
  1098. if (is_ast_range(fv->field)) {
  1099. ast_node(ie, BinaryExpr, fv->field);
  1100. TypeAndValue lo_tav = ie->left->tav;
  1101. TypeAndValue hi_tav = ie->right->tav;
  1102. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1103. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1104. TokenKind op = ie->op.kind;
  1105. i64 lo = exact_value_to_i64(lo_tav.value);
  1106. i64 hi = exact_value_to_i64(hi_tav.value);
  1107. if (op != Token_RangeHalf) {
  1108. hi += 1;
  1109. }
  1110. if (lo == i) {
  1111. TypeAndValue tav = fv->value->tav;
  1112. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1113. for (i64 k = lo; k < hi; k++) {
  1114. values[value_index++] = val;
  1115. }
  1116. found = true;
  1117. i += (hi-lo-1);
  1118. break;
  1119. }
  1120. } else {
  1121. TypeAndValue index_tav = fv->field->tav;
  1122. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1123. i64 index = exact_value_to_i64(index_tav.value);
  1124. if (index == i) {
  1125. TypeAndValue tav = fv->value->tav;
  1126. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1127. values[value_index++] = val;
  1128. found = true;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. if (!found) {
  1134. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1135. }
  1136. }
  1137. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1138. return res;
  1139. } else if (are_types_identical(value.value_compound->tav.type, elem_type)) {
  1140. // Compound is of array item type; expand its value to all items in array.
  1141. LLVMValueRef* values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1142. for (isize i = 0; i < type->Array.count; i++) {
  1143. values[i] = lb_const_value(m, elem_type, value, cc, elem_type).value;
  1144. }
  1145. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1146. return res;
  1147. } else {
  1148. // Assume that compound value is an array literal
  1149. GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
  1150. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
  1151. for (isize i = 0; i < elem_count; i++) {
  1152. TypeAndValue tav = cl->elems[i]->tav;
  1153. GB_ASSERT(tav.mode != Addressing_Invalid);
  1154. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1155. }
  1156. for (isize i = elem_count; i < type->Array.count; i++) {
  1157. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1158. }
  1159. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc);
  1160. return res;
  1161. }
  1162. } else if (is_type_enumerated_array(type)) {
  1163. ast_node(cl, CompoundLit, value.value_compound);
  1164. Type *elem_type = type->EnumeratedArray.elem;
  1165. isize elem_count = cl->elems.count;
  1166. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1167. return lb_const_nil(m, original_type);
  1168. }
  1169. if (cl->elems[0]->kind == Ast_FieldValue) {
  1170. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1171. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  1172. isize value_index = 0;
  1173. i64 total_lo = exact_value_to_i64(*type->EnumeratedArray.min_value);
  1174. i64 total_hi = exact_value_to_i64(*type->EnumeratedArray.max_value);
  1175. for (i64 i = total_lo; i <= total_hi; i++) {
  1176. bool found = false;
  1177. for (isize j = 0; j < elem_count; j++) {
  1178. Ast *elem = cl->elems[j];
  1179. ast_node(fv, FieldValue, elem);
  1180. if (is_ast_range(fv->field)) {
  1181. ast_node(ie, BinaryExpr, fv->field);
  1182. TypeAndValue lo_tav = ie->left->tav;
  1183. TypeAndValue hi_tav = ie->right->tav;
  1184. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1185. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1186. TokenKind op = ie->op.kind;
  1187. i64 lo = exact_value_to_i64(lo_tav.value);
  1188. i64 hi = exact_value_to_i64(hi_tav.value);
  1189. if (op != Token_RangeHalf) {
  1190. hi += 1;
  1191. }
  1192. if (lo == i) {
  1193. TypeAndValue tav = fv->value->tav;
  1194. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1195. for (i64 k = lo; k < hi; k++) {
  1196. values[value_index++] = val;
  1197. }
  1198. found = true;
  1199. i += (hi-lo-1);
  1200. break;
  1201. }
  1202. } else {
  1203. TypeAndValue index_tav = fv->field->tav;
  1204. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1205. i64 index = exact_value_to_i64(index_tav.value);
  1206. if (index == i) {
  1207. TypeAndValue tav = fv->value->tav;
  1208. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1209. values[value_index++] = val;
  1210. found = true;
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. if (!found) {
  1216. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1217. }
  1218. }
  1219. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  1220. return res;
  1221. } else {
  1222. GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
  1223. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
  1224. for (isize i = 0; i < elem_count; i++) {
  1225. TypeAndValue tav = cl->elems[i]->tav;
  1226. GB_ASSERT(tav.mode != Addressing_Invalid);
  1227. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1228. }
  1229. for (isize i = elem_count; i < type->EnumeratedArray.count; i++) {
  1230. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1231. }
  1232. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc);
  1233. return res;
  1234. }
  1235. } else if (is_type_simd_vector(type)) {
  1236. ast_node(cl, CompoundLit, value.value_compound);
  1237. Type *elem_type = type->SimdVector.elem;
  1238. isize elem_count = cl->elems.count;
  1239. if (elem_count == 0) {
  1240. return lb_const_nil(m, original_type);
  1241. }
  1242. GB_ASSERT(elem_type_can_be_constant(elem_type));
  1243. isize total_elem_count = cast(isize)type->SimdVector.count;
  1244. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
  1245. if (cl->elems[0]->kind == Ast_FieldValue) {
  1246. // TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
  1247. isize value_index = 0;
  1248. for (i64 i = 0; i < total_elem_count; i++) {
  1249. bool found = false;
  1250. for (isize j = 0; j < elem_count; j++) {
  1251. Ast *elem = cl->elems[j];
  1252. ast_node(fv, FieldValue, elem);
  1253. if (is_ast_range(fv->field)) {
  1254. ast_node(ie, BinaryExpr, fv->field);
  1255. TypeAndValue lo_tav = ie->left->tav;
  1256. TypeAndValue hi_tav = ie->right->tav;
  1257. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1258. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1259. TokenKind op = ie->op.kind;
  1260. i64 lo = exact_value_to_i64(lo_tav.value);
  1261. i64 hi = exact_value_to_i64(hi_tav.value);
  1262. if (op != Token_RangeHalf) {
  1263. hi += 1;
  1264. }
  1265. if (lo == i) {
  1266. TypeAndValue tav = fv->value->tav;
  1267. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1268. for (i64 k = lo; k < hi; k++) {
  1269. values[value_index++] = val;
  1270. }
  1271. found = true;
  1272. i += (hi-lo-1);
  1273. break;
  1274. }
  1275. } else {
  1276. TypeAndValue index_tav = fv->field->tav;
  1277. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1278. i64 index = exact_value_to_i64(index_tav.value);
  1279. if (index == i) {
  1280. TypeAndValue tav = fv->value->tav;
  1281. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1282. values[value_index++] = val;
  1283. found = true;
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. if (!found) {
  1289. values[value_index++] = LLVMConstNull(lb_type(m, elem_type));
  1290. }
  1291. }
  1292. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  1293. return res;
  1294. } else {
  1295. for (isize i = 0; i < elem_count; i++) {
  1296. TypeAndValue tav = cl->elems[i]->tav;
  1297. GB_ASSERT(tav.mode != Addressing_Invalid);
  1298. values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1299. }
  1300. LLVMTypeRef et = lb_type(m, elem_type);
  1301. for (isize i = elem_count; i < total_elem_count; i++) {
  1302. values[i] = LLVMConstNull(et);
  1303. }
  1304. for (isize i = 0; i < total_elem_count; i++) {
  1305. values[i] = llvm_const_cast(m, values[i], et, /*failure_*/nullptr);
  1306. }
  1307. res.value = LLVMConstVector(values, cast(unsigned)total_elem_count);
  1308. return res;
  1309. }
  1310. } else if (is_type_struct(type)) {
  1311. ast_node(cl, CompoundLit, value.value_compound);
  1312. if (cl->elems.count == 0) {
  1313. return lb_const_nil(m, original_type);
  1314. }
  1315. if (is_type_raw_union(type)) {
  1316. if (is_type_raw_union_constantable(type)) {
  1317. GB_ASSERT(cl->elems.count == 1);
  1318. GB_ASSERT(cl->elems[0]->kind == Ast_FieldValue);
  1319. ast_node(fv, FieldValue, cl->elems[0]);
  1320. Entity *f = entity_of_node(fv->field);
  1321. TypeAndValue tav = fv->value->tav;
  1322. if (tav.value.kind != ExactValue_Invalid) {
  1323. lbValue value = lb_const_value(m, f->type, tav.value, cc, f->type);
  1324. LLVMValueRef values[2];
  1325. unsigned value_count = 0;
  1326. values[value_count++] = value.value;
  1327. i64 union_alignment = type_align_of(type);
  1328. i64 value_alignment = type_align_of(f->type);
  1329. i64 alignment = gb_max(gb_min(value_alignment, union_alignment), 1);
  1330. i64 union_size = type_size_of(type);
  1331. i64 value_size = lb_sizeof(LLVMTypeOf(value.value));
  1332. i64 padding = union_size-value_size;
  1333. if (padding > 0) {
  1334. LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, alignment);
  1335. values[value_count++] = LLVMConstNull(padding_type);
  1336. }
  1337. LLVMValueRef res = LLVMConstStructInContext(m->ctx, values, value_count, /*packed*/padding > 0);
  1338. return {res, original_type};
  1339. }
  1340. }
  1341. return lb_const_nil(m, original_type);
  1342. }
  1343. LLVMTypeRef struct_type = lb_type(m, original_type);
  1344. auto field_remapping = lb_get_struct_remapping(m, type);
  1345. unsigned value_count = LLVMCountStructElementTypes(struct_type);
  1346. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  1347. bool *visited = gb_alloc_array(temporary_allocator(), bool, value_count);
  1348. if (cl->elems[0]->kind == Ast_FieldValue) {
  1349. isize elem_count = cl->elems.count;
  1350. for (isize i = 0; i < elem_count; i++) {
  1351. ast_node(fv, FieldValue, cl->elems[i]);
  1352. String name = fv->field->Ident.token.string;
  1353. TypeAndValue tav = fv->value->tav;
  1354. GB_ASSERT(tav.mode != Addressing_Invalid);
  1355. Selection sel = lookup_field(type, name, false);
  1356. GB_ASSERT(!sel.indirect);
  1357. Entity *f = type->Struct.fields[sel.index[0]];
  1358. i32 index = field_remapping[f->Variable.field_index];
  1359. if (elem_type_can_be_constant(f->type)) {
  1360. if (sel.index.count == 1) {
  1361. lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
  1362. LLVMTypeRef value_type = LLVMTypeOf(value.value);
  1363. GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
  1364. values[index] = value.value;
  1365. visited[index] = true;
  1366. } else {
  1367. if (!visited[index]) {
  1368. auto new_cc = cc;
  1369. new_cc.allow_local = false;
  1370. values[index] = lb_const_value(m, f->type, {}, new_cc).value;
  1371. visited[index] = true;
  1372. }
  1373. unsigned idx_list_len = cast(unsigned)sel.index.count-1;
  1374. unsigned *idx_list = gb_alloc_array(temporary_allocator(), unsigned, idx_list_len);
  1375. if (lb_is_nested_possibly_constant(type, sel, fv->value)) {
  1376. bool is_constant = true;
  1377. Type *cv_type = f->type;
  1378. for (isize j = 1; j < sel.index.count; j++) {
  1379. i32 index = sel.index[j];
  1380. Type *cvt = base_type(cv_type);
  1381. if (cvt->kind == Type_Struct) {
  1382. if (cvt->Struct.is_raw_union) {
  1383. // sanity check which should have been caught by `lb_is_nested_possibly_constant`
  1384. is_constant = false;
  1385. break;
  1386. }
  1387. cv_type = cvt->Struct.fields[index]->type;
  1388. if (is_type_struct(cvt)) {
  1389. auto cv_field_remapping = lb_get_struct_remapping(m, cvt);
  1390. unsigned remapped_index = cast(unsigned)cv_field_remapping[index];
  1391. idx_list[j-1] = remapped_index;
  1392. } else {
  1393. idx_list[j-1] = cast(unsigned)index;
  1394. }
  1395. } else if (cvt->kind == Type_Array) {
  1396. cv_type = cvt->Array.elem;
  1397. idx_list[j-1] = cast(unsigned)index;
  1398. } else {
  1399. GB_PANIC("UNKNOWN TYPE: %s", type_to_string(cv_type));
  1400. }
  1401. }
  1402. if (is_constant) {
  1403. LLVMValueRef elem_value = lb_const_value(m, tav.type, tav.value, cc, tav.type).value;
  1404. if (LLVMIsConstant(elem_value) && LLVMIsConstant(values[index])) {
  1405. values[index] = llvm_const_insert_value(m, values[index], elem_value, idx_list, idx_list_len);
  1406. } else if (is_local) {
  1407. #if 1
  1408. lbProcedure *p = m->curr_procedure;
  1409. GB_ASSERT(p != nullptr);
  1410. if (LLVMIsConstant(values[index])) {
  1411. lbAddr addr = lb_add_local_generated(p, f->type, false);
  1412. lb_addr_store(p, addr, lbValue{values[index], f->type});
  1413. values[index] = lb_addr_load(p, addr).value;
  1414. }
  1415. GB_ASSERT(LLVMIsALoadInst(values[index]));
  1416. LLVMValueRef ptr = LLVMGetOperand(values[index], 0);
  1417. LLVMValueRef *indices = gb_alloc_array(temporary_allocator(), LLVMValueRef, idx_list_len);
  1418. LLVMTypeRef lt_u32 = lb_type(m, t_u32);
  1419. for (unsigned i = 0; i < idx_list_len; i++) {
  1420. indices[i] = LLVMConstInt(lt_u32, idx_list[i], false);
  1421. }
  1422. ptr = LLVMBuildGEP2(p->builder, lb_type(m, f->type), ptr, indices, idx_list_len, "");
  1423. ptr = LLVMBuildPointerCast(p->builder, ptr, lb_type(m, alloc_type_pointer(tav.type)), "");
  1424. if (LLVMIsALoadInst(elem_value)) {
  1425. i64 sz = type_size_of(tav.type);
  1426. LLVMValueRef src = LLVMGetOperand(elem_value, 0);
  1427. lb_mem_copy_non_overlapping(p, {ptr, t_rawptr}, {src, t_rawptr}, lb_const_int(m, t_int, sz), false);
  1428. } else {
  1429. LLVMBuildStore(p->builder, elem_value, ptr);
  1430. }
  1431. #endif
  1432. is_constant = false;
  1433. } else {
  1434. is_constant = false;
  1435. }
  1436. }
  1437. }
  1438. }
  1439. }
  1440. }
  1441. } else {
  1442. for_array(i, cl->elems) {
  1443. Entity *f = type->Struct.fields[i];
  1444. TypeAndValue tav = cl->elems[i]->tav;
  1445. ExactValue val = {};
  1446. if (tav.mode != Addressing_Invalid) {
  1447. val = tav.value;
  1448. }
  1449. i32 index = field_remapping[f->Variable.field_index];
  1450. if (elem_type_can_be_constant(f->type)) {
  1451. lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
  1452. LLVMTypeRef value_type = LLVMTypeOf(value.value);
  1453. GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
  1454. values[index] = value.value;
  1455. visited[index] = true;
  1456. }
  1457. }
  1458. }
  1459. for (isize i = 0; i < value_count; i++) {
  1460. if (!visited[i]) {
  1461. GB_ASSERT(values[i] == nullptr);
  1462. LLVMTypeRef type = LLVMStructGetTypeAtIndex(struct_type, cast(unsigned)i);
  1463. values[i] = LLVMConstNull(type);
  1464. }
  1465. }
  1466. bool is_constant = true;
  1467. for (isize i = 0; i < value_count; i++) {
  1468. LLVMValueRef val = values[i];
  1469. if (!LLVMIsConstant(val)) {
  1470. GB_ASSERT(is_local);
  1471. GB_ASSERT(LLVMIsALoadInst(val));
  1472. is_constant = false;
  1473. }
  1474. }
  1475. if (is_constant) {
  1476. res.value = llvm_const_named_struct_internal(m, struct_type, values, cast(unsigned)value_count);
  1477. LLVMTypeRef res_type = LLVMTypeOf(res.value);
  1478. GB_ASSERT(lb_sizeof(res_type) == lb_sizeof(struct_type));
  1479. return res;
  1480. } else {
  1481. // TODO(bill): THIS IS HACK BUT IT WORKS FOR WHAT I NEED
  1482. LLVMValueRef *old_values = values;
  1483. LLVMValueRef *new_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, value_count);
  1484. for (isize i = 0; i < value_count; i++) {
  1485. LLVMValueRef old_value = old_values[i];
  1486. if (LLVMIsConstant(old_value)) {
  1487. new_values[i] = old_value;
  1488. } else {
  1489. new_values[i] = LLVMConstNull(LLVMTypeOf(old_value));
  1490. }
  1491. }
  1492. LLVMValueRef constant_value = llvm_const_named_struct_internal(m, struct_type, new_values, cast(unsigned)value_count);
  1493. GB_ASSERT(is_local);
  1494. lbProcedure *p = m->curr_procedure;
  1495. lbAddr v = lb_add_local_generated(p, res.type, true);
  1496. map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, v);
  1497. LLVMBuildStore(p->builder, constant_value, v.addr.value);
  1498. for (isize i = 0; i < value_count; i++) {
  1499. LLVMValueRef val = old_values[i];
  1500. if (!LLVMIsConstant(val)) {
  1501. LLVMValueRef dst = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), v.addr.value, cast(unsigned)i, "");
  1502. // if (LLVMIsALoadInst(val)) {
  1503. // Type *ptr_type = v.addr.type;
  1504. // i64 sz = type_size_of(type_deref(ptr_type));
  1505. // LLVMValueRef src = LLVMGetOperand(val, 0);
  1506. // lb_mem_copy_non_overlapping(p, {dst, ptr_type}, {src, ptr_type}, lb_const_int(m, t_int, sz), false);
  1507. // } else {
  1508. LLVMBuildStore(p->builder, val, dst);
  1509. // }
  1510. }
  1511. }
  1512. return lb_addr_load(p, v);
  1513. }
  1514. } else if (is_type_bit_set(type)) {
  1515. ast_node(cl, CompoundLit, value.value_compound);
  1516. if (cl->elems.count == 0) {
  1517. return lb_const_nil(m, original_type);
  1518. }
  1519. i64 sz = type_size_of(type);
  1520. if (sz == 0) {
  1521. return lb_const_nil(m, original_type);
  1522. }
  1523. BigInt bits = {};
  1524. BigInt one = {};
  1525. big_int_from_u64(&one, 1);
  1526. for_array(i, cl->elems) {
  1527. Ast *e = cl->elems[i];
  1528. GB_ASSERT(e->kind != Ast_FieldValue);
  1529. TypeAndValue tav = e->tav;
  1530. if (tav.mode != Addressing_Constant) {
  1531. continue;
  1532. }
  1533. GB_ASSERT(tav.value.kind == ExactValue_Integer);
  1534. i64 v = big_int_to_i64(&tav.value.value_integer);
  1535. i64 lower = type->BitSet.lower;
  1536. u64 index = cast(u64)(v-lower);
  1537. BigInt bit = {};
  1538. big_int_from_u64(&bit, index);
  1539. big_int_shl(&bit, &one, &bit);
  1540. big_int_or(&bits, &bits, &bit);
  1541. }
  1542. res.value = lb_big_int_to_llvm(m, original_type, &bits);
  1543. return res;
  1544. } else if (is_type_matrix(type)) {
  1545. ast_node(cl, CompoundLit, value.value_compound);
  1546. Type *elem_type = type->Matrix.elem;
  1547. isize elem_count = cl->elems.count;
  1548. if (elem_count == 0 || !elem_type_can_be_constant(elem_type)) {
  1549. return lb_const_nil(m, original_type);
  1550. }
  1551. i64 max_count = type->Matrix.row_count*type->Matrix.column_count;
  1552. i64 total_count = matrix_type_total_internal_elems(type);
  1553. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1554. if (cl->elems[0]->kind == Ast_FieldValue) {
  1555. for_array(j, cl->elems) {
  1556. Ast *elem = cl->elems[j];
  1557. ast_node(fv, FieldValue, elem);
  1558. if (is_ast_range(fv->field)) {
  1559. ast_node(ie, BinaryExpr, fv->field);
  1560. TypeAndValue lo_tav = ie->left->tav;
  1561. TypeAndValue hi_tav = ie->right->tav;
  1562. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1563. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1564. TokenKind op = ie->op.kind;
  1565. i64 lo = exact_value_to_i64(lo_tav.value);
  1566. i64 hi = exact_value_to_i64(hi_tav.value);
  1567. if (op != Token_RangeHalf) {
  1568. hi += 1;
  1569. }
  1570. GB_ASSERT(0 <= lo && lo <= max_count);
  1571. GB_ASSERT(0 <= hi && hi <= max_count);
  1572. GB_ASSERT(lo <= hi);
  1573. TypeAndValue tav = fv->value->tav;
  1574. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1575. for (i64 k = lo; k < hi; k++) {
  1576. i64 offset = matrix_row_major_index_to_offset(type, k);
  1577. GB_ASSERT(values[offset] == nullptr);
  1578. values[offset] = val;
  1579. }
  1580. } else {
  1581. TypeAndValue index_tav = fv->field->tav;
  1582. GB_ASSERT(index_tav.mode == Addressing_Constant);
  1583. i64 index = exact_value_to_i64(index_tav.value);
  1584. GB_ASSERT(index < max_count);
  1585. TypeAndValue tav = fv->value->tav;
  1586. LLVMValueRef val = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1587. i64 offset = matrix_row_major_index_to_offset(type, index);
  1588. GB_ASSERT(values[offset] == nullptr);
  1589. values[offset] = val;
  1590. }
  1591. }
  1592. for (i64 i = 0; i < total_count; i++) {
  1593. if (values[i] == nullptr) {
  1594. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1595. }
  1596. }
  1597. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1598. return res;
  1599. } else {
  1600. GB_ASSERT_MSG(elem_count == max_count, "%td != %td", elem_count, max_count);
  1601. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)total_count);
  1602. for_array(i, cl->elems) {
  1603. TypeAndValue tav = cl->elems[i]->tav;
  1604. GB_ASSERT(tav.mode != Addressing_Invalid);
  1605. i64 offset = 0;
  1606. offset = matrix_row_major_index_to_offset(type, i);
  1607. values[offset] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value;
  1608. }
  1609. for (isize i = 0; i < total_count; i++) {
  1610. if (values[i] == nullptr) {
  1611. values[i] = LLVMConstNull(lb_type(m, elem_type));
  1612. }
  1613. }
  1614. res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)total_count, values, cc);
  1615. return res;
  1616. }
  1617. } else {
  1618. return lb_const_nil(m, original_type);
  1619. }
  1620. break;
  1621. case ExactValue_Procedure:
  1622. GB_PANIC("handled earlier");
  1623. break;
  1624. case ExactValue_Typeid:
  1625. return lb_typeid(m, value.value_typeid);
  1626. }
  1627. return lb_const_nil(m, original_type);
  1628. }