Add static global variables

This commit is contained in:
Rui Ueyama 2020-09-04 11:21:12 +09:00
parent 148deadd2d
commit 5c50f75153
3 changed files with 10 additions and 1 deletions

View File

@ -461,7 +461,11 @@ static void emit_data(Var *prog) {
if (var->is_function || !var->is_definition) if (var->is_function || !var->is_definition)
continue; continue;
println(" .globl %s", var->name); if (var->is_static)
println(" .local %s", var->name);
else
println(" .globl %s", var->name);
println(" .align %d", var->align); println(" .align %d", var->align);
if (var->init_data) { if (var->init_data) {

View File

@ -301,6 +301,7 @@ static Var *new_lvar(char *name, Type *ty) {
static Var *new_gvar(char *name, Type *ty) { static Var *new_gvar(char *name, Type *ty) {
Var *var = new_var(name, ty); Var *var = new_var(name, ty);
var->next = globals; var->next = globals;
var->is_static = true;
var->is_definition = true; var->is_definition = true;
globals = var; globals = var;
return var; return var;
@ -2190,6 +2191,7 @@ static Token *global_variable(Token *tok, Type *basety, VarAttr *attr) {
Type *ty = declarator(&tok, tok, basety); Type *ty = declarator(&tok, tok, basety);
Var *var = new_gvar(get_ident(ty->name), ty); Var *var = new_gvar(get_ident(ty->name), ty);
var->is_definition = !attr->is_extern; var->is_definition = !attr->is_extern;
var->is_static = attr->is_static;
if (attr->align) if (attr->align)
var->align = attr->align; var->align = attr->align;

View File

@ -1,6 +1,7 @@
#include "test.h" #include "test.h"
int g1, g2[4]; int g1, g2[4];
static int g3 = 3;
int main() { int main() {
ASSERT(3, ({ int a; a=3; a; })); ASSERT(3, ({ int a; a=3; a; }));
@ -64,6 +65,8 @@ int main() {
{ void *x; } { void *x; }
ASSERT(3, g3);
printf("OK\n"); printf("OK\n");
return 0; return 0;
} }