using function names
This commit is contained in:
parent
b404c21bfc
commit
bbd9eb17e0
4 changed files with 48 additions and 18 deletions
|
@ -144,6 +144,7 @@
|
||||||
; ushort (padding)
|
; ushort (padding)
|
||||||
; uint type
|
; uint type
|
||||||
; immediately following the header in memory are the arguments of the expression
|
; immediately following the header in memory are the arguments of the expression
|
||||||
|
; - for functions, a pointer to the name of the function (we don't know where it is yet)
|
||||||
; - for local variables, the 64-bit rbp offset (number to be subtracted from rbp)
|
; - for local variables, the 64-bit rbp offset (number to be subtracted from rbp)
|
||||||
; - for global variables, the 64-bit runtime address
|
; - for global variables, the 64-bit runtime address
|
||||||
; - for constant ints, the 64-bit integral value
|
; - for constant ints, the 64-bit integral value
|
||||||
|
@ -157,6 +158,7 @@
|
||||||
; - for function calls, the function, followed by each of the arguments to the function — info indicates the number of arguments
|
; - for function calls, the function, followed by each of the arguments to the function — info indicates the number of arguments
|
||||||
; File/line number are not stored in expressions.
|
; File/line number are not stored in expressions.
|
||||||
; Note that string literals are stored as constant integers (you can check the type to know what it is)
|
; Note that string literals are stored as constant integers (you can check the type to know what it is)
|
||||||
|
#define EXPRESSION_FUNCTION 198
|
||||||
#define EXPRESSION_LOCAL_VARIABLE 199
|
#define EXPRESSION_LOCAL_VARIABLE 199
|
||||||
#define EXPRESSION_GLOBAL_VARIABLE 200
|
#define EXPRESSION_GLOBAL_VARIABLE 200
|
||||||
#define EXPRESSION_CONSTANT_INT 201
|
#define EXPRESSION_CONSTANT_INT 201
|
||||||
|
@ -241,8 +243,9 @@
|
||||||
; reading the first 16 bits of type data as a word will give this if the type refers to a function pointer.
|
; reading the first 16 bits of type data as a word will give this if the type refers to a function pointer.
|
||||||
#define TYPE2_FUNCTION_POINTER 0x100d
|
#define TYPE2_FUNCTION_POINTER 0x100d
|
||||||
|
|
||||||
; types willl be initialized (in main) so that this refers to the type char*
|
; types willl be initialized (in main) so that these will refer to the proper types
|
||||||
#define TYPE_POINTER_TO_CHAR 20
|
#define TYPE_POINTER_TO_CHAR 20
|
||||||
|
#define TYPE_POINTER_TO_VOID 22
|
||||||
|
|
||||||
; STATEMENTS
|
; STATEMENTS
|
||||||
; In C, note that `if', `while', etc. always have a single statement as their body:
|
; In C, note that `if', `while', etc. always have a single statement as their body:
|
||||||
|
|
|
@ -99,6 +99,11 @@ function types_init
|
||||||
p += 1
|
p += 1
|
||||||
*1p = TYPE_CHAR
|
*1p = TYPE_CHAR
|
||||||
p += 1
|
p += 1
|
||||||
|
p = _types + TYPE_POINTER_TO_VOID
|
||||||
|
*1p = TYPE_POINTER
|
||||||
|
p += 1
|
||||||
|
*1p = TYPE_VOID
|
||||||
|
p += 1
|
||||||
|
|
||||||
*8ptypes_bytes_used = p - types
|
*8ptypes_bytes_used = p - types
|
||||||
return
|
return
|
||||||
|
|
19
05/main.c
19
05/main.c
|
@ -1,12 +1,19 @@
|
||||||
static int g;
|
/* static int g; */
|
||||||
|
|
||||||
int f(int x, int y[3]) {
|
int main() {
|
||||||
int z = 17 +x;
|
int a = exit;
|
||||||
int g[]={1,2,3,4,5};
|
int b[] = {1,2,3};
|
||||||
int h;
|
exit(1);
|
||||||
return *(g + x + z);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* int f(int x, int y[3]) { */
|
||||||
|
/* int z = 17 +x; */
|
||||||
|
/* int g[]={1,2,3,4,5}; */
|
||||||
|
/* int h; */
|
||||||
|
/* funciton(h,g,z); */
|
||||||
|
/* return *(g + x + z); */
|
||||||
|
/* } */
|
||||||
|
|
||||||
/* typedef int AA[sizeof x]; */
|
/* typedef int AA[sizeof x]; */
|
||||||
|
|
||||||
|
|
||||||
|
|
37
05/parse.b
37
05/parse.b
|
@ -654,10 +654,10 @@ function parse_statement
|
||||||
token += 16
|
token += 16
|
||||||
if *1token == SYMBOL_LBRACE goto local_init_lbrace
|
if *1token == SYMBOL_LBRACE goto local_init_lbrace
|
||||||
n = token_next_semicolon_or_comma_not_in_brackets(token)
|
n = token_next_semicolon_or_comma_not_in_brackets(token)
|
||||||
out += 16
|
out += 24
|
||||||
p = expressions_end
|
p = expressions_end
|
||||||
*8out = p
|
*8out = p
|
||||||
out -= 16
|
out -= 24
|
||||||
expressions_end = parse_expression(token, n, p)
|
expressions_end = parse_expression(token, n, p)
|
||||||
p += 4
|
p += 4
|
||||||
type_decay_array_to_pointer(*4p) ; fix typing for `int[] x = {5,6}; int *y = x;`
|
type_decay_array_to_pointer(*4p) ; fix typing for `int[] x = {5,6}; int *y = x;`
|
||||||
|
@ -1764,16 +1764,17 @@ function parse_type_declarators
|
||||||
types_bytes_used += 1
|
types_bytes_used += 1
|
||||||
|
|
||||||
p = suffix + 16
|
p = suffix + 16
|
||||||
if *1p != KEYWORD_VOID goto ftype_has_parameters
|
|
||||||
if *1p == SYMBOL_RPAREN goto ftype_no_parameters ; e.g. int f() { return 17; }
|
if *1p == SYMBOL_RPAREN goto ftype_no_parameters ; e.g. int f() { return 17; }
|
||||||
|
if *1p != KEYWORD_VOID goto ftype_has_parameters
|
||||||
n = p + 16
|
n = p + 16
|
||||||
|
suffix += 16
|
||||||
if *1n != SYMBOL_RPAREN goto ftype_has_parameters
|
if *1n != SYMBOL_RPAREN goto ftype_has_parameters
|
||||||
:ftype_no_parameters
|
:ftype_no_parameters
|
||||||
; special handling of function type with no parameters
|
; special handling of function type with no parameters
|
||||||
out = types + types_bytes_used
|
out = types + types_bytes_used
|
||||||
*1out = 0
|
*1out = 0
|
||||||
types_bytes_used += 1
|
types_bytes_used += 1
|
||||||
suffix += 48
|
suffix += 32
|
||||||
goto type_declarators_loop
|
goto type_declarators_loop
|
||||||
|
|
||||||
|
|
||||||
|
@ -2402,7 +2403,6 @@ function parse_expression
|
||||||
goto expr_find_operator_loop
|
goto expr_find_operator_loop
|
||||||
:expr_find_operator_loop_end
|
:expr_find_operator_loop_end
|
||||||
|
|
||||||
|
|
||||||
if best == 0 goto unrecognized_expression
|
if best == 0 goto unrecognized_expression
|
||||||
|
|
||||||
n = best - tokens
|
n = best - tokens
|
||||||
|
@ -2423,6 +2423,7 @@ function parse_expression
|
||||||
a = out + 4 ; type of first operand
|
a = out + 4 ; type of first operand
|
||||||
out = parse_expression(tokens, best, out) ; first operand
|
out = parse_expression(tokens, best, out) ; first operand
|
||||||
p = best + 16
|
p = best + 16
|
||||||
|
if c == EXPRESSION_CALL goto parse_call
|
||||||
b = out + 4 ; type of second operand
|
b = out + 4 ; type of second operand
|
||||||
if c != EXPRESSION_SUBSCRIPT goto binary_not_subscript
|
if c != EXPRESSION_SUBSCRIPT goto binary_not_subscript
|
||||||
tokens_end -= 16
|
tokens_end -= 16
|
||||||
|
@ -2545,7 +2546,9 @@ function parse_expression
|
||||||
:expr_binary_bad_types
|
:expr_binary_bad_types
|
||||||
bad_types_to_operator(tokens, *4a, *4b)
|
bad_types_to_operator(tokens, *4a, *4b)
|
||||||
|
|
||||||
|
:parse_call
|
||||||
|
byte 0xcc ; @TODO
|
||||||
|
|
||||||
:parse_expr_unary
|
:parse_expr_unary
|
||||||
if c == KEYWORD_SIZEOF goto parse_sizeof
|
if c == KEYWORD_SIZEOF goto parse_sizeof
|
||||||
*1out = unary_op_to_expression_type(c)
|
*1out = unary_op_to_expression_type(c)
|
||||||
|
@ -2849,11 +2852,15 @@ function parse_expression
|
||||||
return out
|
return out
|
||||||
:not_global
|
:not_global
|
||||||
|
|
||||||
in -= 16
|
; it must be a function
|
||||||
token_error(in, .str_undeclared_variable)
|
*1out = EXPRESSION_FUNCTION
|
||||||
:str_undeclared_variable
|
out += 4
|
||||||
string Undeclared variable.
|
*4out = TYPE_POINTER_TO_VOID
|
||||||
byte 0
|
out += 4
|
||||||
|
*8out = a
|
||||||
|
out += 8
|
||||||
|
return out
|
||||||
|
|
||||||
:found_local_variable
|
:found_local_variable
|
||||||
; it's a local variable
|
; it's a local variable
|
||||||
*1out = EXPRESSION_LOCAL_VARIABLE
|
*1out = EXPRESSION_LOCAL_VARIABLE
|
||||||
|
@ -3716,6 +3723,8 @@ function operator_right_associative
|
||||||
byte EXPRESSION_DOT
|
byte EXPRESSION_DOT
|
||||||
byte SYMBOL_LSQUARE
|
byte SYMBOL_LSQUARE
|
||||||
byte EXPRESSION_SUBSCRIPT
|
byte EXPRESSION_SUBSCRIPT
|
||||||
|
byte SYMBOL_LPAREN
|
||||||
|
byte EXPRESSION_CALL
|
||||||
byte 0
|
byte 0
|
||||||
byte 0
|
byte 0
|
||||||
|
|
||||||
|
@ -3791,6 +3800,7 @@ function print_expression
|
||||||
:print_expr_skip_type
|
:print_expr_skip_type
|
||||||
c = *1expression
|
c = *1expression
|
||||||
|
|
||||||
|
if c == EXPRESSION_FUNCTION goto print_expr_function
|
||||||
if c == EXPRESSION_LOCAL_VARIABLE goto print_local_variable
|
if c == EXPRESSION_LOCAL_VARIABLE goto print_local_variable
|
||||||
if c == EXPRESSION_GLOBAL_VARIABLE goto print_global_variable
|
if c == EXPRESSION_GLOBAL_VARIABLE goto print_global_variable
|
||||||
if c == EXPRESSION_CONSTANT_INT goto print_expr_int
|
if c == EXPRESSION_CONSTANT_INT goto print_expr_int
|
||||||
|
@ -3832,6 +3842,11 @@ function print_expression
|
||||||
:str_local_prefix
|
:str_local_prefix
|
||||||
string [rbp-
|
string [rbp-
|
||||||
byte 0
|
byte 0
|
||||||
|
:print_expr_function
|
||||||
|
expression += 8
|
||||||
|
puts(*8expression)
|
||||||
|
expression += 8
|
||||||
|
return expression
|
||||||
:print_global_variable
|
:print_global_variable
|
||||||
puts(.str_global_at)
|
puts(.str_global_at)
|
||||||
expression += 8
|
expression += 8
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue