Skip to content
Snippets Groups Projects
Commit d74e3083 authored by zhaochaoyi's avatar zhaochaoyi
Browse files

stop code gen DAG

parent 64532eb6
No related branches found
No related tags found
No related merge requests found
typedef struct CodeList_t CodeList_t;
typedef struct Operand Operand;
typedef struct InterCode InterCode;
static char * temp_var_prefix = "t", * label_name = "label";
struct Operand {
enum {
NONTYPE,VARIABLE, CONSTANT, ADDRESS, FUNCTION, GOTO, ORIGIN, RELOP, DEC, INT_CONST,
} kind;
union {
int var_no;
char * value;
char * f_name;
int goto_id;
char * id_name;
char * relop;
int dec;
int int_const;
} var;
};
struct InterCode {
enum {
T_LABEL,T_FUNCTION,T_ASSIGN,T_ADD, T_MINUS, T_MUL, T_DIV, T_ADDR,
T_A_STAR, T_STAR_A, T_GO, T_IF, T_RETURN, T_DEC, T_ARG, T_CALL,
T_PARAM, T_READ, T_WRITE,
} kind;
int line,block;
union {
struct { Operand id; } label;
struct { Operand arg1; } go;
struct { Operand func; } function;
struct { Operand arg1; } ret;
struct { Operand arg1; } arg;
struct { Operand arg1; } param;
struct { Operand arg1; } read;
struct { Operand arg1; } write;
struct { Operand arg1,arg2; } assign;
struct { Operand arg1,arg2; } a_star;
struct { Operand arg1,arg2; } star_a;
struct { Operand arg1,arg2; } addr;
struct { Operand arg1,arg2; } dec;
struct { Operand arg1,arg2; } call;
struct { Operand arg1,arg2,arg3; } add;
struct { Operand arg1,arg2,arg3; } minus;
struct { Operand arg1,arg2,arg3; } mul;
struct { Operand arg1,arg2,arg3; } div;
struct { Operand arg1,arg2,arg3,op; } ifgoto;
struct { Operand arg1,arg2,arg3,arg4; };
struct { Operand args[4]; };
} op;
InterCode * next, * prev;
};
struct CodeList_t {
InterCode head,tail;
};
typedef struct InterCode_Table_t {
}InterCode_Table_t;
#include "intercode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern CodeList_t code_list;
typedef struct Info_Use_later {
enum {
NONEXIST, ACTIVATE , DEACTIVATE ,
} type;
InterCode * connect;
}Info_Use_later;
typedef struct Code_Block_t {
int first_code,end_code;
InterCode * head,* tail;
Info_Use_later * info;
}Code_Block_t;
Code_Block_t * code_block;
static void code_optimizer_init(int size);
static void code_optimizer_finish(int size);
static int partition_code_block();
static CodeList_t * code_optimizer_code_blocks(int size);
static CodeList_t * code_optimizer_block(Code_Block_t * block);
CodeList_t * code_optimizer(int size) {
code_optimizer_init(size);
int code_block_size = partition_code_block();
CodeList_t * ret = code_optimizer_code_blocks(size);
code_optimizer_finish(code_block_size);
return ret;
}
static void code_optimizer_init(int size) {
code_block = (Code_Block_t *) malloc(sizeof(Code_Block_t) * size);
}
static void code_optimizer_finish(int size) {
for(int i = 0;i < size;i++) {
free(code_block[i].info);
}
free(code_block);
}
static int partition_code_block() {
InterCode * cur = code_list.head.next;
int id = -1;
code_block[++id].first_code = cur->line;
code_block[id].head = cur;
cur = cur->next;
while (cur != &code_list.tail) {
cur->block = id;
switch (cur->kind) {
case T_GO:
case T_IF:
code_block[id].end_code = cur->line;
code_block[id].tail = cur;
id++;
code_block[id].first_code = cur->line + 1;
code_block[id].head = cur->next;
break;
case T_LABEL:
if (code_block[id].first_code != cur->line) {
code_block[id].end_code = cur->line - 1;
code_block[id].tail = cur->prev;
id++;
code_block[id].first_code = cur->line;
code_block[id].head = cur;
}
break;
default:;
}
cur = cur->next;
}
return id;
}
//static Info_Use_later * search_info(int var_no,Code_Block_t * block) {
// return &block->info[var_no - block->first_code];
//}
//static void code_optimizer_info_use_later(Code_Block_t * block) {
//#define BIAS(A) ((A) - block->first_code)
// block->info = (Info_Use_later *) malloc(3 * (block->end_code - block->first_code + 1) * sizeof(Info_Use_later));
// memset(block->info,0, (block->end_code - block->first_code + 1) * sizeof(Info_Use_later));
// for(InterCode * cur = block->tail;cur != block->head; cur = cur->prev) {
// switch (cur->kind) {
// case T_ASSIGN:
// break;
// case T_ADD:
// case T_MINUS:
// case T_MUL:
// case T_DIV:
// block->info[BIAS(cur->line)].connect =
// }
// }
//}
typedef struct Graph_Node_t {
int var_list;
int kind; //use inter code kind
}Graph_Node_t;
static CodeList_t * code_optimizer_code_blocks(int size) {
CodeList_t * ret = (CodeList_t*) malloc(sizeof(CodeList_t));
ret->head.next = &ret->tail;
ret->tail.prev = &ret->head;
for(int i = 0;i < size;i++) {
//TODO
//分析每个基本块的后续使用信息,P340
//构建DAG,P343
//消除公共子表达式
CodeList_t * temp = code_optimizer_block(&code_block[i]);
ret->tail.prev = temp->head.next;
temp->head.next->prev = ret->tail.prev;
temp->tail.prev->next = &ret->tail;
ret->tail.prev = temp->tail.prev;
free(temp);
}
}
static CodeList_t * code_optimizer_block(Code_Block_t * block) {
CodeList_t * ret = (CodeList_t*) malloc(sizeof(CodeList_t));
ret->head.next = &ret->tail;
ret->tail.prev = &ret->head;
//消除公共子表达式,
}
......@@ -17,9 +17,6 @@ int main(int argc,char *argv[]) {
if (argc <= 1) {
printf("Usage:%s $FILE\n",argv[0]);
}
#ifndef FINAL
Log("\n\n\n%s:\n",argv[i]);
#endif
yyinit();
FILE * f = fopen(argv[1], "r");
......@@ -36,7 +33,6 @@ int main(int argc,char *argv[]) {
//semantic_check->main(tree->root);
freopen(argv[2],"w",stdout);
translate();
}
//end_free();
return 0;
......
No preview for this file type
#include "data.h"
#include "debug.h"
#include "intercode.h"
#include <stdio.h>
typedef struct CodeList_t CodeList_t;
typedef struct Operand Operand;
typedef struct InterCode InterCode;
static Type Is_Top_Addr = {
.kind = REMAINED,
};
static char * temp_var_prefix = "t", * label_name = "label";
struct Operand {
enum {
VARIABLE, CONSTANT, ADDRESS, FUNCTION, GOTO, ORIGIN, RELOP, DEC, INT_CONST,
} kind;
union {
int var_no;
char * value;
char * f_name;
int goto_id;
char * id_name;
char * relop;
int dec;
int int_const;
} var;
};
struct InterCode {
enum {
T_LABEL,T_FUNCTION,T_ASSIGN,T_ADD, T_MINUS, T_MUL, T_DIV, T_ADDR,
T_A_STAR, T_STAR_A, T_GO, T_IF, T_RETURN, T_DEC, T_ARG, T_CALL,
T_PARAM, T_READ, T_WRITE,
} kind;
union {
struct { Operand id; } label;
struct { Operand arg1; } go;
struct { Operand func; } function;
struct { Operand arg1; } ret;
struct { Operand arg1; } arg;
struct { Operand arg1; } param;
struct { Operand arg1; } read;
struct { Operand arg1; } write;
struct { Operand arg1,arg2; } assign;
struct { Operand arg1,arg2; } a_star;
struct { Operand arg1,arg2; } star_a;
struct { Operand arg1,arg2; } addr;
struct { Operand arg1,arg2; } dec;
struct { Operand arg1,arg2; } call;
struct { Operand arg1,arg2,arg3; } add;
struct { Operand arg1,arg2,arg3; } minus;
struct { Operand arg1,arg2,arg3; } mul;
struct { Operand arg1,arg2,arg3; } div;
struct { Operand arg1,arg2,arg3,op; } ifgoto;
struct { Operand arg1,arg2,arg3,arg4; };
} op;
InterCode * next, * prev;
};
struct CodeList_t {
InterCode head,tail;
};
static void codelist_insert(CodeList_t * this,InterCode * pos, InterCode * cur);
static void codelist_display(CodeList_t * this);
......@@ -162,8 +107,8 @@ static void gencode(int kind,...) {
panic("Wrong");
}
va_end(ap);
intercode_display(code);
printf("\n");
// intercode_display(code);
// printf("\n");
codelist_insert(&code_list,code_list.tail.prev,code);
}
......@@ -184,6 +129,8 @@ static void codelist_init(CodeList_t * this) {
}
static void codelist_insert(CodeList_t * this,InterCode * pos, InterCode * cur) {
static int inter_code_line = 0;
cur->line = ++inter_code_line;
InterCode * end = pos;
end->next = cur;
cur->next = &this->tail;
......@@ -356,28 +303,9 @@ static void operand_display(Operand * op) {
}
}
static void code_list_optimizer(CodeList_t * this) {
int size = genvar() + 1;
struct map_table {
Operand arg;
};
struct map_table * map = (struct map_table *) malloc(sizeof(struct map_table) * size);
memset(map,0, sizeof(struct map_table) * size);
InterCode * cur = this->head.next, * temp;
while (cur != &this->tail) {
temp = cur->next;
if(cur->kind == T_ASSIGN) {
map[cur->op.assign.arg1.var.var_no].arg = cur->op.assign.arg2;
cur->prev->next = temp;
temp->prev = cur->prev;
free(cur);
} else {
if(cur->op.arg1.kind == VARIABLE && map[cur->op.arg1.var.var_no].arg.var.var_no) {
}
}
cur = temp;
}
static void code_list_optimizer(CodeList_t * this) {
}
......@@ -428,7 +356,8 @@ void translate() {
translate_Insert_Node(translate_Creat_Node("write",write_type,-1));
translate_Program(tree->root);
// codelist_display(&code_list);
code_list_optimizer(&code_list);
codelist_display(&code_list);
}
static int translate_getsize(const Type * type) {
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment