Add static global variables

This commit is contained in:
Rui Ueyama 2020-09-04 11:21:12 +09:00
parent 30b3e216cd
commit eb85527656
3 changed files with 10 additions and 1 deletions

View File

@ -466,7 +466,11 @@ static void emit_data(Obj *prog) {
if (var->is_function || !var->is_definition)
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);
if (var->init_data) {

View File

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

View File

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