summaryrefslogtreecommitdiff
path: root/05
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-02-13 09:33:05 -0500
committerpommicket <pommicket@gmail.com>2022-02-13 09:33:05 -0500
commit07faf56f7d50bd088318f093e9dc272e5b2affbd (patch)
treec21aa7375eb8fda92cf8d9fc0e7d9c178365bd51 /05
parent8c82a837786c59966410d53aaa128917f7d94c36 (diff)
fix dereferencing function pointer codegen
Diffstat (limited to '05')
-rw-r--r--05/codegen.b10
-rw-r--r--05/main.c4
-rw-r--r--05/parse.b11
3 files changed, 18 insertions, 7 deletions
diff --git a/05/codegen.b b/05/codegen.b
index f824182..51dce21 100644
--- a/05/codegen.b
+++ b/05/codegen.b
@@ -1457,6 +1457,8 @@ function generate_push_expression
if c == EXPRESSION_CONSTANT_INT goto generate_int
if c == EXPRESSION_CONSTANT_FLOAT goto generate_float
+ if c == EXPRESSION_GLOBAL_VARIABLE goto generate_global_variable
+ if c == EXPRESSION_LOCAL_VARIABLE goto generate_local_variable
if c == EXPRESSION_FUNCTION goto generate_function_addr
if c == EXPRESSION_CAST goto generate_cast
if c == EXPRESSION_UNARY_PLUS goto generate_cast ; the unary plus operator just casts to the promoted type
@@ -1473,15 +1475,13 @@ function generate_push_expression
if c == EXPRESSION_BITWISE_XOR goto generate_bitwise_xor
if c == EXPRESSION_LSHIFT goto generate_lshift
if c == EXPRESSION_RSHIFT goto generate_rshift
- if c == EXPRESSION_GLOBAL_VARIABLE goto generate_global_variable
- if c == EXPRESSION_LOCAL_VARIABLE goto generate_local_variable
+ if c == EXPRESSION_ASSIGN goto generate_assign
if c == EXPRESSION_DEREFERENCE goto generate_dereference
if c == EXPRESSION_SUBSCRIPT goto generate_subscript
if c == EXPRESSION_ADDRESS_OF goto generate_address_of
if c == EXPRESSION_DOT goto generate_dot_or_arrow
if c == EXPRESSION_ARROW goto generate_dot_or_arrow
if c == EXPRESSION_COMMA goto generate_comma
- if c == EXPRESSION_ASSIGN goto generate_assign
if c == EXPRESSION_CALL goto generate_call
if c == EXPRESSION_LOGICAL_AND goto generate_logical_and
if c == EXPRESSION_LOGICAL_OR goto generate_logical_or
@@ -1765,8 +1765,12 @@ function generate_push_expression
return expr
:generate_dereference
expr += 8
+ p = expr + 4
expr = generate_push_expression(statement, expr)
+ p = types + *4p
+ if *2p == TYPE2_FUNCTION_POINTER goto deref_function_pointer
generate_stack_dereference(statement, type)
+ :deref_function_pointer ; dereferencing a function pointer does NOTHING
return expr
:generate_subscript
expr += 8
diff --git a/05/main.c b/05/main.c
index 2f40441..65cb111 100644
--- a/05/main.c
+++ b/05/main.c
@@ -12,7 +12,7 @@ long fibonacci(long x) {
}
int main(int argc, char **argv) {
- double x = 3.5;
- return factorial(x);
+ long (*fp)(long) = factorial;
+ return (*fp)(6);
}
diff --git a/05/parse.b b/05/parse.b
index fbdf955..fc67f64 100644
--- a/05/parse.b
+++ b/05/parse.b
@@ -32,6 +32,13 @@ function type_is_array
if *1p == TYPE_ARRAY goto return_1
return 0
+function type_is_function
+ argument type
+ local p
+ p = types + type
+ if *1p == TYPE_FUNCTION goto return_1
+ return 0
+
function functype_return_type
argument ftype
local type
@@ -3190,7 +3197,7 @@ function parse_expression
if c == 0 goto undeclared_variable
*1out = EXPRESSION_FUNCTION
out += 4
- *4out = c
+ *4out = type_create_pointer(c)
out += 4
*8out = a
out += 8
@@ -3360,7 +3367,7 @@ function type_sizeof
; - code generation reasons
if c == TYPE_VOID goto return_1
if c == TYPE_POINTER goto return_8
- if c == TYPE_FUNCTION goto return_8
+ if c == TYPE_FUNCTION goto return_1
if c == TYPE_ARRAY goto sizeof_array
if c == TYPE_STRUCT goto sizeof_struct