Skip to content

Commit

Permalink
feat: support initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
diohabara committed Jul 28, 2023
1 parent bae68dc commit 5b8b15d
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 98 deletions.
55 changes: 0 additions & 55 deletions .gdb_history

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tmp*

# for debug
test
.gdb_history
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"files.associations": {
"*.v": "coq",
"*.asm": "gas",
"*.ts": "typescript",
"*.pl": "prolog",
"*.plt": "prolog",
"stdbool.h": "c",
"tests": "cpp"
"tests": "cpp",
"initializer_list": "c"
},
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools"
}
22 changes: 20 additions & 2 deletions ccc.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Type Type;
typedef struct Member Member;
typedef struct Initializer Initializer;
//
// tokenize.c
//
Expand All @@ -33,6 +35,7 @@ struct Token {
void error(char *fmt, ...);
void error_at(char *loc, char *fmt, ...);
void error_tok(Token *tok, char *fmt, ...);
void warn_tok(Token *tok, char *fmt, ...);
Token *peek(char *s);
Token *consume(char *op);
char *strndup(char *p, int len);
Expand All @@ -59,8 +62,7 @@ struct Var {
// local variable
int offset; // Offset from RBP
// global variable
char *contents;
int cont_len;
Initializer *initializer;
};
typedef struct VarList VarList;
struct VarList {
Expand Down Expand Up @@ -158,11 +160,27 @@ struct Node {
Member *member;
};

// global variable initializer
// global variable can be initialized either by a constant expression or a
// pointer to another global variable
struct Initializer {
Initializer *next;

// constant expression
int sz;
long val;

// reference to another global variable
char *label;
long addend;
};

typedef struct Function Function;
struct Function {
Function *next;
char *name;
VarList *params;
bool is_static;
Node *node;
VarList *locals;
int stack_size;
Expand Down
18 changes: 14 additions & 4 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,20 +554,30 @@ void emit_data(Program *prog) {
for (VarList *vl = prog->globals; vl; vl = vl->next) {
Var *var = vl->var;
printf("%s:\n", var->name);
if (!var->contents) {
if (!var->initializer) {
printf(" .zero %d\n", size_of(var->ty, var->tok));
continue;
}
for (int i = 0; i < var->cont_len; i++) {
printf(" .byte %d\n", var->contents[i]);
for (Initializer *init = var->initializer; init; init = init->next) {
if (init->label) {
printf(" .quad %s%+ld\n", init->label, init->addend);
continue;
}
if (init->sz == 1) {
printf(" .byte %ld\n", init->val);
} else {
printf(" .%dbyte %ld\n", init->sz, init->val);
}
}
}
}

void emit_text(Program *prog) {
printf(".text\n");
for (Function *fn = prog->fns; fn; fn = fn->next) {
printf(".global %s\n", fn->name);
if (!fn->is_static) {
printf(".global %s\n", fn->name);
}
printf("%s:\n", fn->name);
funcname = fn->name;

Expand Down
Loading

0 comments on commit 5b8b15d

Please sign in to comment.