diff options
| author | gingerBill <bill@gingerbill.org> | 2020-08-02 14:59:39 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2020-08-02 14:59:39 +0100 |
| commit | d1d5f61230608ed818fc832cbb5979da312db5d1 (patch) | |
| tree | 8b122fe2daa69a06e101dea181a4e6df68e31ee8 /src | |
| parent | 0aaab849382aa0cbce7234ec386f88161b79ca07 (diff) | |
Add `intrinsics.alloca`
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.cpp | 54 | ||||
| -rw-r--r-- | src/checker_builtin_procs.hpp | 4 | ||||
| -rw-r--r-- | src/ir.cpp | 17 | ||||
| -rw-r--r-- | src/ir_print.cpp | 10 | ||||
| -rw-r--r-- | src/llvm_backend.cpp | 13 |
5 files changed, 94 insertions, 4 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index a29cdac36..0633b20ea 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5408,6 +5408,60 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 break; } + case BuiltinProc_alloca: + { + Operand sz = {}; + Operand al = {}; + + check_expr(c, &sz, ce->args[0]); + if (sz.mode == Addressing_Invalid) { + return false; + } + check_expr(c, &al, ce->args[1]); + if (al.mode == Addressing_Invalid) { + return false; + } + convert_to_typed(c, &sz, t_int); if (sz.mode == Addressing_Invalid) return false; + convert_to_typed(c, &al, t_int); if (al.mode == Addressing_Invalid) return false; + + if (!is_type_integer(sz.type) || !is_type_integer(al.type)) { + error(operand->expr, "Both parameters to '%.*s' must integers", LIT(builtin_name)); + return false; + } + + if (sz.mode == Addressing_Constant) { + i64 i_sz = exact_value_to_i64(sz.value); + if (i_sz < 0) { + error(sz.expr, "Size parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_sz); + return false; + } + } + if (al.mode == Addressing_Constant) { + i64 i_al = exact_value_to_i64(al.value); + if (i_al < 0) { + error(al.expr, "Alignment parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_al); + return false; + } + + if (i_al > 1<<29) { + error(al.expr, "Alignment parameter to '%.*s' must not exceed '1<<29', got %lld", LIT(builtin_name), cast(long long)i_al); + return false; + } + + if (!gb_is_power_of_two(cast(isize)i_al) && i_al != 0) { + error(al.expr, "Alignment parameter to '%.*s' must be a power of 2 or 0, got %lld", LIT(builtin_name), cast(long long)i_al); + return false; + } + } else { + error(al.expr, "Alignment parameter to '%.*s' must be constant", LIT(builtin_name)); + } + + operand->type = t_u8_ptr; + operand->mode = Addressing_Value; + break; + } + + case BuiltinProc_cpu_relax: operand->mode = Addressing_NoValue; break; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index ea6ba82f9..3e2c920b5 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -36,6 +36,7 @@ enum BuiltinProcId { BuiltinProc_simd_vector, BuiltinProc_soa_struct, + BuiltinProc_alloca, BuiltinProc_cpu_relax, BuiltinProc_atomic_fence, @@ -220,7 +221,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("simd_vector"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type {STR_LIT("soa_struct"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type - {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, + {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, {STR_LIT("atomic_fence"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, {STR_LIT("atomic_fence_acq"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, diff --git a/src/ir.cpp b/src/ir.cpp index 0fa842852..bfed55083 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -200,7 +200,7 @@ gbAllocator ir_allocator(void) { IR_INSTR_KIND(ZeroInit, struct { irValue *address; }) \ IR_INSTR_KIND(Store, struct { irValue *address, *value; bool is_volatile; }) \ IR_INSTR_KIND(Load, struct { Type *type; irValue *address; i64 custom_align; }) \ - IR_INSTR_KIND(InlineCode, struct { BuiltinProcId id; Array<irValue *> operands; }) \ + IR_INSTR_KIND(InlineCode, struct { BuiltinProcId id; Array<irValue *> operands; Type *type; }) \ IR_INSTR_KIND(AtomicFence, struct { BuiltinProcId id; }) \ IR_INSTR_KIND(AtomicStore, struct { \ irValue *address, *value; \ @@ -741,6 +741,8 @@ gb_inline bool ir_min_dep_entity(irModule *m, Entity *e) { Type *ir_instr_type(irInstr *instr) { switch (instr->kind) { + case irInstr_InlineCode: + return instr->InlineCode.type; case irInstr_Local: return instr->Local.type; case irInstr_Load: @@ -1093,11 +1095,12 @@ irValue *ir_instr_load(irProcedure *p, irValue *address) { return v; } -irValue *ir_instr_inline_code(irProcedure *p, BuiltinProcId id, Array<irValue *> operands) { +irValue *ir_instr_inline_code(irProcedure *p, BuiltinProcId id, Array<irValue *> const &operands, Type *type) { irValue *v = ir_alloc_instr(p, irInstr_InlineCode); irInstr *i = &v->Instr; i->InlineCode.id = id; i->InlineCode.operands = operands; + i->InlineCode.type = type; return v; } @@ -7287,8 +7290,16 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu // "Intrinsics" + case BuiltinProc_alloca: + { + auto args = array_make<irValue *>(heap_allocator(), 2); + args[0] = ir_emit_conv(proc, ir_build_expr(proc, ce->args[0]), t_i32); + args[1] = ir_build_expr(proc, ce->args[1]); + return ir_emit(proc, ir_instr_inline_code(proc, id, args, t_u8_ptr)); + } + case BuiltinProc_cpu_relax: - return ir_emit(proc, ir_instr_inline_code(proc, id, {})); + return ir_emit(proc, ir_instr_inline_code(proc, id, {}, nullptr)); case BuiltinProc_atomic_fence: case BuiltinProc_atomic_fence_acq: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index fb09ad471..9cd7b2384 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -1539,6 +1539,16 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) { case irInstr_InlineCode: { switch (instr->InlineCode.id) { + case BuiltinProc_alloca: + ir_fprintf(f, "%%%d = ", value->index); + ir_write_str_lit(f, "alloca i8, "); + ir_print_type(f, m, ir_type(instr->InlineCode.operands[0])); + ir_write_str_lit(f, " "); + ir_print_value(f, m, instr->InlineCode.operands[0], ir_type(instr->InlineCode.operands[0])); + ir_write_str_lit(f, ", align "); + ir_print_value(f, m, instr->InlineCode.operands[1], t_i32); + break; + case BuiltinProc_cpu_relax: ir_write_str_lit(f, "call void asm sideeffect \"pause\", \"\"()"); break; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 734003065..5b2ad6068 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -7778,6 +7778,19 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, // "Intrinsics" + + case BuiltinProc_alloca: + { + lbValue sz = lb_build_expr(p, ce->args[0]); + i64 al = exact_value_to_i64(type_and_value_of_expr(ce->args[1]).value); + + lbValue res = {}; + res.type = t_u8_ptr; + res.value = LLVMBuildArrayAlloca(p->builder, lb_type(p->module, t_u8), sz.value, ""); + LLVMSetAlignment(res.value, cast(unsigned)al); + return res; + } + case BuiltinProc_cpu_relax: // TODO(bill): BuiltinProc_cpu_relax // ir_write_str_lit(f, "call void asm sideeffect \"pause\", \"\"()"); |