type.c


    1 #include <stdio.h>
    2 #include <assert.h>
    3 #include <string.h>
    4 #include "xcc.h"
    5 #include "AST.h"
    6 #include "type.h"
    7 
    8 static struct Type *create_function_type(struct Type *ret_type);
    9 static void set_arg_types(struct AST *ast, struct Type *type);
   10 static void type_dump_sub(struct Type *type, int depth);
   11 
   12 struct Types types;
   13 
   14 static struct Type *create_function_type(struct Type *ret_type) {
   15 	struct Type *type = emalloc(sizeof(struct Type));
   16 
   17 	type->kind = TYPE_KIND_FUNCTION;
   18 	type->size = -1;
   19 	type->id = NULL;
   20 	type->u.t_function.ret_type = ret_type;
   21 	type->u.t_function.arg_num = 0;
   22 	type->u.t_function.arg_name = NULL;
   23 	type->u.t_function.arg_type = NULL;
   24 	return type;
   25 }
   26 
   27 static void set_arg_types(struct AST *ast_plist, struct Type *type) {
   28 	struct AST *ast, *ast_par_decn;
   29 	int i, arg_num;
   30 
   31 	assert((ast_plist->ast_type == AST_parameter_list_single)
   32 			|| (ast_plist->ast_type == AST_parameter_list_pair));
   33 	assert(type->kind == TYPE_KIND_FUNCTION);
   34 	ast = search_AST_bottom(ast_plist, AST_parameter_list_single, &arg_num);
   35 	type->u.t_function.arg_name = emalloc(sizeof(char *) * arg_num);
   36 	type->u.t_function.arg_type = emalloc(sizeof(struct Type *) * arg_num);
   37 	type->u.t_function.arg_num = arg_num;
   38 	for (i = 0; ; i++) {
   39 		int j;
   40 		switch (ast->ast_type) {
   41 		case AST_parameter_list_single:
   42 			ast_par_decn = ast->u.child[0];
   43 			break;
   44 		case AST_parameter_list_pair:
   45 			ast_par_decn = ast->u.child[1];
   46 			break;
   47 		default:
   48 			assert(0);
   49 			break;
   50 		}
   51 		type->u.t_function.arg_name[i] = ast_par_decn->type->id;
   52 		type->u.t_function.arg_type[i] = ast_par_decn->type;
   53 		for (j = 0; j < i; j++) {
   54 			char **arg_name = type->u.t_function.arg_name;
   55 			if (!strcmp(arg_name[i], arg_name[j])) {
   56 				fprintf(stderr, "redefinition of %s\n", arg_name[i]);
   57 				yyerror("");
   58 			}
   59 		}
   60 		if (ast == ast_plist)
   61 			break;
   62 		ast = ast->parent;
   63 	}
   64 }
   65 
   66 static void type_dump_sub(struct Type *type, int depth) {
   67 	int i;
   68 	char *kind;
   69 
   70 	switch (type->kind) {
   71 	case TYPE_KIND_PRIM:
   72 		kind = "PRIMITIVE";
   73 		break;
   74 	case TYPE_KIND_POINTER:
   75 		kind = "POINTER  ";
   76 		break;
   77 	case TYPE_KIND_FUNCTION:
   78 		kind = "FUNCTION ";
   79 		break;
   80 	default:
   81 		assert(0);
   82 		break;
   83 	}
   84 	printf("%*s%s: %d %s: ", depth * 2, "", kind,
   85 			type->size, (type->id == NULL ? "" : type->id));
   86 	switch (type->kind) {
   87 	case TYPE_KIND_PRIM:
   88 		switch (type->u.t_prim.ptype) {
   89 		case PRIM_TYPE_VOID:
   90 			printf("void\n");
   91 			break;
   92 		case PRIM_TYPE_INT:
   93 			printf("int\n");
   94 			break;
   95 		case PRIM_TYPE_CHAR:
   96 			printf("char\n");
   97 			break;
   98 		default:
   99 			assert(0);
  100 			break;
  101 		}
  102 		break;
  103 	case TYPE_KIND_POINTER:
  104 		printf("\n");
  105 		type_dump_sub(type->u.t_pointer.type, depth + 1);
  106 		break;
  107 	case TYPE_KIND_FUNCTION:
  108 		printf("\n%*s=>return\n", depth * 2, "");
  109 		type_dump_sub(type->u.t_function.ret_type, depth + 1);
  110 		for (i = 0; i < type->u.t_function.arg_num; i++) {
  111 			printf("%*s=>arg[%d]: %s\n", depth, "",
  112 					i * 2, type->u.t_function.arg_name[i]);
  113 			type_dump_sub(type->u.t_function.arg_type[i], depth + 1);
  114 		}
  115 		break;
  116 	default:
  117 		assert(0);
  118 		break;
  119 	}
  120 }
  121 
  122 struct Type *create_prim_type(enum PrimType ptype) {
  123 	struct Type *type = emalloc(sizeof(struct Type));
  124 
  125 	type->kind = TYPE_KIND_PRIM;
  126 	type->id = NULL;
  127 	type->u.t_prim.ptype = ptype;
  128 	switch (ptype) {
  129 	case PRIM_TYPE_VOID:
  130 		type->size = -1;
  131 		break;
  132 	case PRIM_TYPE_INT:
  133 		type->size = 4;
  134 		break;
  135 	case PRIM_TYPE_CHAR:
  136 		type->size = 1;
  137 		break;
  138 	default:
  139 		assert(0);
  140 		break;
  141 	}
  142 	return type;
  143 }
  144 
  145 struct Type *create_pointer_type(struct Type *pointedto_type) {
  146 	struct Type *type = emalloc(sizeof(struct Type));
  147 
  148 	type->kind = TYPE_KIND_POINTER;
  149 	type->size = 4;
  150 	type->id = NULL;
  151 	type->u.t_pointer.type = pointedto_type;
  152 	return type;
  153 }
  154 
  155 struct Type *type_analyze_declarator(struct AST *ast_decr, struct Type *type) {
  156 	struct Type *type1, *type2;
  157 
  158 	switch (ast_decr->ast_type) {
  159 	case AST_declarator_id:
  160 		type->id = ast_decr->u.child[0]->u.id;
  161 		return type;
  162 	case AST_declarator_pointer:
  163 		type1 = create_pointer_type(type);
  164 		type2 = type_analyze_declarator(ast_decr->u.child[0], type1);
  165 		type->id = type2->id;
  166 		return type2;
  167 	case AST_declarator_paren:
  168 		type1 = type_analyze_declarator(ast_decr->u.child[0], type);
  169 		return type1;
  170 	case AST_declarator_func:
  171 		type1 = create_function_type(type);
  172 		type2 = type_analyze_declarator(ast_decr->u.child[0], type1);
  173 		type->id = type2->id;
  174 		set_arg_types(ast_decr->u.child[1], type1);
  175 		return type2;
  176 	case AST_declarator_func_null:
  177 		type1 = create_function_type(type);
  178 		type2 = type_analyze_declarator(ast_decr->u.child[0], type1);
  179 		type->id = type2->id;
  180 		return type2;
  181 	default:
  182 		assert(0);
  183 		break;
  184 	}
  185 	return NULL;
  186 }
  187 
  188 struct Type *type_add(struct Type *type1, struct Type *type2) {
  189 	if ((type1->kind == TYPE_KIND_POINTER) && (type2->kind == TYPE_KIND_POINTER))
  190 		yyerror("pointer + pointer prohibited\n");
  191 	if (type1->kind == TYPE_KIND_POINTER)
  192 		return type1;
  193 	if (type2->kind == TYPE_KIND_POINTER)
  194 		return type2;
  195 	return types.t_int;
  196 }
  197 
  198 struct Type *type_sub(struct Type *type1, struct Type *type2) {
  199 	if ((type1->kind == TYPE_KIND_POINTER) && (type2->kind == TYPE_KIND_POINTER)) {
  200 		if (type1->size != type2->size)
  201 			yyerror("operand sizes are different\n");
  202 		return types.t_int;
  203 	}
  204 	if (type1->kind == TYPE_KIND_POINTER)
  205 		return type1;
  206 	if (type2->kind == TYPE_KIND_POINTER)
  207 		yyerror("scalar - pointernprohibited\n");
  208 	return types.t_int;
  209 }
  210 
  211 struct Type *type_check_pointer_deref(struct AST *ast, struct Type *type) {
  212 	assert(ast->ast_type == AST_expression_unary_deref);
  213 	if (type->kind == TYPE_KIND_POINTER)
  214 		return type->u.t_pointer.type;
  215 	else
  216 		yyerror("no pointer type dereferenced\n");
  217 	return NULL;
  218 }
  219 
  220 void type_dump(struct Type *type) {
  221 	type_dump_sub(type, 0);
  222 }