2
0

llvm_backend_utility.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. bool lb_is_type_aggregate(Type *t) {
  2. t = base_type(t);
  3. switch (t->kind) {
  4. case Type_Basic:
  5. switch (t->Basic.kind) {
  6. case Basic_string:
  7. case Basic_any:
  8. return true;
  9. case Basic_complex32:
  10. case Basic_complex64:
  11. case Basic_complex128:
  12. case Basic_quaternion64:
  13. case Basic_quaternion128:
  14. case Basic_quaternion256:
  15. return true;
  16. }
  17. break;
  18. case Type_Pointer:
  19. return false;
  20. case Type_Array:
  21. case Type_Slice:
  22. case Type_Struct:
  23. case Type_Union:
  24. case Type_Tuple:
  25. case Type_DynamicArray:
  26. case Type_Map:
  27. case Type_SimdVector:
  28. return true;
  29. case Type_Named:
  30. return lb_is_type_aggregate(t->Named.base);
  31. }
  32. return false;
  33. }
  34. lbValue lb_correct_endianness(lbProcedure *p, lbValue value) {
  35. Type *src = core_type(value.type);
  36. GB_ASSERT(is_type_integer(src) || is_type_float(src));
  37. if (is_type_different_to_arch_endianness(src)) {
  38. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  39. value = lb_emit_byte_swap(p, value, platform_src_type);
  40. }
  41. return value;
  42. }
  43. void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment) {
  44. bool is_inlinable = false;
  45. i64 const_len = 0;
  46. if (LLVMIsConstant(len)) {
  47. const_len = cast(i64)LLVMConstIntGetSExtValue(len);
  48. // TODO(bill): Determine when it is better to do the `*.inline` versions
  49. if (const_len <= 4*build_context.word_size) {
  50. is_inlinable = true;
  51. }
  52. }
  53. char const *name = "llvm.memset";
  54. if (is_inlinable) {
  55. name = "llvm.memset.inline";
  56. }
  57. LLVMTypeRef types[2] = {
  58. lb_type(p->module, t_rawptr),
  59. lb_type(p->module, t_int)
  60. };
  61. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  62. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s.%s", name, LLVMPrintTypeToString(types[0]), LLVMPrintTypeToString(types[1]));
  63. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  64. LLVMValueRef args[4] = {};
  65. args[0] = LLVMBuildPointerCast(p->builder, ptr, types[0], "");
  66. args[1] = LLVMConstInt(LLVMInt8TypeInContext(p->module->ctx), 0, false);
  67. args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, "");
  68. args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, false); // is_volatile parameter
  69. LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  70. }
  71. void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment) {
  72. LLVMTypeRef llvm_type = lb_type(p->module, type);
  73. LLVMTypeKind kind = LLVMGetTypeKind(llvm_type);
  74. switch (kind) {
  75. case LLVMStructTypeKind:
  76. case LLVMArrayTypeKind:
  77. {
  78. // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too
  79. i32 sz = cast(i32)type_size_of(type);
  80. lb_mem_zero_ptr_internal(p, ptr, lb_const_int(p->module, t_int, sz).value, alignment);
  81. }
  82. break;
  83. default:
  84. LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr);
  85. break;
  86. }
  87. }
  88. lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y) {
  89. cond = lb_emit_conv(p, cond, t_llvm_bool);
  90. lbValue res = {};
  91. res.value = LLVMBuildSelect(p->builder, cond.value, x.value, y.value, "");
  92. res.type = x.type;
  93. return res;
  94. }
  95. lbValue lb_emit_min(lbProcedure *p, Type *t, lbValue x, lbValue y) {
  96. x = lb_emit_conv(p, x, t);
  97. y = lb_emit_conv(p, y, t);
  98. return lb_emit_select(p, lb_emit_comp(p, Token_Lt, x, y), x, y);
  99. }
  100. lbValue lb_emit_max(lbProcedure *p, Type *t, lbValue x, lbValue y) {
  101. x = lb_emit_conv(p, x, t);
  102. y = lb_emit_conv(p, y, t);
  103. return lb_emit_select(p, lb_emit_comp(p, Token_Gt, x, y), x, y);
  104. }
  105. lbValue lb_emit_clamp(lbProcedure *p, Type *t, lbValue x, lbValue min, lbValue max) {
  106. lbValue z = {};
  107. z = lb_emit_max(p, t, x, min);
  108. z = lb_emit_min(p, t, z, max);
  109. return z;
  110. }
  111. lbValue lb_emit_string(lbProcedure *p, lbValue str_elem, lbValue str_len) {
  112. if (false && lb_is_const(str_elem) && lb_is_const(str_len)) {
  113. LLVMValueRef values[2] = {
  114. str_elem.value,
  115. str_len.value,
  116. };
  117. lbValue res = {};
  118. res.type = t_string;
  119. res.value = llvm_const_named_struct(p->module, t_string, values, gb_count_of(values));
  120. return res;
  121. } else {
  122. lbAddr res = lb_add_local_generated(p, t_string, false);
  123. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 0), str_elem);
  124. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 1), str_len);
  125. return lb_addr_load(p, res);
  126. }
  127. }
  128. lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t) {
  129. Type *src_type = value.type;
  130. if (are_types_identical(t, src_type)) {
  131. return value;
  132. }
  133. lbValue res = {};
  134. res.type = t;
  135. Type *src = base_type(src_type);
  136. Type *dst = base_type(t);
  137. lbModule *m = p->module;
  138. i64 sz = type_size_of(src);
  139. i64 dz = type_size_of(dst);
  140. if (sz != dz) {
  141. LLVMTypeRef s = lb_type(m, src);
  142. LLVMTypeRef d = lb_type(m, dst);
  143. i64 llvm_sz = lb_sizeof(s);
  144. i64 llvm_dz = lb_sizeof(d);
  145. GB_ASSERT_MSG(llvm_sz == llvm_dz, "%s %s", LLVMPrintTypeToString(s), LLVMPrintTypeToString(d));
  146. }
  147. GB_ASSERT_MSG(sz == dz, "Invalid transmute conversion: '%s' to '%s'", type_to_string(src_type), type_to_string(t));
  148. // NOTE(bill): Casting between an integer and a pointer cannot be done through a bitcast
  149. if (is_type_uintptr(src) && is_type_pointer(dst)) {
  150. res.value = LLVMBuildIntToPtr(p->builder, value.value, lb_type(m, t), "");
  151. return res;
  152. }
  153. if (is_type_pointer(src) && is_type_uintptr(dst)) {
  154. res.value = LLVMBuildPtrToInt(p->builder, value.value, lb_type(m, t), "");
  155. return res;
  156. }
  157. if (is_type_uintptr(src) && is_type_proc(dst)) {
  158. res.value = LLVMBuildIntToPtr(p->builder, value.value, lb_type(m, t), "");
  159. return res;
  160. }
  161. if (is_type_proc(src) && is_type_uintptr(dst)) {
  162. res.value = LLVMBuildPtrToInt(p->builder, value.value, lb_type(m, t), "");
  163. return res;
  164. }
  165. if (is_type_integer(src) && (is_type_pointer(dst) || is_type_cstring(dst))) {
  166. res.value = LLVMBuildIntToPtr(p->builder, value.value, lb_type(m, t), "");
  167. return res;
  168. } else if ((is_type_pointer(src) || is_type_cstring(src)) && is_type_integer(dst)) {
  169. res.value = LLVMBuildPtrToInt(p->builder, value.value, lb_type(m, t), "");
  170. return res;
  171. }
  172. if (is_type_pointer(src) && is_type_pointer(dst)) {
  173. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(p->module, t), "");
  174. return res;
  175. }
  176. if (lb_is_type_aggregate(src) || lb_is_type_aggregate(dst)) {
  177. lbValue s = lb_address_from_load_or_generate_local(p, value);
  178. lbValue d = lb_emit_transmute(p, s, alloc_type_pointer(t));
  179. return lb_emit_load(p, d);
  180. }
  181. res.value = LLVMBuildBitCast(p->builder, value.value, lb_type(p->module, t), "");
  182. return res;
  183. }
  184. lbValue lb_copy_value_to_ptr(lbProcedure *p, lbValue val, Type *new_type, i64 alignment) {
  185. i64 type_alignment = type_align_of(new_type);
  186. if (alignment < type_alignment) {
  187. alignment = type_alignment;
  188. }
  189. GB_ASSERT_MSG(are_types_identical(new_type, val.type), "%s %s", type_to_string(new_type), type_to_string(val.type));
  190. lbAddr ptr = lb_add_local_generated(p, new_type, false);
  191. LLVMSetAlignment(ptr.addr.value, cast(unsigned)alignment);
  192. lb_addr_store(p, ptr, val);
  193. // ptr.kind = lbAddr_Context;
  194. return ptr.addr;
  195. }
  196. lbValue lb_soa_zip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) {
  197. GB_ASSERT(ce->args.count > 0);
  198. auto slices = slice_make<lbValue>(temporary_allocator(), ce->args.count);
  199. for_array(i, slices) {
  200. Ast *arg = ce->args[i];
  201. if (arg->kind == Ast_FieldValue) {
  202. arg = arg->FieldValue.value;
  203. }
  204. slices[i] = lb_build_expr(p, arg);
  205. }
  206. lbValue len = lb_slice_len(p, slices[0]);
  207. for (isize i = 1; i < slices.count; i++) {
  208. lbValue other_len = lb_slice_len(p, slices[i]);
  209. len = lb_emit_min(p, t_int, len, other_len);
  210. }
  211. GB_ASSERT(is_type_soa_struct(tv.type));
  212. lbAddr res = lb_add_local_generated(p, tv.type, true);
  213. for_array(i, slices) {
  214. lbValue src = lb_slice_elem(p, slices[i]);
  215. lbValue dst = lb_emit_struct_ep(p, res.addr, cast(i32)i);
  216. lb_emit_store(p, dst, src);
  217. }
  218. lbValue len_dst = lb_emit_struct_ep(p, res.addr, cast(i32)slices.count);
  219. lb_emit_store(p, len_dst, len);
  220. return lb_addr_load(p, res);
  221. }
  222. lbValue lb_soa_unzip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) {
  223. GB_ASSERT(ce->args.count == 1);
  224. lbValue arg = lb_build_expr(p, ce->args[0]);
  225. Type *t = base_type(arg.type);
  226. GB_ASSERT(is_type_soa_struct(t) && t->Struct.soa_kind == StructSoa_Slice);
  227. lbValue len = lb_soa_struct_len(p, arg);
  228. lbAddr res = lb_add_local_generated(p, tv.type, true);
  229. if (is_type_tuple(tv.type)) {
  230. lbValue rp = lb_addr_get_ptr(p, res);
  231. for (i32 i = 0; i < cast(i32)(t->Struct.fields.count-1); i++) {
  232. lbValue ptr = lb_emit_struct_ev(p, arg, i);
  233. lbAddr dst = lb_addr(lb_emit_struct_ep(p, rp, i));
  234. lb_fill_slice(p, dst, ptr, len);
  235. }
  236. } else {
  237. GB_ASSERT(is_type_slice(tv.type));
  238. lbValue ptr = lb_emit_struct_ev(p, arg, 0);
  239. lb_fill_slice(p, res, ptr, len);
  240. }
  241. return lb_addr_load(p, res);
  242. }
  243. void lb_emit_try_lhs_rhs(lbProcedure *p, Ast *arg, TypeAndValue const &tv, lbValue *lhs_, lbValue *rhs_) {
  244. lbValue lhs = {};
  245. lbValue rhs = {};
  246. lbValue value = lb_build_expr(p, arg);
  247. if (is_type_tuple(value.type)) {
  248. i32 n = cast(i32)(value.type->Tuple.variables.count-1);
  249. if (value.type->Tuple.variables.count == 2) {
  250. lhs = lb_emit_struct_ev(p, value, 0);
  251. } else {
  252. lbAddr lhs_addr = lb_add_local_generated(p, tv.type, false);
  253. lbValue lhs_ptr = lb_addr_get_ptr(p, lhs_addr);
  254. for (i32 i = 0; i < n; i++) {
  255. lb_emit_store(p, lb_emit_struct_ep(p, lhs_ptr, i), lb_emit_struct_ev(p, value, i));
  256. }
  257. lhs = lb_addr_load(p, lhs_addr);
  258. }
  259. rhs = lb_emit_struct_ev(p, value, n);
  260. } else {
  261. rhs = value;
  262. }
  263. GB_ASSERT(rhs.value != nullptr);
  264. if (lhs_) *lhs_ = lhs;
  265. if (rhs_) *rhs_ = rhs;
  266. }
  267. lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) {
  268. lbValue has_value = {};
  269. if (is_type_boolean(rhs.type)) {
  270. has_value = rhs;
  271. } else {
  272. GB_ASSERT_MSG(type_has_nil(rhs.type), "%s", type_to_string(rhs.type));
  273. has_value = lb_emit_comp_against_nil(p, Token_CmpEq, rhs);
  274. }
  275. GB_ASSERT(has_value.value != nullptr);
  276. return has_value;
  277. }
  278. lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) {
  279. lbValue lhs = {};
  280. lbValue rhs = {};
  281. lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs);
  282. LLVMValueRef incoming_values[2] = {};
  283. LLVMBasicBlockRef incoming_blocks[2] = {};
  284. GB_ASSERT(else_expr != nullptr);
  285. lbBlock *then = lb_create_block(p, "or_else.then");
  286. lbBlock *done = lb_create_block(p, "or_else.done"); // NOTE(bill): Append later
  287. lbBlock *else_ = lb_create_block(p, "or_else.else");
  288. lb_emit_if(p, lb_emit_try_has_value(p, rhs), then, else_);
  289. lb_start_block(p, then);
  290. Type *type = default_type(tv.type);
  291. incoming_values[0] = lb_emit_conv(p, lhs, type).value;
  292. lb_emit_jump(p, done);
  293. lb_start_block(p, else_);
  294. incoming_values[1] = lb_emit_conv(p, lb_build_expr(p, else_expr), type).value;
  295. lb_emit_jump(p, done);
  296. lb_start_block(p, done);
  297. lbValue res = {};
  298. res.value = LLVMBuildPhi(p->builder, lb_type(p->module, type), "");
  299. res.type = type;
  300. GB_ASSERT(p->curr_block->preds.count >= 2);
  301. incoming_blocks[0] = p->curr_block->preds[0]->block;
  302. incoming_blocks[1] = p->curr_block->preds[1]->block;
  303. LLVMAddIncoming(res.value, incoming_values, incoming_blocks, 2);
  304. return res;
  305. }
  306. void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results);
  307. void lb_build_return_stmt_internal(lbProcedure *p, lbValue const &res);
  308. lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) {
  309. lbValue lhs = {};
  310. lbValue rhs = {};
  311. lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs);
  312. lbBlock *return_block = lb_create_block(p, "or_return.return");
  313. lbBlock *continue_block = lb_create_block(p, "or_return.continue");
  314. lb_emit_if(p, lb_emit_try_has_value(p, rhs), continue_block, return_block);
  315. lb_start_block(p, return_block);
  316. {
  317. Type *proc_type = base_type(p->type);
  318. Type *results = proc_type->Proc.results;
  319. GB_ASSERT(results != nullptr && results->kind == Type_Tuple);
  320. TypeTuple *tuple = &results->Tuple;
  321. GB_ASSERT(tuple->variables.count != 0);
  322. Entity *end_entity = tuple->variables[tuple->variables.count-1];
  323. rhs = lb_emit_conv(p, rhs, end_entity->type);
  324. if (p->type->Proc.has_named_results) {
  325. GB_ASSERT(end_entity->token.string.len != 0);
  326. // NOTE(bill): store the named values before returning
  327. lbValue found = map_must_get(&p->module->values, hash_entity(end_entity));
  328. lb_emit_store(p, found, rhs);
  329. lb_build_return_stmt(p, {});
  330. } else {
  331. GB_ASSERT(tuple->variables.count == 1);
  332. lb_build_return_stmt_internal(p, rhs);
  333. }
  334. }
  335. lb_start_block(p, continue_block);
  336. if (tv.type != nullptr) {
  337. return lb_emit_conv(p, lhs, tv.type);
  338. }
  339. return {};
  340. }
  341. void lb_emit_increment(lbProcedure *p, lbValue addr) {
  342. GB_ASSERT(is_type_pointer(addr.type));
  343. Type *type = type_deref(addr.type);
  344. lbValue v_one = lb_const_value(p->module, type, exact_value_i64(1));
  345. lb_emit_store(p, addr, lb_emit_arith(p, Token_Add, lb_emit_load(p, addr), v_one, type));
  346. }
  347. lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) {
  348. GB_ASSERT(type_size_of(value.type) == type_size_of(end_type));
  349. if (type_size_of(value.type) < 2) {
  350. return value;
  351. }
  352. Type *original_type = value.type;
  353. if (is_type_float(original_type)) {
  354. i64 sz = type_size_of(original_type);
  355. Type *integer_type = nullptr;
  356. switch (sz) {
  357. case 2: integer_type = t_u16; break;
  358. case 4: integer_type = t_u32; break;
  359. case 8: integer_type = t_u64; break;
  360. }
  361. GB_ASSERT(integer_type != nullptr);
  362. value = lb_emit_transmute(p, value, integer_type);
  363. }
  364. char const *name = "llvm.bswap";
  365. LLVMTypeRef types[1] = {lb_type(p->module, value.type)};
  366. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  367. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
  368. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  369. LLVMValueRef args[1] = {};
  370. args[0] = value.value;
  371. lbValue res = {};
  372. res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  373. res.type = value.type;
  374. if (is_type_float(original_type)) {
  375. res = lb_emit_transmute(p, res, original_type);
  376. }
  377. res.type = end_type;
  378. return res;
  379. }
  380. lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) {
  381. x = lb_emit_conv(p, x, type);
  382. char const *name = "llvm.ctpop";
  383. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  384. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  385. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
  386. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  387. LLVMValueRef args[1] = {};
  388. args[0] = x.value;
  389. lbValue res = {};
  390. res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  391. res.type = type;
  392. return res;
  393. }
  394. lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type) {
  395. i64 sz = 8*type_size_of(type);
  396. lbValue size = lb_const_int(p->module, type, cast(u64)sz);
  397. lbValue count = lb_emit_count_ones(p, x, type);
  398. return lb_emit_arith(p, Token_Sub, size, count, type);
  399. }
  400. lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) {
  401. x = lb_emit_conv(p, x, type);
  402. char const *name = "llvm.cttz";
  403. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  404. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  405. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
  406. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  407. LLVMValueRef args[2] = {};
  408. args[0] = x.value;
  409. args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx));
  410. lbValue res = {};
  411. res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  412. res.type = type;
  413. return res;
  414. }
  415. lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) {
  416. x = lb_emit_conv(p, x, type);
  417. char const *name = "llvm.ctlz";
  418. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  419. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  420. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
  421. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  422. LLVMValueRef args[2] = {};
  423. args[0] = x.value;
  424. args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx));
  425. lbValue res = {};
  426. res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  427. res.type = type;
  428. return res;
  429. }
  430. lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) {
  431. x = lb_emit_conv(p, x, type);
  432. char const *name = "llvm.bitreverse";
  433. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  434. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  435. GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
  436. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  437. LLVMValueRef args[1] = {};
  438. args[0] = x.value;
  439. lbValue res = {};
  440. res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
  441. res.type = type;
  442. return res;
  443. }
  444. lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) {
  445. GB_ASSERT(is_type_bit_set(x.type));
  446. Type *underlying = bit_set_to_int(x.type);
  447. lbValue card = lb_emit_count_ones(p, x, underlying);
  448. return lb_emit_conv(p, card, t_int);
  449. }
  450. lbValue lb_emit_union_cast_only_ok_check(lbProcedure *p, lbValue value, Type *type, TokenPos pos) {
  451. GB_ASSERT(is_type_tuple(type));
  452. lbModule *m = p->module;
  453. Type *src_type = value.type;
  454. bool is_ptr = is_type_pointer(src_type);
  455. // IMPORTANT NOTE(bill): This assumes that the value is completely ignored
  456. // so when it does an assignment, it complete ignores the value.
  457. // Just make it two booleans and ignore the first one
  458. //
  459. // _, ok := x.(T);
  460. //
  461. Type *ok_type = type->Tuple.variables[1]->type;
  462. Type *gen_tuple_types[2] = {};
  463. gen_tuple_types[0] = ok_type;
  464. gen_tuple_types[1] = ok_type;
  465. Type *gen_tuple = alloc_type_tuple_from_field_types(gen_tuple_types, gb_count_of(gen_tuple_types), false, true);
  466. lbAddr v = lb_add_local_generated(p, gen_tuple, false);
  467. if (is_ptr) {
  468. value = lb_emit_load(p, value);
  469. }
  470. Type *src = base_type(type_deref(src_type));
  471. GB_ASSERT_MSG(is_type_union(src), "%s", type_to_string(src_type));
  472. Type *dst = type->Tuple.variables[0]->type;
  473. lbValue cond = {};
  474. if (is_type_union_maybe_pointer(src)) {
  475. lbValue data = lb_emit_transmute(p, value, dst);
  476. cond = lb_emit_comp_against_nil(p, Token_NotEq, data);
  477. } else {
  478. lbValue tag = lb_emit_union_tag_value(p, value);
  479. lbValue dst_tag = lb_const_union_tag(m, src, dst);
  480. cond = lb_emit_comp(p, Token_CmpEq, tag, dst_tag);
  481. }
  482. lbValue gep1 = lb_emit_struct_ep(p, v.addr, 1);
  483. lb_emit_store(p, gep1, cond);
  484. return lb_addr_load(p, v);
  485. }
  486. lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) {
  487. lbModule *m = p->module;
  488. Type *src_type = value.type;
  489. bool is_ptr = is_type_pointer(src_type);
  490. bool is_tuple = true;
  491. Type *tuple = type;
  492. if (type->kind != Type_Tuple) {
  493. is_tuple = false;
  494. tuple = make_optional_ok_type(type);
  495. }
  496. lbAddr v = lb_add_local_generated(p, tuple, true);
  497. if (is_ptr) {
  498. value = lb_emit_load(p, value);
  499. }
  500. Type *src = base_type(type_deref(src_type));
  501. GB_ASSERT_MSG(is_type_union(src), "%s", type_to_string(src_type));
  502. Type *dst = tuple->Tuple.variables[0]->type;
  503. lbValue value_ = lb_address_from_load_or_generate_local(p, value);
  504. lbValue tag = {};
  505. lbValue dst_tag = {};
  506. lbValue cond = {};
  507. lbValue data = {};
  508. lbValue gep0 = lb_emit_struct_ep(p, v.addr, 0);
  509. lbValue gep1 = lb_emit_struct_ep(p, v.addr, 1);
  510. if (is_type_union_maybe_pointer(src)) {
  511. data = lb_emit_load(p, lb_emit_conv(p, value_, gep0.type));
  512. } else {
  513. tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, value_));
  514. dst_tag = lb_const_union_tag(m, src, dst);
  515. }
  516. lbBlock *ok_block = lb_create_block(p, "union_cast.ok");
  517. lbBlock *end_block = lb_create_block(p, "union_cast.end");
  518. if (data.value != nullptr) {
  519. GB_ASSERT(is_type_union_maybe_pointer(src));
  520. cond = lb_emit_comp_against_nil(p, Token_NotEq, data);
  521. } else {
  522. cond = lb_emit_comp(p, Token_CmpEq, tag, dst_tag);
  523. }
  524. lb_emit_if(p, cond, ok_block, end_block);
  525. lb_start_block(p, ok_block);
  526. if (data.value == nullptr) {
  527. data = lb_emit_load(p, lb_emit_conv(p, value_, gep0.type));
  528. }
  529. lb_emit_store(p, gep0, data);
  530. lb_emit_store(p, gep1, lb_const_bool(m, t_bool, true));
  531. lb_emit_jump(p, end_block);
  532. lb_start_block(p, end_block);
  533. if (!is_tuple) {
  534. {
  535. // NOTE(bill): Panic on invalid conversion
  536. Type *dst_type = tuple->Tuple.variables[0]->type;
  537. lbValue ok = lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 1));
  538. auto args = array_make<lbValue>(permanent_allocator(), 7);
  539. args[0] = ok;
  540. args[1] = lb_const_string(m, get_file_path_string(pos.file_id));
  541. args[2] = lb_const_int(m, t_i32, pos.line);
  542. args[3] = lb_const_int(m, t_i32, pos.column);
  543. args[4] = lb_typeid(m, src_type);
  544. args[5] = lb_typeid(m, dst_type);
  545. args[6] = lb_emit_conv(p, value_, t_rawptr);
  546. lb_emit_runtime_call(p, "type_assertion_check2", args);
  547. }
  548. return lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 0));
  549. }
  550. return lb_addr_load(p, v);
  551. }
  552. lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos pos) {
  553. lbModule *m = p->module;
  554. Type *src_type = value.type;
  555. if (is_type_pointer(src_type)) {
  556. value = lb_emit_load(p, value);
  557. }
  558. bool is_tuple = true;
  559. Type *tuple = type;
  560. if (type->kind != Type_Tuple) {
  561. is_tuple = false;
  562. tuple = make_optional_ok_type(type);
  563. }
  564. Type *dst_type = tuple->Tuple.variables[0]->type;
  565. lbAddr v = lb_add_local_generated(p, tuple, true);
  566. lbValue dst_typeid = lb_typeid(m, dst_type);
  567. lbValue any_typeid = lb_emit_struct_ev(p, value, 1);
  568. lbBlock *ok_block = lb_create_block(p, "any_cast.ok");
  569. lbBlock *end_block = lb_create_block(p, "any_cast.end");
  570. lbValue cond = lb_emit_comp(p, Token_CmpEq, any_typeid, dst_typeid);
  571. lb_emit_if(p, cond, ok_block, end_block);
  572. lb_start_block(p, ok_block);
  573. lbValue gep0 = lb_emit_struct_ep(p, v.addr, 0);
  574. lbValue gep1 = lb_emit_struct_ep(p, v.addr, 1);
  575. lbValue any_data = lb_emit_struct_ev(p, value, 0);
  576. lbValue ptr = lb_emit_conv(p, any_data, alloc_type_pointer(dst_type));
  577. lb_emit_store(p, gep0, lb_emit_load(p, ptr));
  578. lb_emit_store(p, gep1, lb_const_bool(m, t_bool, true));
  579. lb_emit_jump(p, end_block);
  580. lb_start_block(p, end_block);
  581. if (!is_tuple) {
  582. // NOTE(bill): Panic on invalid conversion
  583. lbValue ok = lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 1));
  584. auto args = array_make<lbValue>(permanent_allocator(), 7);
  585. args[0] = ok;
  586. args[1] = lb_const_string(m, get_file_path_string(pos.file_id));
  587. args[2] = lb_const_int(m, t_i32, pos.line);
  588. args[3] = lb_const_int(m, t_i32, pos.column);
  589. args[4] = any_typeid;
  590. args[5] = dst_typeid;
  591. args[6] = lb_emit_struct_ev(p, value, 0);;
  592. lb_emit_runtime_call(p, "type_assertion_check2", args);
  593. return lb_addr(lb_emit_struct_ep(p, v.addr, 0));
  594. }
  595. return v;
  596. }
  597. lbValue lb_emit_any_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) {
  598. return lb_addr_load(p, lb_emit_any_cast_addr(p, value, type, pos));
  599. }
  600. lbAddr lb_find_or_generate_context_ptr(lbProcedure *p) {
  601. if (p->context_stack.count > 0) {
  602. return p->context_stack[p->context_stack.count-1].ctx;
  603. }
  604. Type *pt = base_type(p->type);
  605. GB_ASSERT(pt->kind == Type_Proc);
  606. GB_ASSERT(pt->Proc.calling_convention != ProcCC_Odin);
  607. lbAddr c = lb_add_local_generated(p, t_context, true);
  608. c.kind = lbAddr_Context;
  609. lb_emit_init_context(p, c);
  610. lb_push_context_onto_stack(p, c);
  611. lb_add_debug_context_variable(p, c);
  612. return c;
  613. }
  614. lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value) {
  615. if (LLVMIsALoadInst(value.value)) {
  616. lbValue res = {};
  617. res.value = LLVMGetOperand(value.value, 0);
  618. res.type = alloc_type_pointer(value.type);
  619. return res;
  620. }
  621. GB_ASSERT(is_type_typed(value.type));
  622. lbAddr res = lb_add_local_generated(p, value.type, false);
  623. lb_addr_store(p, res, value);
  624. return res.addr;
  625. }
  626. lbValue lb_address_from_load(lbProcedure *p, lbValue value) {
  627. if (LLVMIsALoadInst(value.value)) {
  628. lbValue res = {};
  629. res.value = LLVMGetOperand(value.value, 0);
  630. res.type = alloc_type_pointer(value.type);
  631. return res;
  632. }
  633. GB_PANIC("lb_address_from_load");
  634. return {};
  635. }
  636. lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) {
  637. t = base_type(t);
  638. LLVMTypeRef struct_type = lb_type(m, t);
  639. auto *field_remapping = map_get(&m->struct_field_remapping, hash_pointer(struct_type));
  640. if (field_remapping == nullptr) {
  641. field_remapping = map_get(&m->struct_field_remapping, hash_pointer(t));
  642. }
  643. GB_ASSERT(field_remapping != nullptr);
  644. return *field_remapping;
  645. }
  646. i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) {
  647. if (t->kind == Type_Struct) {
  648. auto field_remapping = lb_get_struct_remapping(m, t);
  649. index = field_remapping[index];
  650. }
  651. return index;
  652. }
  653. LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align) {
  654. // NOTE(bill): limit to `[N x u64]` to prevent ABI issues
  655. padding_align = gb_clamp(padding_align, 1, 8);
  656. if (padding % padding_align == 0) {
  657. LLVMTypeRef elem = nullptr;
  658. isize len = padding/padding_align;
  659. switch (padding_align) {
  660. case 1: elem = lb_type(m, t_u8); break;
  661. case 2: elem = lb_type(m, t_u16); break;
  662. case 4: elem = lb_type(m, t_u32); break;
  663. case 8: elem = lb_type(m, t_u64); break;
  664. }
  665. GB_ASSERT_MSG(elem != nullptr, "Invalid lb_type_padding_filler padding and padding_align: %lld", padding_align);
  666. if (len != 1) {
  667. return LLVMArrayType(elem, cast(unsigned)len);
  668. } else {
  669. return elem;
  670. }
  671. } else {
  672. return LLVMArrayType(lb_type(m, t_u8), cast(unsigned)padding);
  673. }
  674. }
  675. char const *llvm_type_kinds[] = {
  676. "LLVMVoidTypeKind",
  677. "LLVMHalfTypeKind",
  678. "LLVMFloatTypeKind",
  679. "LLVMDoubleTypeKind",
  680. "LLVMX86_FP80TypeKind",
  681. "LLVMFP128TypeKind",
  682. "LLVMPPC_FP128TypeKind",
  683. "LLVMLabelTypeKind",
  684. "LLVMIntegerTypeKind",
  685. "LLVMFunctionTypeKind",
  686. "LLVMStructTypeKind",
  687. "LLVMArrayTypeKind",
  688. "LLVMPointerTypeKind",
  689. "LLVMVectorTypeKind",
  690. "LLVMMetadataTypeKind",
  691. "LLVMX86_MMXTypeKind",
  692. "LLVMTokenTypeKind",
  693. "LLVMScalableVectorTypeKind",
  694. "LLVMBFloatTypeKind",
  695. };
  696. lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) {
  697. GB_ASSERT(is_type_pointer(s.type));
  698. Type *t = base_type(type_deref(s.type));
  699. Type *result_type = nullptr;
  700. if (is_type_relative_pointer(t)) {
  701. s = lb_addr_get_ptr(p, lb_addr(s));
  702. }
  703. if (is_type_struct(t)) {
  704. result_type = get_struct_field_type(t, index);
  705. } else if (is_type_union(t)) {
  706. GB_ASSERT(index == -1);
  707. return lb_emit_union_tag_ptr(p, s);
  708. } else if (is_type_tuple(t)) {
  709. GB_ASSERT(t->Tuple.variables.count > 0);
  710. result_type = t->Tuple.variables[index]->type;
  711. } else if (is_type_complex(t)) {
  712. Type *ft = base_complex_elem_type(t);
  713. switch (index) {
  714. case 0: result_type = ft; break;
  715. case 1: result_type = ft; break;
  716. }
  717. } else if (is_type_quaternion(t)) {
  718. Type *ft = base_complex_elem_type(t);
  719. switch (index) {
  720. case 0: result_type = ft; break;
  721. case 1: result_type = ft; break;
  722. case 2: result_type = ft; break;
  723. case 3: result_type = ft; break;
  724. }
  725. } else if (is_type_slice(t)) {
  726. switch (index) {
  727. case 0: result_type = alloc_type_pointer(t->Slice.elem); break;
  728. case 1: result_type = t_int; break;
  729. }
  730. } else if (is_type_string(t)) {
  731. switch (index) {
  732. case 0: result_type = t_u8_ptr; break;
  733. case 1: result_type = t_int; break;
  734. }
  735. } else if (is_type_any(t)) {
  736. switch (index) {
  737. case 0: result_type = t_rawptr; break;
  738. case 1: result_type = t_typeid; break;
  739. }
  740. } else if (is_type_dynamic_array(t)) {
  741. switch (index) {
  742. case 0: result_type = alloc_type_pointer(t->DynamicArray.elem); break;
  743. case 1: result_type = t_int; break;
  744. case 2: result_type = t_int; break;
  745. case 3: result_type = t_allocator; break;
  746. }
  747. } else if (is_type_map(t)) {
  748. init_map_internal_types(t);
  749. Type *itp = alloc_type_pointer(t->Map.internal_type);
  750. s = lb_emit_transmute(p, s, itp);
  751. Type *gst = t->Map.internal_type;
  752. GB_ASSERT(gst->kind == Type_Struct);
  753. switch (index) {
  754. case 0: result_type = get_struct_field_type(gst, 0); break;
  755. case 1: result_type = get_struct_field_type(gst, 1); break;
  756. }
  757. } else if (is_type_array(t)) {
  758. return lb_emit_array_epi(p, s, index);
  759. } else if (is_type_relative_slice(t)) {
  760. switch (index) {
  761. case 0: result_type = t->RelativeSlice.base_integer; break;
  762. case 1: result_type = t->RelativeSlice.base_integer; break;
  763. }
  764. } else {
  765. GB_PANIC("TODO(bill): struct_gep type: %s, %d", type_to_string(s.type), index);
  766. }
  767. GB_ASSERT_MSG(result_type != nullptr, "%s %d", type_to_string(t), index);
  768. i32 original_index = index;
  769. index = lb_convert_struct_index(p->module, t, index);
  770. if (lb_is_const(s)) {
  771. lbModule *m = p->module;
  772. lbValue res = {};
  773. LLVMValueRef indices[2] = {llvm_zero(m), LLVMConstInt(lb_type(m, t_i32), index, false)};
  774. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  775. res.type = alloc_type_pointer(result_type);
  776. return res;
  777. } else {
  778. lbValue res = {};
  779. LLVMTypeRef st = LLVMGetElementType(LLVMTypeOf(s.value));
  780. // gb_printf_err("%s\n", type_to_string(s.type));
  781. // gb_printf_err("%s\n", LLVMPrintTypeToString(LLVMTypeOf(s.value)));
  782. // gb_printf_err("%d\n", index);
  783. GB_ASSERT_MSG(LLVMGetTypeKind(st) == LLVMStructTypeKind, "%s", llvm_type_kinds[LLVMGetTypeKind(st)]);
  784. unsigned count = LLVMCountStructElementTypes(st);
  785. GB_ASSERT_MSG(count >= cast(unsigned)index, "%u %d %d", count, index, original_index);
  786. res.value = LLVMBuildStructGEP(p->builder, s.value, cast(unsigned)index, "");
  787. res.type = alloc_type_pointer(result_type);
  788. return res;
  789. }
  790. }
  791. lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) {
  792. if (LLVMIsALoadInst(s.value)) {
  793. lbValue res = {};
  794. res.value = LLVMGetOperand(s.value, 0);
  795. res.type = alloc_type_pointer(s.type);
  796. lbValue ptr = lb_emit_struct_ep(p, res, index);
  797. return lb_emit_load(p, ptr);
  798. }
  799. Type *t = base_type(s.type);
  800. Type *result_type = nullptr;
  801. switch (t->kind) {
  802. case Type_Basic:
  803. switch (t->Basic.kind) {
  804. case Basic_string:
  805. switch (index) {
  806. case 0: result_type = t_u8_ptr; break;
  807. case 1: result_type = t_int; break;
  808. }
  809. break;
  810. case Basic_any:
  811. switch (index) {
  812. case 0: result_type = t_rawptr; break;
  813. case 1: result_type = t_typeid; break;
  814. }
  815. break;
  816. case Basic_complex32:
  817. case Basic_complex64:
  818. case Basic_complex128:
  819. {
  820. Type *ft = base_complex_elem_type(t);
  821. switch (index) {
  822. case 0: result_type = ft; break;
  823. case 1: result_type = ft; break;
  824. }
  825. break;
  826. }
  827. case Basic_quaternion64:
  828. case Basic_quaternion128:
  829. case Basic_quaternion256:
  830. {
  831. Type *ft = base_complex_elem_type(t);
  832. switch (index) {
  833. case 0: result_type = ft; break;
  834. case 1: result_type = ft; break;
  835. case 2: result_type = ft; break;
  836. case 3: result_type = ft; break;
  837. }
  838. break;
  839. }
  840. }
  841. break;
  842. case Type_Struct:
  843. result_type = get_struct_field_type(t, index);
  844. break;
  845. case Type_Union:
  846. GB_ASSERT(index == -1);
  847. // return lb_emit_union_tag_value(p, s);
  848. GB_PANIC("lb_emit_union_tag_value");
  849. case Type_Tuple:
  850. GB_ASSERT(t->Tuple.variables.count > 0);
  851. result_type = t->Tuple.variables[index]->type;
  852. if (t->Tuple.variables.count == 1) {
  853. return s;
  854. }
  855. break;
  856. case Type_Slice:
  857. switch (index) {
  858. case 0: result_type = alloc_type_pointer(t->Slice.elem); break;
  859. case 1: result_type = t_int; break;
  860. }
  861. break;
  862. case Type_DynamicArray:
  863. switch (index) {
  864. case 0: result_type = alloc_type_pointer(t->DynamicArray.elem); break;
  865. case 1: result_type = t_int; break;
  866. case 2: result_type = t_int; break;
  867. case 3: result_type = t_allocator; break;
  868. }
  869. break;
  870. case Type_Map:
  871. {
  872. init_map_internal_types(t);
  873. Type *gst = t->Map.generated_struct_type;
  874. switch (index) {
  875. case 0: result_type = get_struct_field_type(gst, 0); break;
  876. case 1: result_type = get_struct_field_type(gst, 1); break;
  877. }
  878. }
  879. break;
  880. case Type_Array:
  881. result_type = t->Array.elem;
  882. break;
  883. default:
  884. GB_PANIC("TODO(bill): struct_ev type: %s, %d", type_to_string(s.type), index);
  885. break;
  886. }
  887. GB_ASSERT_MSG(result_type != nullptr, "%s, %d", type_to_string(s.type), index);
  888. index = lb_convert_struct_index(p->module, t, index);
  889. lbValue res = {};
  890. res.value = LLVMBuildExtractValue(p->builder, s.value, cast(unsigned)index, "");
  891. res.type = result_type;
  892. return res;
  893. }
  894. lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) {
  895. GB_ASSERT(sel.index.count > 0);
  896. Type *type = type_deref(e.type);
  897. for_array(i, sel.index) {
  898. i32 index = cast(i32)sel.index[i];
  899. if (is_type_pointer(type)) {
  900. type = type_deref(type);
  901. e = lb_emit_load(p, e);
  902. }
  903. type = core_type(type);
  904. if (is_type_quaternion(type)) {
  905. e = lb_emit_struct_ep(p, e, index);
  906. } else if (is_type_raw_union(type)) {
  907. type = get_struct_field_type(type, index);
  908. GB_ASSERT(is_type_pointer(e.type));
  909. e = lb_emit_transmute(p, e, alloc_type_pointer(type));
  910. } else if (is_type_struct(type)) {
  911. type = get_struct_field_type(type, index);
  912. e = lb_emit_struct_ep(p, e, index);
  913. } else if (type->kind == Type_Union) {
  914. GB_ASSERT(index == -1);
  915. type = t_type_info_ptr;
  916. e = lb_emit_struct_ep(p, e, index);
  917. } else if (type->kind == Type_Tuple) {
  918. type = type->Tuple.variables[index]->type;
  919. e = lb_emit_struct_ep(p, e, index);
  920. } else if (type->kind == Type_Basic) {
  921. switch (type->Basic.kind) {
  922. case Basic_any: {
  923. if (index == 0) {
  924. type = t_rawptr;
  925. } else if (index == 1) {
  926. type = t_type_info_ptr;
  927. }
  928. e = lb_emit_struct_ep(p, e, index);
  929. break;
  930. }
  931. case Basic_string:
  932. e = lb_emit_struct_ep(p, e, index);
  933. break;
  934. default:
  935. GB_PANIC("un-gep-able type %s", type_to_string(type));
  936. break;
  937. }
  938. } else if (type->kind == Type_Slice) {
  939. e = lb_emit_struct_ep(p, e, index);
  940. } else if (type->kind == Type_DynamicArray) {
  941. e = lb_emit_struct_ep(p, e, index);
  942. } else if (type->kind == Type_Array) {
  943. e = lb_emit_array_epi(p, e, index);
  944. } else if (type->kind == Type_Map) {
  945. e = lb_emit_struct_ep(p, e, index);
  946. } else if (type->kind == Type_RelativePointer) {
  947. e = lb_emit_struct_ep(p, e, index);
  948. } else {
  949. GB_PANIC("un-gep-able type %s", type_to_string(type));
  950. }
  951. }
  952. return e;
  953. }
  954. lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel) {
  955. lbValue ptr = lb_address_from_load_or_generate_local(p, e);
  956. lbValue res = lb_emit_deep_field_gep(p, ptr, sel);
  957. return lb_emit_load(p, res);
  958. }
  959. lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index) {
  960. Type *t = s.type;
  961. GB_ASSERT_MSG(is_type_pointer(t), "%s", type_to_string(t));
  962. Type *st = base_type(type_deref(t));
  963. GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st), "%s", type_to_string(st));
  964. GB_ASSERT_MSG(is_type_integer(core_type(index.type)), "%s", type_to_string(index.type));
  965. LLVMValueRef indices[2] = {};
  966. indices[0] = llvm_zero(p->module);
  967. indices[1] = lb_emit_conv(p, index, t_int).value;
  968. Type *ptr = base_array_type(st);
  969. lbValue res = {};
  970. res.value = LLVMBuildGEP(p->builder, s.value, indices, 2, "");
  971. res.type = alloc_type_pointer(ptr);
  972. return res;
  973. }
  974. lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) {
  975. Type *t = s.type;
  976. GB_ASSERT(is_type_pointer(t));
  977. Type *st = base_type(type_deref(t));
  978. GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st), "%s", type_to_string(st));
  979. GB_ASSERT(0 <= index);
  980. Type *ptr = base_array_type(st);
  981. LLVMValueRef indices[2] = {
  982. LLVMConstInt(lb_type(p->module, t_int), 0, false),
  983. LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)index, false),
  984. };
  985. lbValue res = {};
  986. if (lb_is_const(s)) {
  987. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  988. } else {
  989. res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), "");
  990. }
  991. res.type = alloc_type_pointer(ptr);
  992. return res;
  993. }
  994. lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) {
  995. index = lb_correct_endianness(p, index);
  996. LLVMValueRef indices[1] = {index.value};
  997. lbValue res = {};
  998. res.type = ptr.type;
  999. if (lb_is_const(ptr) && lb_is_const(index)) {
  1000. res.value = LLVMConstGEP(ptr.value, indices, 1);
  1001. } else {
  1002. res.value = LLVMBuildGEP(p->builder, ptr.value, indices, 1, "");
  1003. }
  1004. return res;
  1005. }
  1006. lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column) {
  1007. Type *t = s.type;
  1008. GB_ASSERT(is_type_pointer(t));
  1009. Type *mt = base_type(type_deref(t));
  1010. Type *ptr = base_array_type(mt);
  1011. if (column == 0) {
  1012. GB_ASSERT_MSG(is_type_matrix(mt) || is_type_array_like(mt), "%s", type_to_string(mt));
  1013. LLVMValueRef indices[2] = {
  1014. LLVMConstInt(lb_type(p->module, t_int), 0, false),
  1015. LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)row, false),
  1016. };
  1017. lbValue res = {};
  1018. if (lb_is_const(s)) {
  1019. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  1020. } else {
  1021. res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), "");
  1022. }
  1023. Type *ptr = base_array_type(mt);
  1024. res.type = alloc_type_pointer(ptr);
  1025. return res;
  1026. } else if (row == 0 && is_type_array_like(mt)) {
  1027. LLVMValueRef indices[2] = {
  1028. LLVMConstInt(lb_type(p->module, t_int), 0, false),
  1029. LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)column, false),
  1030. };
  1031. lbValue res = {};
  1032. if (lb_is_const(s)) {
  1033. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  1034. } else {
  1035. res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), "");
  1036. }
  1037. Type *ptr = base_array_type(mt);
  1038. res.type = alloc_type_pointer(ptr);
  1039. return res;
  1040. }
  1041. GB_ASSERT_MSG(is_type_matrix(mt), "%s", type_to_string(mt));
  1042. isize offset = matrix_indices_to_offset(mt, row, column);
  1043. LLVMValueRef indices[2] = {
  1044. LLVMConstInt(lb_type(p->module, t_int), 0, false),
  1045. LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)offset, false),
  1046. };
  1047. lbValue res = {};
  1048. if (lb_is_const(s)) {
  1049. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  1050. } else {
  1051. res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), "");
  1052. }
  1053. res.type = alloc_type_pointer(ptr);
  1054. return res;
  1055. }
  1056. lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column) {
  1057. Type *t = s.type;
  1058. GB_ASSERT(is_type_pointer(t));
  1059. Type *mt = base_type(type_deref(t));
  1060. GB_ASSERT_MSG(is_type_matrix(mt), "%s", type_to_string(mt));
  1061. Type *ptr = base_array_type(mt);
  1062. LLVMValueRef stride_elems = lb_const_int(p->module, t_int, matrix_type_stride_in_elems(mt)).value;
  1063. row = lb_emit_conv(p, row, t_int);
  1064. column = lb_emit_conv(p, column, t_int);
  1065. LLVMValueRef index = LLVMBuildAdd(p->builder, row.value, LLVMBuildMul(p->builder, column.value, stride_elems, ""), "");
  1066. LLVMValueRef indices[2] = {
  1067. LLVMConstInt(lb_type(p->module, t_int), 0, false),
  1068. index,
  1069. };
  1070. lbValue res = {};
  1071. if (lb_is_const(s)) {
  1072. res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices));
  1073. } else {
  1074. res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), "");
  1075. }
  1076. res.type = alloc_type_pointer(ptr);
  1077. return res;
  1078. }
  1079. lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column) {
  1080. Type *st = base_type(s.type);
  1081. GB_ASSERT_MSG(is_type_matrix(st), "%s", type_to_string(st));
  1082. lbValue value = lb_address_from_load_or_generate_local(p, s);
  1083. lbValue ptr = lb_emit_matrix_epi(p, value, row, column);
  1084. return lb_emit_load(p, ptr);
  1085. }
  1086. void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len) {
  1087. Type *t = lb_addr_type(slice);
  1088. GB_ASSERT(is_type_slice(t));
  1089. lbValue ptr = lb_addr_get_ptr(p, slice);
  1090. lb_emit_store(p, lb_emit_struct_ep(p, ptr, 0), base_elem);
  1091. lb_emit_store(p, lb_emit_struct_ep(p, ptr, 1), len);
  1092. }
  1093. void lb_fill_string(lbProcedure *p, lbAddr const &string, lbValue base_elem, lbValue len) {
  1094. Type *t = lb_addr_type(string);
  1095. GB_ASSERT(is_type_string(t));
  1096. lbValue ptr = lb_addr_get_ptr(p, string);
  1097. lb_emit_store(p, lb_emit_struct_ep(p, ptr, 0), base_elem);
  1098. lb_emit_store(p, lb_emit_struct_ep(p, ptr, 1), len);
  1099. }
  1100. lbValue lb_string_elem(lbProcedure *p, lbValue string) {
  1101. Type *t = base_type(string.type);
  1102. GB_ASSERT(t->kind == Type_Basic && t->Basic.kind == Basic_string);
  1103. return lb_emit_struct_ev(p, string, 0);
  1104. }
  1105. lbValue lb_string_len(lbProcedure *p, lbValue string) {
  1106. Type *t = base_type(string.type);
  1107. GB_ASSERT_MSG(t->kind == Type_Basic && t->Basic.kind == Basic_string, "%s", type_to_string(t));
  1108. return lb_emit_struct_ev(p, string, 1);
  1109. }
  1110. lbValue lb_cstring_len(lbProcedure *p, lbValue value) {
  1111. GB_ASSERT(is_type_cstring(value.type));
  1112. auto args = array_make<lbValue>(permanent_allocator(), 1);
  1113. args[0] = lb_emit_conv(p, value, t_cstring);
  1114. return lb_emit_runtime_call(p, "cstring_len", args);
  1115. }
  1116. lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr) {
  1117. Type *t = type_deref(array_ptr.type);
  1118. GB_ASSERT(is_type_array(t));
  1119. return lb_emit_struct_ep(p, array_ptr, 0);
  1120. }
  1121. lbValue lb_slice_elem(lbProcedure *p, lbValue slice) {
  1122. GB_ASSERT(is_type_slice(slice.type));
  1123. return lb_emit_struct_ev(p, slice, 0);
  1124. }
  1125. lbValue lb_slice_len(lbProcedure *p, lbValue slice) {
  1126. GB_ASSERT(is_type_slice(slice.type));
  1127. return lb_emit_struct_ev(p, slice, 1);
  1128. }
  1129. lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da) {
  1130. GB_ASSERT(is_type_dynamic_array(da.type));
  1131. return lb_emit_struct_ev(p, da, 0);
  1132. }
  1133. lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da) {
  1134. GB_ASSERT(is_type_dynamic_array(da.type));
  1135. return lb_emit_struct_ev(p, da, 1);
  1136. }
  1137. lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da) {
  1138. GB_ASSERT(is_type_dynamic_array(da.type));
  1139. return lb_emit_struct_ev(p, da, 2);
  1140. }
  1141. lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da) {
  1142. GB_ASSERT(is_type_dynamic_array(da.type));
  1143. return lb_emit_struct_ev(p, da, 3);
  1144. }
  1145. lbValue lb_map_entries(lbProcedure *p, lbValue value) {
  1146. Type *t = base_type(value.type);
  1147. GB_ASSERT_MSG(t->kind == Type_Map, "%s", type_to_string(t));
  1148. init_map_internal_types(t);
  1149. i32 index = 1;
  1150. lbValue entries = lb_emit_struct_ev(p, value, index);
  1151. return entries;
  1152. }
  1153. lbValue lb_map_entries_ptr(lbProcedure *p, lbValue value) {
  1154. Type *t = base_type(type_deref(value.type));
  1155. GB_ASSERT_MSG(t->kind == Type_Map, "%s", type_to_string(t));
  1156. init_map_internal_types(t);
  1157. i32 index = 1;
  1158. lbValue entries = lb_emit_struct_ep(p, value, index);
  1159. return entries;
  1160. }
  1161. lbValue lb_map_len(lbProcedure *p, lbValue value) {
  1162. lbValue entries = lb_map_entries(p, value);
  1163. return lb_dynamic_array_len(p, entries);
  1164. }
  1165. lbValue lb_map_cap(lbProcedure *p, lbValue value) {
  1166. lbValue entries = lb_map_entries(p, value);
  1167. return lb_dynamic_array_cap(p, entries);
  1168. }
  1169. lbValue lb_soa_struct_len(lbProcedure *p, lbValue value) {
  1170. Type *t = base_type(value.type);
  1171. bool is_ptr = false;
  1172. if (is_type_pointer(t)) {
  1173. is_ptr = true;
  1174. t = base_type(type_deref(t));
  1175. }
  1176. if (t->Struct.soa_kind == StructSoa_Fixed) {
  1177. return lb_const_int(p->module, t_int, t->Struct.soa_count);
  1178. }
  1179. GB_ASSERT(t->Struct.soa_kind == StructSoa_Slice ||
  1180. t->Struct.soa_kind == StructSoa_Dynamic);
  1181. isize n = 0;
  1182. Type *elem = base_type(t->Struct.soa_elem);
  1183. if (elem->kind == Type_Struct) {
  1184. n = cast(isize)elem->Struct.fields.count;
  1185. } else if (elem->kind == Type_Array) {
  1186. n = cast(isize)elem->Array.count;
  1187. } else {
  1188. GB_PANIC("Unreachable");
  1189. }
  1190. if (is_ptr) {
  1191. lbValue v = lb_emit_struct_ep(p, value, cast(i32)n);
  1192. return lb_emit_load(p, v);
  1193. }
  1194. return lb_emit_struct_ev(p, value, cast(i32)n);
  1195. }
  1196. lbValue lb_soa_struct_cap(lbProcedure *p, lbValue value) {
  1197. Type *t = base_type(value.type);
  1198. bool is_ptr = false;
  1199. if (is_type_pointer(t)) {
  1200. is_ptr = true;
  1201. t = base_type(type_deref(t));
  1202. }
  1203. if (t->Struct.soa_kind == StructSoa_Fixed) {
  1204. return lb_const_int(p->module, t_int, t->Struct.soa_count);
  1205. }
  1206. GB_ASSERT(t->Struct.soa_kind == StructSoa_Dynamic);
  1207. isize n = 0;
  1208. Type *elem = base_type(t->Struct.soa_elem);
  1209. if (elem->kind == Type_Struct) {
  1210. n = cast(isize)elem->Struct.fields.count+1;
  1211. } else if (elem->kind == Type_Array) {
  1212. n = cast(isize)elem->Array.count+1;
  1213. } else {
  1214. GB_PANIC("Unreachable");
  1215. }
  1216. if (is_ptr) {
  1217. lbValue v = lb_emit_struct_ep(p, value, cast(i32)n);
  1218. return lb_emit_load(p, v);
  1219. }
  1220. return lb_emit_struct_ev(p, value, cast(i32)n);
  1221. }
  1222. lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t) {
  1223. lbModule *m = p->module;
  1224. a = lb_emit_conv(p, a, t);
  1225. b = lb_emit_conv(p, b, t);
  1226. c = lb_emit_conv(p, c, t);
  1227. if (!is_type_different_to_arch_endianness(t) && is_type_float(t)) {
  1228. char const *name = "llvm.fma";
  1229. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  1230. GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
  1231. LLVMTypeRef types[1] = {};
  1232. types[0] = lb_type(m, t);
  1233. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(m->mod, id, types, gb_count_of(types));
  1234. LLVMValueRef values[3] = {};
  1235. values[0] = a.value;
  1236. values[1] = b.value;
  1237. values[2] = c.value;
  1238. LLVMValueRef call = LLVMBuildCall(p->builder, ip, values, gb_count_of(values), "");
  1239. return {call, t};
  1240. } else {
  1241. lbValue x = lb_emit_arith(p, Token_Mul, a, b, t);
  1242. lbValue y = lb_emit_arith(p, Token_Add, x, c, t);
  1243. return y;
  1244. }
  1245. }
  1246. LLVMValueRef llvm_mask_iota(lbModule *m, unsigned start, unsigned count) {
  1247. auto iota = slice_make<LLVMValueRef>(temporary_allocator(), count);
  1248. for (unsigned i = 0; i < count; i++) {
  1249. iota[i] = lb_const_int(m, t_u32, start+i).value;
  1250. }
  1251. return LLVMConstVector(iota.data, count);
  1252. }
  1253. LLVMValueRef llvm_mask_zero(lbModule *m, unsigned count) {
  1254. return LLVMConstNull(LLVMVectorType(lb_type(m, t_u32), count));
  1255. }
  1256. LLVMValueRef llvm_splat(lbProcedure *p, LLVMValueRef value, unsigned count) {
  1257. GB_ASSERT(count > 0);
  1258. if (LLVMIsConstant(value)) {
  1259. LLVMValueRef single = LLVMConstVector(&value, 1);
  1260. if (count == 1) {
  1261. return single;
  1262. }
  1263. LLVMValueRef mask = llvm_mask_zero(p->module, count);
  1264. return LLVMConstShuffleVector(single, LLVMGetUndef(LLVMTypeOf(single)), mask);
  1265. }
  1266. LLVMTypeRef single_type = LLVMVectorType(LLVMTypeOf(value), 1);
  1267. LLVMValueRef single = LLVMBuildBitCast(p->builder, value, single_type, "");
  1268. if (count == 1) {
  1269. return single;
  1270. }
  1271. LLVMValueRef mask = llvm_mask_zero(p->module, count);
  1272. return LLVMBuildShuffleVector(p->builder, single, LLVMGetUndef(LLVMTypeOf(single)), mask, "");
  1273. }
  1274. LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) {
  1275. LLVMTypeRef type = LLVMTypeOf(value);
  1276. GB_ASSERT(LLVMGetTypeKind(type) == LLVMVectorTypeKind);
  1277. LLVMTypeRef elem = LLVMGetElementType(type);
  1278. char const *name = nullptr;
  1279. i32 value_offset = 0;
  1280. i32 value_count = 0;
  1281. switch (LLVMGetTypeKind(elem)) {
  1282. case LLVMHalfTypeKind:
  1283. case LLVMFloatTypeKind:
  1284. case LLVMDoubleTypeKind:
  1285. name = "llvm.vector.reduce.fadd";
  1286. value_offset = 0;
  1287. value_count = 2;
  1288. break;
  1289. case LLVMIntegerTypeKind:
  1290. name = "llvm.vector.reduce.add";
  1291. value_offset = 1;
  1292. value_count = 1;
  1293. break;
  1294. default:
  1295. GB_PANIC("invalid vector type %s", LLVMPrintTypeToString(type));
  1296. break;
  1297. }
  1298. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  1299. GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
  1300. LLVMTypeRef types[1] = {};
  1301. types[0] = type;
  1302. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
  1303. LLVMValueRef values[2] = {};
  1304. values[0] = LLVMConstNull(elem);
  1305. values[1] = value;
  1306. LLVMValueRef call = LLVMBuildCall(p->builder, ip, values+value_offset, value_count, "");
  1307. return call;
  1308. }
  1309. LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) {
  1310. GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b));
  1311. LLVMTypeRef elem = LLVMGetElementType(LLVMTypeOf(a));
  1312. if (LLVMGetTypeKind(elem) == LLVMIntegerTypeKind) {
  1313. return LLVMBuildAdd(p->builder, a, b, "");
  1314. }
  1315. return LLVMBuildFAdd(p->builder, a, b, "");
  1316. }
  1317. LLVMValueRef llvm_vector_mul(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) {
  1318. GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b));
  1319. LLVMTypeRef elem = LLVMGetElementType(LLVMTypeOf(a));
  1320. if (LLVMGetTypeKind(elem) == LLVMIntegerTypeKind) {
  1321. return LLVMBuildMul(p->builder, a, b, "");
  1322. }
  1323. return LLVMBuildFMul(p->builder, a, b, "");
  1324. }
  1325. LLVMValueRef llvm_vector_dot(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) {
  1326. return llvm_vector_reduce_add(p, llvm_vector_mul(p, a, b));
  1327. }