braced initializers in local declarations

This commit is contained in:
pommicket 2022-02-05 19:22:47 -05:00
parent ebe96b9e85
commit 41f122f01e
2 changed files with 31 additions and 8 deletions

View file

@ -15,7 +15,12 @@ int h(void) {
int y, r[3], s; int y, r[3], s;
char d, e[5], f, g, *p; char d, e[5], f, g, *p;
int z = 3, R=12+459834-g; int z = 3, R=12+459834-g;
return g; int x[] = {1,2,3,4,5};
struct {
char a,b;
} P[] = {1,2,3,4,5};
static int marker = 0x12345678;
return 5;
} }
/* typedef int AA[sizeof x]; */ /* typedef int AA[sizeof x]; */

View file

@ -392,25 +392,31 @@ function parse_statement
write_statement_header(out, STATEMENT_LOCAL_DECLARATION, token) write_statement_header(out, STATEMENT_LOCAL_DECLARATION, token)
out += 8 out += 8
*8out = local_var_next_rbp_offset *8out = local_var_next_rbp_offset
out += 8 out += 32
*8out = type_sizeof(l_type)
out += 24
p = local_variables p = local_variables
p += block_depth < 3 p += block_depth < 3
l_offset = local_var_next_rbp_offset l_offset = local_var_next_rbp_offset
c = ident_list_lookup(*8p, l_name)
if c != 0 goto local_redeclaration
c = l_offset c = l_offset
c |= l_type < 32 c |= l_type < 32
ident_list_add(*8p, l_name, c) ident_list_add(*8p, l_name, c)
token = l_suffix_end
:local_decl_continue
; we need to calculate the size of the type here, because of stuff like
; int x[] = {1,2,3};
n = type_sizeof(l_type)
out -= 24
*8out = n
out += 24
; advance ; advance
local_var_next_rbp_offset += type_sizeof(l_type) local_var_next_rbp_offset += type_sizeof(l_type)
; align ; align
local_var_next_rbp_offset += 7 local_var_next_rbp_offset += 7
local_var_next_rbp_offset >= 3 local_var_next_rbp_offset >= 3
local_var_next_rbp_offset <= 3 local_var_next_rbp_offset <= 3
token = l_suffix_end
:local_decl_continue
if *1token == SYMBOL_SEMICOLON goto local_decl_loop_end if *1token == SYMBOL_SEMICOLON goto local_decl_loop_end
if *1token == SYMBOL_EQ goto local_decl_initializer if *1token == SYMBOL_EQ goto local_decl_initializer
if *1token != SYMBOL_COMMA goto local_decl_badsuffix if *1token != SYMBOL_COMMA goto local_decl_badsuffix
@ -429,12 +435,24 @@ function parse_statement
token = n token = n
goto local_decl_continue goto local_decl_continue
:local_init_lbrace :local_init_lbrace
byte 0xcc ; @TODO rwdata_end_addr += 7
rwdata_end_addr >= 3
rwdata_end_addr <= 3
out -= 8
*8out = rwdata_end_addr
out += 8
parse_constant_initializer(&token, l_type)
goto local_decl_continue
:local_decl_badsuffix :local_decl_badsuffix
token_error(token, .str_local_decl_badsuffix) token_error(token, .str_local_decl_badsuffix)
:str_local_decl_badsuffix :str_local_decl_badsuffix
string Expected equals, comma, or semicolon after variable declaration. string Expected equals, comma, or semicolon after variable declaration.
byte 0 byte 0
:local_redeclaration
token_error(token, .str_local_redeclaration)
:str_local_redeclaration
string Redeclaration of local variable.
byte 0
:local_decl_loop_end :local_decl_loop_end
token += 16 ; skip semicolon token += 16 ; skip semicolon
goto parse_statement_ret goto parse_statement_ret