int scf_elf_open(scf_elf_context_t** pelf, const char* machine, const char* path, const char* mode)
{
- scf_elf_context_t* elf = calloc(1, sizeof(scf_elf_context_t));
- assert(elf);
-
+ scf_elf_context_t* elf;
int i;
+
+ elf = calloc(1, sizeof(scf_elf_context_t));
+ if (!elf)
+ return -ENOMEM;
+
for (i = 0; elf_ops_array[i]; i++) {
if (!strcmp(elf_ops_array[i]->machine, machine)) {
elf->ops = elf_ops_array[i];
if (!elf->ops) {
scf_loge("\n");
+ free(elf);
return -1;
}
elf->fp = fopen(path, mode);
if (!elf->fp) {
scf_loge("\n");
+ free(elf);
return -1;
}
scf_loge("\n");
+ fclose(elf->fp);
free(elf);
elf = NULL;
return -1;
#include"scf_elf_link.h"
+void scf_ar_sym_free(scf_ar_sym_t* sym)
+{
+ if (sym) {
+ if (sym->name)
+ scf_string_free(sym->name);
+ free(sym);
+ }
+}
+
int __scf_elf_file_open(scf_elf_file_t** pfile)
{
scf_elf_file_t* ef = calloc(1, sizeof(scf_elf_file_t));
return -ENOMEM;
ret = fread(buf, ar_size, 1, ar->fp);
- if (ret != 1)
- return -1;
+ if (ret != 1) {
+ ret = -1;
+ goto error;
+ }
for (i = 0; i < sizeof(nb_syms); i++) {
nb_syms <<= 8;
}
sym = calloc(1, sizeof(scf_ar_sym_t));
- if (!sym)
- return -ENOMEM;
+ if (!sym) {
+ ret = -ENOMEM;
+ goto error;
+ }
if (scf_vector_add(ar->symbols, sym) < 0) {
free(sym);
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto error;
}
sym->offset = offset;
k++;
sym->name = scf_string_cstr_len(buf + j, k);
- if (!sym->name)
- return -ENOMEM;
+ if (!sym->name) {
+ ret = -ENOMEM;
+ goto error;
+ }
j += k + 1;
}
assert(j == ar_size);
return 0;
+
+error:
+ scf_vector_clear(ar->symbols, ( void (*)(void*) )scf_ar_sym_free);
+ free(buf);
+ return ret;
}
int scf_ar_file_open(scf_ar_file_t** par, const char* path)
ar->fp = fopen(path, "rb");
if (!ar->fp) {
ret = -1;
- goto error;
+ goto open_error;
}
ret = ar_symbols(ar);
return 0;
error:
- free(ar->files);
+ fclose(ar->fp);
+open_error:
+ scf_vector_free(ar->files);
file_error:
- free(ar->symbols);
+ scf_vector_free(ar->symbols);
sym_error:
free(ar);
return ret;
{
elf_section_t* s;
elf_sym_t* sym;
- Elf64_Rela* rela;
+ Elf64_Rela* rela;
int i;
for (i = x64->symbols->size - 1; i >= 0; i--) {
} scf_x64_context_t;
typedef struct {
- scf_dag_node_t* dag_node;
+ scf_dag_node_t* dag_node;
- scf_register_t* reg;
+ scf_register_t* reg;
- scf_x64_OpCode_t* OpCode;
+ scf_x64_OpCode_t* OpCode;
} x64_rcg_node_t;
int i;
int j;
+ int k;
for (i = tmp_insts->size - 1; i >= 0; i--) {
inst = tmp_insts->data[i];
scf_register_t* r0;
scf_register_t* r1;
+ scf_register_t* r2;
if (SCF_X64_MOV != inst->OpCode->type)
continue;
r0 = inst->dst.base;
- if (X64_COLOR_CONFLICT(rax->color, r0->color))
+ for (k = 0; k < X64_ABI_RET_NB; k++) {
+ r2 = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[k], 8);
+
+ if (X64_COLOR_CONFLICT(r2->color, r0->color))
+ break;
+ }
+
+ if (k < X64_ABI_RET_NB)
continue;
} else if (!x64_inst_data_is_local(&inst->dst))
}
}
- scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
goto _error;
}
}
- scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
goto _error;
}
}
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
}
}
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
}
}
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
if (!h)
return -1;
{{NULL, NULL}, SCF_OP_FOR, _scf_op_const_for},
};
-scf_operator_handler_t* scf_find_const_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type)
+scf_operator_handler_t* scf_find_const_operator_handler(const int type)
{
int i;
for (i = 0; i < sizeof(const_operator_handlers) / sizeof(const_operator_handlers[0]); i++) {
#include"scf_operator_handler.h"
-scf_operator_handler_t* scf_find_const_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type);
+scf_operator_handler_t* scf_find_const_operator_handler(const int type);
int scf_function_const_opt(scf_ast_t* ast, scf_function_t* f);
}
}
- h = scf_find_semantic_operator_handler(node->op->type, -1, -1, -1);
+ h = scf_find_semantic_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
goto _error;
}
}
- h = scf_find_semantic_operator_handler(node->op->type, -1, -1, -1);
+ h = scf_find_semantic_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
goto _error;
}
}
- scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type);
if (!h) {
scf_loge("op->type: %d, va: %d, %d, %d\n", op->type, SCF_OP_VA_START, SCF_OP_VA_ARG, SCF_OP_VA_END);
return -1;
}
}
- scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
}
}
- scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
scf_variable_t** pret = d->pret;
- scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
}
}
- scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1);
+ scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type);
if (!h) {
scf_loge("\n");
return -1;
{{NULL, NULL}, SCF_OP_FOR, _scf_op_semantic_for},
};
-scf_operator_handler_t* scf_find_semantic_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type)
+scf_operator_handler_t* scf_find_semantic_operator_handler(const int type)
{
int i;
for (i = 0; i < sizeof(semantic_operator_handlers) / sizeof(semantic_operator_handlers[0]); i++) {
#include"scf_operator_handler.h"
-scf_operator_handler_t* scf_find_semantic_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type);
+scf_operator_handler_t* scf_find_semantic_operator_handler(const int type);
int scf_function_semantic_analysis(scf_ast_t* ast, scf_function_t* f);