Skip to content
Permalink
Browse files

A file tree_analysis.c is added. Listing of string literals is done i…

…n analysis().
  • Loading branch information...
moizumi99 committed Dec 27, 2018
1 parent ab95492 commit 269fcea019c7f4e4f027305225b6f56384a0b8ef
Showing with 55 additions and 26 deletions.
  1. +1 −1 Makefile
  2. +4 −3 m99cc.h
  3. +3 −1 main.c
  4. +7 −21 parse.c
  5. +1 −0 parse_test.c
  6. +39 −0 tree_analysis.c
@@ -1,5 +1,5 @@
CFLAGS=-Wall -std=c11 -g -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast
SRCS=main.c codegen.c parse.c token.c util.c dtype.c token_test.c parse_test.c util_test.c dtype_test.c
SRCS=main.c codegen.c parse.c token.c util.c dtype.c token_test.c parse_test.c util_test.c dtype_test.c tree_analysis.c
OBJS=$(SRCS:.c=.o)

m99cc: $(OBJS)
@@ -125,6 +125,9 @@ Vector *tokenize(char *p);
Vector *parse(Vector *tokens_input);
void gen_node(Node *node);

// tree_analysis.c.
Vector *analysis(Vector *program_code);

// codegen.c
void gen_program();

@@ -139,11 +142,9 @@ Map *new_map();
void map_put(Map *map, char *key, void *val);
void *map_get(Map *map, char *key);

/* void *get_symbol_address(Map *symbols, char *name); */
/* int get_symbol_size(Map *symbols, char *name); */
/* int get_symbol_type(char *name); */
int data_size(DataType *dtype);


// Unit tests.
void runtest();
void runtest_tokenize();
4 main.c
@@ -70,7 +70,7 @@ int main(int argc, char **argv) {

// open input
if (srcfile == NULL) {
fprintf(stderr, "Cant open file %s\n", argv[1]);
fprintf(stderr, "Can't open file %s\n", argv[1]);
exit(1);
}
fseek( srcfile , 0L , SEEK_END);
@@ -95,6 +95,8 @@ int main(int argc, char **argv) {
// Parse
program_code = parse(tokens);

program_code = analysis(program_code);

if (dump_symbols_enable) {
fprintf(stderr, "Global Symbols: \n");
dump_symbols(global_symbols);
28 parse.c
@@ -7,12 +7,12 @@

#define GET_TOKEN(T, I) (*((Token *)(T)->data[(I)]))

static Vector *tokens;
extern Vector *string_literals;

extern Map *global_symbols;
extern Vector *local_symbols;

static Vector *tokens;
static Map *current_local_symbols;
static int pos;

enum {
SC_GLOBAL, // Global scope.
@@ -34,19 +34,15 @@ char *create_string_in_heap(char *str, int len) {
return str_mem;
}

int pos;

static int str_counter = 0;

Node *new_node_str(char *name, int len) {
Node *node = malloc(sizeof(Node));
node->ty = ND_STR;
node->lhs = NULL;
node->rhs = NULL;
node->val = str_counter;
node->name = NULL;
node->val = 0;
node->name = create_string_in_heap(name, len);
node->block = NULL;
vec_push(string_literals, create_string_in_heap(name, len));
return node;
}

@@ -104,17 +100,6 @@ void add_global_symbol(char *name_perm, int type, int num,
map_put(global_symbols, name_perm, (void *)new_symbol);
}

/* int get_symbol_type(char *name) { */
/* Symbol *tmp_symbol = map_get(current_local_symbols, name); */
/* if (tmp_symbol == NULL) { */
/* tmp_symbol = map_get(global_symbols, name); */
/* } */
/* if (tmp_symbol->data_type == NULL) { */
/* return DT_INVALID; */
/* } */
/* return tmp_symbol->data_type->dtype; */
/* } */

Symbol *get_symbol(Map *global_symbol_table, Map *local_symbol_table, Node *node) {
Symbol *s = map_get(local_symbol_table, node->name);
if (s == NULL) {
@@ -339,7 +324,7 @@ int get_data_step_from_data_type(DataType *data_type) {
if (data_type->pointer_type->dtype == DT_CHAR) {
return 1;
}
// Suppport other data steps, like short.
// TODO: Suppport other data steps, like short.
return 8;
}

@@ -365,6 +350,7 @@ Node *expression(int priority) {
int node_type = get_node_type_from_token(token_type);
Node *rhs = expression(priority);
if (node_type == '+' || node_type == '-') {
// If adding number to pointer, adjust the size.
DataType *ldt = get_node_data_type(lhs);
DataType *rdt = get_node_data_type(rhs);
if (ldt->dtype == DT_PNT && rdt->dtype != DT_PNT) {
@@ -32,6 +32,7 @@ char *get_type(int ty) {
case ND_DEFINITION: return "ND_DEFINITION";
case ND_DATATYPE: return "ND_DATATYPE";
case ND_DEREF: return "ND_DEREF";
case ND_STR: return "ND_STR";
default:
if (ty < 256) {
num[0] = (char) ty;
@@ -0,0 +1,39 @@
#include "m99cc.h"
#include <stdlib.h>

extern Vector *string_literals;
static int str_counter;

void list_string_in_code(Vector *code);

void list_string_in_node(Node *node) {
if (node == NULL) {
return;
}
if (node->ty == ND_STR) {
node->val = str_counter++;
vec_push(string_literals, node->name);
return;
}
list_string_in_node(node->lhs);
list_string_in_node(node->rhs);
list_string_in_code(node->block);
}

void list_string_in_code(Vector *code) {
if (code == NULL) {
return;
}
Node *node;
for (int i = 0; (node = code->data[i]); i++) {
list_string_in_node(node);
}
}

Vector *analysis(Vector *program_code) {
// Extract string literals.
str_counter = 0;
list_string_in_code(program_code);
// TODO: add analysis of pointer addresses.
return program_code;
}

0 comments on commit 269fcea

Please sign in to comment.
You can’t perform that action at this time.