Skip to content

Commit

Permalink
optimization levels
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jan 4, 2021
1 parent bb348dc commit 4f31637
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions asm.l
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@

%%

void asm_file(FILE * f) {
void asm_file(FILE * f, int optlevel) {
yyscan_t yyscanner;

yylex_init(&yyscanner);
yyset_in(f, yyscanner);
yyparse(yyscanner);
yyparse(optlevel, yyscanner);
yylex_destroy(yyscanner);
}
8 changes: 5 additions & 3 deletions asm.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
%define api.token.raw
%define parse.error verbose
%locations
%parse-param { int optlevel }
%param { yyscan_t scanner }

%code top {
Expand Down Expand Up @@ -31,7 +32,7 @@

%code {
int yylex(YYSTYPE * yylvalp, YYLTYPE * yyllocp, yyscan_t scanner);
void yyerror(YYLTYPE * yyllocp, yyscan_t unused, const char * msg);
void yyerror(YYLTYPE * yyllocp, int unused_1, yyscan_t unused, const char * msg);
}

%token END 0 "end of file"
Expand All @@ -51,7 +52,7 @@
%start Start
%%
Start
: MaybeLF ToplevelScope { asm_gen(stdout, $2); }
: MaybeLF ToplevelScope { asm_gen(stdout, $2, optlevel); }
;

MaybeLF
Expand Down Expand Up @@ -145,8 +146,9 @@ NumericalConstant

%%

void yyerror(YYLTYPE * yyllocp, yyscan_t unused, const char * msg) {
void yyerror(YYLTYPE * yyllocp, int unused_1, yyscan_t unused, const char * msg) {
(void) unused;
(void) unused_1;
fprintf(stderr, "wsi: %d:%d: %s\n", yyllocp->first_line, yyllocp->first_column, msg);
exit(1);
}
4 changes: 2 additions & 2 deletions asm_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct node_t {
uint32_t line, column;
};

void asm_gen(FILE *, vector(struct node_t));
void asm_optimize(vector(struct node_t) * data);
void asm_gen(FILE *, vector(struct node_t), int optlevel);
void asm_optimize(vector(struct node_t) * data, int optlevel);

#endif
4 changes: 2 additions & 2 deletions asm_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void numeral(FILE * output, int32_t x) {
N;
}

void asm_gen(FILE * output, vector(struct node_t) data) {
void asm_gen(FILE * output, vector(struct node_t) data, int optlevel) {
if(data) {
int32_t x;

Expand Down Expand Up @@ -144,7 +144,7 @@ void asm_gen(FILE * output, vector(struct node_t) data) {
finalize_labels(&s);

// Pass 2: Optimizing the code.
asm_optimize(&data);
asm_optimize(&data, optlevel);

// Pass 3: Generating code.
vector_foreach(struct node_t, it, data) {
Expand Down
6 changes: 4 additions & 2 deletions asm_optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

#define HAS(x) ((uintptr_t) (n - vector_begin(*data) - (x)) > vector_size(*data))

void asm_optimize(vector(struct node_t) * data) {
// optlevel = 1: optimize for size
// optlevel = 2: optimize for speed
void asm_optimize(vector(struct node_t) * data, int optlevel) {
for(uint32_t idx = 0; idx < vector_size(*data); idx++) {
struct node_t * n = (*data) + idx;

switch(n->type) {
case MUL:
if(n->data1.type == IMM_VALUE && n->data1.value == 2) {
if(n->data1.type == IMM_VALUE && n->data1.value == 2 && optlevel == 1) {
// MUL 2 => ADD / ADD ~~ unconditionally saves 2 bytes.
n->data1.type = IMM_NONE;
n->data1.value = 0;
Expand Down
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void disasm(FILE * output, vector(struct instruction_t) program);
void disasm_single(FILE * output, struct instruction_t * it);
int32_t run(struct parse_result_t program, struct state * state, void(*fatal)(char *));
char * compile(struct parse_result_t program);
void asm_file(FILE * f);
void asm_file(FILE * f, int optlevel);

#ifdef JIT

Expand Down
10 changes: 8 additions & 2 deletions wsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void warn(char * message) {
}

int main(int argc, char * argv[]) {
int i, dis = 0, count = 0, aot = 0, jit = 0, masm = 0;
int i, dis = 0, count = 0, aot = 0, jit = 0, masm = 0, optimize = 0;
for(i = 1; i < argc; i++) {
char * arg = argv[i];
if(*arg == '-') {
Expand All @@ -37,6 +37,8 @@ int main(int argc, char * argv[]) {
" -j/--jit: enable the JIT compiler.\n"
#endif
" -m/--masm: run the macro assembler.\n"
" -Os: optimize for size.\n"
" -Of: optimize to produce fast code.\n"
"default operation: run whitespace code.\n"
);
return 1;
Expand All @@ -46,6 +48,10 @@ int main(int argc, char * argv[]) {
count = 1;
} else if(!strcmp(arg, "--masm") || arg[1] == 'm') {
masm = 1;
} else if(!strcmp(arg, "-Os")) {
optimize = 1;
} else if(!strcmp(arg, "-Of")) {
optimize = 2;
} else if(!strcmp(arg, "--aot") || arg[1] == 'a') {
aot = 1;
#ifdef JIT
Expand Down Expand Up @@ -86,7 +92,7 @@ int main(int argc, char * argv[]) {
}

if(masm) {
asm_file(input);
asm_file(input, optimize);
fclose(input);
return 0;
}
Expand Down

0 comments on commit 4f31637

Please sign in to comment.