start local static declarations

This commit is contained in:
pommicket 2022-02-05 15:50:49 -05:00
parent 267b52814e
commit c5e2556d31
2 changed files with 234 additions and 196 deletions

View file

@ -4,8 +4,11 @@ int f(void) {
continue;a:break;return;return 6+3<<sizeof(int);
goto lbl1;
case 77:;return 92834;
static int x = 0x12345;
}
/* typedef int AA[sizeof x]; */
/* typedef struct { */
/* int i[41]; */

View file

@ -38,6 +38,24 @@ function structure_is_union
; parse a translation unit
function parse_tokens
argument tokens
local token
token = tokens
:parse_tokens_loop
if *1token == TOKEN_EOF goto parse_tokens_eof
parse_toplevel_declaration(&token, global_variables)
goto parse_tokens_loop
:parse_tokens_eof
return
; also handles static declarations inside functions
; advances *p_token past semicolon
; static_vars = where to put static variables
function parse_toplevel_declaration
argument p_token
argument static_vars
local token
local ident
local type
@ -55,10 +73,9 @@ function parse_tokens
local is_extern
local out
token = tokens
:parse_tokens_loop
token = *8p_token
is_extern = 0
if *1token == TOKEN_EOF goto parse_tokens_eof
if *1token == KEYWORD_STATIC goto parse_static_toplevel_decl
if *1token == KEYWORD_EXTERN goto parse_extern_toplevel_decl
if *1token == KEYWORD_TYPEDEF goto parse_typedef
@ -66,10 +83,13 @@ function parse_tokens
b = token_is_type(token)
if b != 0 goto parse_toplevel_decl
token_error(token, .str_bad_statement)
:str_bad_statement
string Bad statement.
token_error(token, .str_bad_decl)
:str_bad_decl
string Bad declaration.
byte 0
:parse_tld_ret
*8p_token = token
return
:parse_static_toplevel_decl
token += 16 ; we don't care that this is static
goto parse_toplevel_decl
@ -121,7 +141,7 @@ function parse_tokens
goto tl_decl_loop
:tl_decl_loop_done
token += 16 ; skip semicolon
goto parse_tokens_loop
goto parse_tld_ret
:tl_decl_no_ident
token_error(prefix_end, .str_tl_decl_no_ident)
@ -136,22 +156,23 @@ function parse_tokens
:parse_tld_no_initializer
p = types + type
if *1p == TYPE_FUNCTION goto parse_tl_decl_cont ; ignore function declarations -- we do two passes anyways
b = ident_list_lookup(global_variables, name)
b = ident_list_lookup(static_vars, name)
if b != 0 goto global_redefinition
c = type < 32
c |= rwdata_end_addr
ident_list_add(global_variables, name, c)
ident_list_add(static_vars, name, c)
; just skip forward by the size of this variable -- it'll automatically be filled with 0s.
rwdata_end_addr += type_sizeof(type)
goto parse_tl_decl_cont
:parse_tld_initializer
p = types + type
if *1p == TYPE_FUNCTION goto function_initializer
b = ident_list_lookup(global_variables, name)
b = ident_list_lookup(static_vars, name)
if b != 0 goto global_redefinition
token += 16 ; skip =
c = type < 32
c |= rwdata_end_addr
ident_list_add(global_variables, name, c)
ident_list_add(static_vars, name, c)
parse_constant_initializer(&token, type)
goto parse_tl_decl_cont
:global_redefinition
@ -165,6 +186,7 @@ function parse_tokens
string Functions should not have initializers.
byte 0
:parse_function_definition
if block_depth != 0 goto nested_function
p = types + type
; @TODO: parameters
; @NOTE: remember to turn array members into pointers
@ -180,7 +202,7 @@ function parse_tokens
function_stmt_data_bytes_used = out - function_stmt_data
ident_list_add(function_statements, name, p)
print_statement(p)
goto parse_tokens_loop
goto parse_tld_ret
:stmtdepth_internal_err
token_error(token, .str_stmtdepth_internal_err)
@ -192,7 +214,13 @@ function parse_tokens
:str_lbrace_after_declaration
string Opening { after declaration of non-function.
byte 0
:nested_function
token_error(token, .str_nested_function)
:str_nested_function
string Nested function.
byte 0
:parse_typedef
if block_depth > 0 goto local_typedef
base_type = token + 16
base_type_end = type_get_base_end(base_type)
@ -237,7 +265,14 @@ function parse_tokens
goto typedef_loop
:typedef_loop_end
token += 16 ; skip semicolon
goto parse_tokens_loop
goto parse_tld_ret
:local_typedef
; @NONSTANDARD
; we could add an extra "typedefs" argument to this function to fix this.
token_error(token, .str_local_typedef)
:str_local_typedef
string typedefs inside functions are not supported.
byte 0
:typedef_no_ident
token_error(token, .str_typedef_no_ident)
:str_typedef_no_ident
@ -253,8 +288,6 @@ function parse_tokens
:str_typedef_redefinition
string typedef redefinition.
byte 0
:parse_tokens_eof
return
; write type, file, and line info for statement
function write_statement_header
@ -281,7 +314,6 @@ function parse_statement
local c
local n
out = *8p_out
token = *8p_token
@ -321,7 +353,10 @@ function parse_statement
*8p_out = out
return
:stmt_static_declaration
byte 0xcc ; @TODO
p = block_static_variables
p += block_depth < 3
parse_toplevel_declaration(&token, *8p)
goto parse_statement_ret
:stmt_break
write_statement_header(out, STATEMENT_BREAK, token)
token += 16