<commit_before>#include "codegen.h"
#include "asm.h"
#include "expr.h"
#include "exprtype.h"
#include "util.h"
#include "parse.h"

int codegen_entry(ast_vector &program) {
	uint32_t main_address;
	std::string module = "";
	ntv.gencode(0xe9); main_address = ntv.count; ntv.gencode_int32(0);
	Module list(module);
	for(ast_vector::iterator it = program.begin(); it != program.end(); ++it) {
		if((*it)->get_type() == AST_FUNCTION) {
			Function f = ((FunctionAST *)*it)->codegen(list);
		} else if((*it)->get_type() == AST_VARIABLE_ASGMT) { // global variable assignment
		}
	}
	list.insert_global_var();
	Function *main = list.get("main");
		if(main == NULL) error("error: not found function: 'main'");
	ntv.gencode_int32_insert(main->info.address - ADDR_SIZE - 1, main_address);
	
	return 0;
}

bool const_folding(AST *e, int *res) {
	BinaryAST *expr = (BinaryAST *)e;
	int res1 = 0, res2 = 0;
	int a, b;
	if(expr->left->get_type() == AST_BINARY) {
		if(!const_folding(expr->left, &res1))
			return false;
		a = res1;
	} else if(expr->left->get_type() == AST_NUMBER) 
		a = ((NumberAST *)expr->left)->number;
	else return false;

	if(expr->right->get_type() == AST_BINARY) {
		if(!const_folding(expr->right, &res2))
			return false;
		b = res2;
	} else if(expr->right->get_type() == AST_NUMBER) 
		b = ((NumberAST *)expr->right)->number;
	else return false;

	if(expr->op == "+") *res = a + b;
	else if(expr->op == "-") *res = a - b;
	else if(expr->op == "*") *res = a * b;
	else if(expr->op == "/") *res = a / b;
	else if(expr->op == "%") *res = a % b;
	else return false;

	return true;
}

int codegen_expression(Function &f, Module &f_list, AST *ast) {
	switch(ast->get_type()) {
	case AST_NUMBER:
		((NumberAST *)ast)->codegen(f, ntv);
		return T_INT;
	case AST_STRING:
		((StringAST *)ast)->codegen(f, ntv);
		return T_STRING;
	case AST_VARIABLE:
		((VariableAST *)ast)->codegen(f, f_list, ntv);
		return ((VariableAST *)ast)->get(f, f_list)->type;
	case AST_VARIABLE_ASGMT:
		((VariableAsgmtAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_FUNCTION_CALL:
		((FunctionCallAST *)ast)->codegen(f, f_list, ntv);
		return T_INT;
	case AST_BINARY: {
		int res = 0;
		if(const_folding(ast, &res))
			return res;
		else
			return ((BinaryAST *)ast)->codegen(f, f_list, ntv);
	}
	case AST_ARRAY:
		return ((ArrayAST *)ast)->codegen(f, f_list, ntv);
	case AST_VARIABLE_INDEX:
		return ((VariableIndexAST *)ast)->codegen(f, f_list, ntv);
	case AST_IF:
		((IfAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_WHILE:
		((WhileAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_FOR:
		((ForAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_BREAK:
		((BreakAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_RETURN:
		((ReturnAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	}
	return T_VOID;
}
<commit_msg>bug fix about const folding<commit_after>#include "codegen.h"
#include "asm.h"
#include "expr.h"
#include "exprtype.h"
#include "util.h"
#include "parse.h"

int codegen_entry(ast_vector &program) {
	uint32_t main_address;
	std::string module = "";
	ntv.gencode(0xe9); main_address = ntv.count; ntv.gencode_int32(0);
	Module list(module);
	for(ast_vector::iterator it = program.begin(); it != program.end(); ++it) {
		if((*it)->get_type() == AST_FUNCTION) {
			Function f = ((FunctionAST *)*it)->codegen(list);
		} else if((*it)->get_type() == AST_VARIABLE_ASGMT) { // global variable assignment
		}
	}
	list.insert_global_var();
	Function *main = list.get("main");
		if(main == NULL) error("error: not found function: 'main'");
	ntv.gencode_int32_insert(main->info.address - ADDR_SIZE - 1, main_address);
	
	return 0;
}

bool const_folding(AST *e, int *res) {
	BinaryAST *expr = (BinaryAST *)e;
	int tmp = 0;
	int a, b;

	if(expr->left->get_type() == AST_BINARY) {
		if(!const_folding(expr->left, &tmp))
			return false;
		a = tmp; is_left_bin = true;
	} else if(expr->left->get_type() == AST_NUMBER) 
		a = ((NumberAST *)expr->left)->number;
	else return false;

	if(expr->right->get_type() == AST_BINARY) {
		if(!const_folding(expr->right, &tmp))
			return false;
		b = tmp; is_right_bin = true;
	} else if(expr->right->get_type() == AST_NUMBER) 
		b = ((NumberAST *)expr->right)->number;
	else return false;

	if(expr->op == "+") *res = a + b;
	else if(expr->op == "-") *res = a - b;
	else if(expr->op == "*") *res = a * b;
	else if(expr->op == "/") *res = a / b;
	else if(expr->op == "%") *res = a % b;
	else return false;

	return true;
}

int codegen_expression(Function &f, Module &f_list, AST *ast) {
	switch(ast->get_type()) {
	case AST_NUMBER:
		((NumberAST *)ast)->codegen(f, ntv);
		return T_INT;
	case AST_STRING:
		((StringAST *)ast)->codegen(f, ntv);
		return T_STRING;
	case AST_VARIABLE:
		((VariableAST *)ast)->codegen(f, f_list, ntv);
		return ((VariableAST *)ast)->get(f, f_list)->type;
	case AST_VARIABLE_ASGMT:
		((VariableAsgmtAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_FUNCTION_CALL:
		((FunctionCallAST *)ast)->codegen(f, f_list, ntv);
		return T_INT;
	case AST_BINARY: {
		int res = 0;
		if(const_folding(ast, &res)) {
			ntv.genas("mov eax %d", res);
			return T_INT;
		} else
			return ((BinaryAST *)ast)->codegen(f, f_list, ntv);
	}
	case AST_ARRAY:
		return ((ArrayAST *)ast)->codegen(f, f_list, ntv);
	case AST_VARIABLE_INDEX:
		return ((VariableIndexAST *)ast)->codegen(f, f_list, ntv);
	case AST_IF:
		((IfAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_WHILE:
		((WhileAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_FOR:
		((ForAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_BREAK:
		((BreakAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	case AST_RETURN:
		((ReturnAST *)ast)->codegen(f, f_list, ntv);
		return T_VOID;
	}
	return T_VOID;
}

{'fraction_non_alphanumeric': 0.18880878819086852, 'fraction_numerical': 0.005664263645726055, 'mean_word_length': 4.4611059044048735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6361404', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2360, 'n_words': 528}