Skip to content

Commit 41144a8

Browse files
committed
ability to inline at function callsite
closes #306
1 parent f043e0e commit 41144a8

11 files changed

+213
-48
lines changed

doc/langref.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo
8181
8282
SwitchItem = Expression | (Expression "..." Expression)
8383
84-
WhileExpression(body) = option("inline") "while" "(" Expression option(";" Expression) ")" body
84+
WhileExpression(body) = "while" "(" Expression option(";" Expression) ")" body
8585
86-
ForExpression(body) = option("inline") "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
86+
ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
8787
8888
BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression
8989
@@ -127,7 +127,9 @@ MultiplyOperator = "*" | "/" | "%" | "**" | "*%"
127127
128128
PrefixOpExpression = PrefixOp PrefixOpExpression | SuffixOpExpression
129129
130-
SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
130+
SuffixOpExpression = InlineExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
131+
132+
InlineExpression = option("inline") PrimaryExpression
131133
132134
FieldAccessExpression = "." Symbol
133135
@@ -161,6 +163,7 @@ ContainerDecl = option("extern" | "packed") ("struct" | "enum" | "union") "{" ma
161163
## Operator Precedence
162164

163165
```
166+
inline x
164167
x() x[] x.y
165168
!x -x -%x ~x *x &x ?x %x %%x ??x
166169
x{}

src/all_types.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ struct ConstErrValue {
167167
struct ConstBoundFnValue {
168168
FnTableEntry *fn;
169169
IrInstruction *first_arg;
170+
bool is_inline;
170171
};
171172

172173
struct ConstArgTuple {
@@ -192,6 +193,11 @@ enum RuntimeHintMaybe {
192193
RuntimeHintMaybeNonNull,
193194
};
194195

196+
struct ConstFn {
197+
FnTableEntry *fn_entry;
198+
bool is_inline;
199+
};
200+
195201
struct ConstExprValue {
196202
TypeTableEntry *type;
197203
ConstValSpecial special;
@@ -202,7 +208,7 @@ struct ConstExprValue {
202208
// populated if special == ConstValSpecialStatic
203209
BigNum x_bignum;
204210
bool x_bool;
205-
FnTableEntry *x_fn;
211+
ConstFn x_fn;
206212
ConstBoundFnValue x_bound_fn;
207213
TypeTableEntry *x_type;
208214
ConstExprValue *x_maybe;
@@ -366,6 +372,7 @@ enum NodeType {
366372
NodeTypeTypeLiteral,
367373
NodeTypeVarLiteral,
368374
NodeTypeTryExpr,
375+
NodeTypeInlineExpr,
369376
};
370377

371378
struct AstNodeRoot {
@@ -789,6 +796,10 @@ struct AstNodeTypeLiteral {
789796
struct AstNodeVarLiteral {
790797
};
791798

799+
struct AstNodeInlineExpr {
800+
AstNode *body;
801+
};
802+
792803
struct AstNode {
793804
enum NodeType type;
794805
size_t line;
@@ -847,6 +858,7 @@ struct AstNode {
847858
AstNodeErrorType error_type;
848859
AstNodeTypeLiteral type_literal;
849860
AstNodeVarLiteral var_literal;
861+
AstNodeInlineExpr inline_expr;
850862
} data;
851863
};
852864

@@ -1748,6 +1760,7 @@ enum IrInstructionId {
17481760
IrInstructionIdDeclRef,
17491761
IrInstructionIdPanic,
17501762
IrInstructionIdEnumTagName,
1763+
IrInstructionIdSetFnRefInline,
17511764
};
17521765

17531766
struct IrInstruction {
@@ -1943,6 +1956,7 @@ struct IrInstructionCall {
19431956
IrInstruction **args;
19441957
bool is_comptime;
19451958
LLVMValueRef tmp_ptr;
1959+
bool is_inline;
19461960
};
19471961

19481962
struct IrInstructionConst {
@@ -2493,6 +2507,12 @@ struct IrInstructionEnumTagName {
24932507
IrInstruction *target;
24942508
};
24952509

2510+
struct IrInstructionSetFnRefInline {
2511+
IrInstruction base;
2512+
2513+
IrInstruction *fn_ref;
2514+
};
2515+
24962516
static const size_t slice_ptr_index = 0;
24972517
static const size_t slice_len_index = 1;
24982518

src/analyze.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
21292129
case NodeTypeTypeLiteral:
21302130
case NodeTypeVarLiteral:
21312131
case NodeTypeTryExpr:
2132+
case NodeTypeInlineExpr:
21322133
zig_unreachable();
21332134
}
21342135
}
@@ -3251,7 +3252,8 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
32513252
// TODO better hashing algorithm
32523253
return 31643936;
32533254
case TypeTableEntryIdFn:
3254-
return hash_ptr(const_val->data.x_fn);
3255+
return hash_ptr(const_val->data.x_fn.fn_entry) +
3256+
(const_val->data.x_fn.is_inline ? 4133894920 : 3983484790);
32553257
case TypeTableEntryIdTypeDecl:
32563258
return hash_ptr(const_val->data.x_type);
32573259
case TypeTableEntryIdNamespace:
@@ -3697,7 +3699,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
36973699
case TypeTableEntryIdPureError:
36983700
return a->data.x_pure_err == b->data.x_pure_err;
36993701
case TypeTableEntryIdFn:
3700-
return a->data.x_fn == b->data.x_fn;
3702+
return a->data.x_fn.fn_entry == b->data.x_fn.fn_entry &&
3703+
a->data.x_fn.is_inline == b->data.x_fn.is_inline;
37013704
case TypeTableEntryIdBool:
37023705
return a->data.x_bool == b->data.x_bool;
37033706
case TypeTableEntryIdInt:
@@ -3933,8 +3936,9 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39333936
zig_unreachable();
39343937
case TypeTableEntryIdFn:
39353938
{
3936-
FnTableEntry *fn_entry = const_val->data.x_fn;
3937-
buf_appendf(buf, "%s", buf_ptr(&fn_entry->symbol_name));
3939+
FnTableEntry *fn_entry = const_val->data.x_fn.fn_entry;
3940+
const char *inline_str = const_val->data.x_fn.is_inline ? "inline " : "";
3941+
buf_appendf(buf, "%s%s", inline_str, buf_ptr(&fn_entry->symbol_name));
39383942
return;
39393943
}
39403944
case TypeTableEntryIdBlock:
@@ -4011,7 +4015,8 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
40114015
case TypeTableEntryIdBoundFn:
40124016
{
40134017
FnTableEntry *fn_entry = const_val->data.x_bound_fn.fn;
4014-
buf_appendf(buf, "(bound fn %s)", buf_ptr(&fn_entry->symbol_name));
4018+
const char *inline_str = const_val->data.x_bound_fn.is_inline ? "inline " : "";
4019+
buf_appendf(buf, "(%sbound fn %s)", inline_str, buf_ptr(&fn_entry->symbol_name));
40154020
return;
40164021
}
40174022
case TypeTableEntryIdStruct:

src/ast_render.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ static const char *node_type_str(NodeType node_type) {
240240
return "VarLiteral";
241241
case NodeTypeTryExpr:
242242
return "TryExpr";
243+
case NodeTypeInlineExpr:
244+
return "InlineExpr";
243245
}
244246
zig_unreachable();
245247
}
@@ -921,6 +923,12 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
921923
render_node_ungrouped(ar, node->data.unwrap_err_expr.op2);
922924
break;
923925
}
926+
case NodeTypeInlineExpr:
927+
{
928+
fprintf(ar->f, "inline ");
929+
render_node_grouped(ar, node->data.inline_expr.body);
930+
break;
931+
}
924932
case NodeTypeFnDecl:
925933
case NodeTypeParamDecl:
926934
case NodeTypeErrorValueDecl:

src/codegen.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg) {
608608
LLVMBuildLoad(g->builder, ptr_ptr, ""),
609609
LLVMBuildLoad(g->builder, len_ptr, ""),
610610
};
611-
ZigLLVMBuildCall(g->builder, fn_val, args, 2, panic_fn->type_entry->data.fn.calling_convention, "");
611+
ZigLLVMBuildCall(g->builder, fn_val, args, 2, panic_fn->type_entry->data.fn.calling_convention, false, "");
612612
LLVMBuildUnreachable(g->builder);
613613
}
614614

@@ -1773,8 +1773,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
17731773
}
17741774
}
17751775

1776+
bool want_always_inline = (instruction->fn_entry != nullptr &&
1777+
instruction->fn_entry->fn_inline == FnInlineAlways) || instruction->is_inline;
1778+
17761779
LLVMValueRef result = ZigLLVMBuildCall(g->builder, fn_val,
1777-
gen_param_values, (unsigned)gen_param_index, fn_type->data.fn.calling_convention, "");
1780+
gen_param_values, (unsigned)gen_param_index, fn_type->data.fn.calling_convention,
1781+
want_always_inline, "");
17781782

17791783
for (size_t param_i = 0; param_i < fn_type_id->param_count; param_i += 1) {
17801784
FnGenParamInfo *gen_info = &fn_type->data.fn.gen_param_info[param_i];
@@ -2749,6 +2753,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
27492753
case IrInstructionIdSetGlobalLinkage:
27502754
case IrInstructionIdDeclRef:
27512755
case IrInstructionIdSwitchVar:
2756+
case IrInstructionIdSetFnRefInline:
27522757
zig_unreachable();
27532758
case IrInstructionIdReturn:
27542759
return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
@@ -3183,7 +3188,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
31833188
}
31843189
}
31853190
case TypeTableEntryIdFn:
3186-
return fn_llvm_value(g, const_val->data.x_fn);
3191+
return fn_llvm_value(g, const_val->data.x_fn.fn_entry);
31873192
case TypeTableEntryIdPointer:
31883193
{
31893194
render_const_val_global(g, const_val, "");

0 commit comments

Comments
 (0)