AST.c


    1 #include <stdio.h>
    2 #include <stdarg.h>
    3 #include <string.h>
    4 #include <assert.h>
    5 #include "xcc.h"
    6 #include "AST.h"
    7 #include "symbol.h"
    8 
    9 char *get_string_label(char *data, struct AST *ast) {
   10 	struct AST *ast1 = ast;
   11 	struct String *string;
   12 
   13 	while (ast1->ast_type != AST_function_definition)
   14 		ast1 = ast1->parent;
   15 	for (string = ast1->u2.func.string; string != NULL; string = string->next)
   16 		if (!strcmp(data, string->data))
   17 			return string->label;
   18 	assert(0);
   19 	return NULL;
   20 }
   21 
   22 char *get_id_index(char *name, struct AST *ast) {
   23 	int i;
   24 	char *index;
   25 	struct AST *ast1 = ast;
   26 	struct Symbol *symbol;
   27 
   28 	while (ast1->ast_type != AST_compound_statement) {
   29 		if (ast1->ast_type == AST_function_definition) {
   30 			for (symbol = ast1->u2.func.arg; symbol != NULL; symbol = symbol->next) {
   31 				if (!strcmp(name, symbol->name))
   32 					break;
   33 			}
   34 			for (i = 0; symbol != NULL; i++)
   35 				symbol = symbol->next;
   36 			index = emalloc(32);
   37 			if (i != 0)
   38 				snprintf(index, 32, "%d(%%ebp)", 4 * (i + 1));
   39 			else
   40 				snprintf(index, 32, "_%s", name);
   41 			return index;
   42 		}
   43 		ast1 = ast1->parent;
   44 	}
   45 	for (symbol = ast1->u2.local; symbol != NULL; symbol = symbol->next)
   46 		if (!strcmp(name, symbol->name))
   47 			break;
   48 	for (i = 0; symbol != NULL; i++)
   49 		symbol = symbol->next;
   50 	if (i != 0) {
   51 		index = emalloc(32);
   52 		snprintf(index, 32, "%d(%%ebp)", -4 * i);
   53 		return index;
   54 	}
   55 	return get_id_index(name, ast->parent);
   56 }
   57 
   58 struct AST *search_AST_bottom(struct AST *root, enum AST_type bottom, int *n) {
   59 	int i = 1;
   60 	struct AST *ast;
   61 
   62 	for (ast = root; ast->ast_type != bottom; ast = ast->u.child[0])
   63 		i++;
   64 	if (n != NULL)
   65 		*n = i;
   66 	return ast;
   67 }
   68 
   69 struct AST *create_AST(enum AST_type ast_type, int argp_len, ...) {
   70 	va_list argp;
   71 	int i;
   72 	struct AST *ast;
   73 	char *id;
   74 
   75 	ast = emalloc(sizeof(struct AST));
   76 	ast->ast_type = ast_type;
   77 	va_start(argp, argp_len);
   78 	switch(ast_type) {
   79 	default:
   80 		ast->num_child = argp_len;
   81 		ast->u.child = emalloc(sizeof(struct AST*) * argp_len);
   82 		for (i = 0; i < argp_len; i++) {
   83 			struct AST *child = va_arg(argp, struct AST*);
   84 			ast->u.child[i] = child;
   85 			if (child != NULL) {
   86 				child->parent = ast;
   87 				child->nth = i;
   88 			}
   89 		}
   90 		break;
   91 	case AST_IDENTIFIER:
   92 	case AST_expression_string:
   93 		ast->num_child = 0;
   94 		id = va_arg(argp, char*);
   95 		ast->u.id = strdup(id);
   96 		break;
   97 	case AST_expression_int:
   98 	case AST_expression_char:
   99 		ast->num_child = 0;
  100 		ast->u.value = va_arg(argp, int);
  101 		break;
  102 	}
  103 	va_end(argp);
  104 	return ast;
  105 }