2022-11-08 09:32:46 +01:00
|
|
|
/*
|
|
|
|
* IR - Lightweight JIT Compilation Framework
|
|
|
|
* (IR CLI driver)
|
|
|
|
* Copyright (C) 2022 Zend by Perforce.
|
|
|
|
* Authors: Dmitry Stogov <dmitry@php.net>
|
|
|
|
*/
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
#include "ir.h"
|
2023-10-20 00:09:46 +02:00
|
|
|
#include "ir_private.h" // TODO: move this together with loader
|
2022-04-27 13:47:52 +02:00
|
|
|
#include <stdlib.h>
|
2022-05-27 12:18:04 +02:00
|
|
|
#include <string.h>
|
2022-04-05 23:19:23 +02:00
|
|
|
|
2023-03-07 08:54:46 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
static void help(const char *cmd)
|
|
|
|
{
|
|
|
|
printf(
|
2023-10-11 11:46:31 +02:00
|
|
|
#if HAVE_LLVM
|
|
|
|
"Usage: %s [options] [--llvm-bitcode|--llvm-asm] input-file...\n"
|
|
|
|
#else
|
2022-04-05 23:19:23 +02:00
|
|
|
"Usage: %s [options] input-file...\n"
|
2023-10-11 11:46:31 +02:00
|
|
|
#endif
|
2022-04-05 23:19:23 +02:00
|
|
|
"Options:\n"
|
2023-10-03 07:34:02 +02:00
|
|
|
" -O[012] - optimization level (default: 2)\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" -S - dump final target assembler code\n"
|
2023-04-18 08:54:35 +02:00
|
|
|
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
|
2022-04-05 23:19:23 +02:00
|
|
|
" -mavx - use AVX instruction set\n"
|
2023-04-18 08:54:35 +02:00
|
|
|
#endif
|
2022-05-17 21:37:13 +02:00
|
|
|
" -muse-fp - use base frame pointer register\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --emit-c [file-name] - convert to C source\n"
|
2023-09-28 19:44:45 +02:00
|
|
|
" --emit-llvm [file-name] - convert to LLVM\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --save [file-name] - save IR\n"
|
|
|
|
" --dot [file-name] - dump IR graph\n"
|
|
|
|
" --dump [file-name] - dump IR table\n"
|
2023-10-03 07:34:02 +02:00
|
|
|
" --dump-after-load - dump IR after load and local optimization\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --dump-after-sccp - dump IR after SCCP optimization pass\n"
|
|
|
|
" --dump-after-gcm - dump IR after GCM optimization pass\n"
|
|
|
|
" --dump-after-schedule - dump IR after SCHEDULE pass\n"
|
|
|
|
" --dump-after-live-ranges - dump IR after live ranges identification\n"
|
2022-08-10 08:31:19 +02:00
|
|
|
" --dump-after-coalescing - dump IR after live ranges coalescing\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --dump-after-all - dump IR after each pass\n"
|
2023-02-21 20:22:41 +01:00
|
|
|
" --dump-final - dump IR after all pass\n"
|
|
|
|
" --dump-size - dump generated code size\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --dump-use-lists - dump def->use lists\n"
|
|
|
|
" --dump-cfg - dump CFG (Control Flow Graph)\n"
|
2022-05-25 08:33:47 +02:00
|
|
|
" --dump-cfg-map - dump CFG map (instruction to BB number)\n"
|
2022-08-10 08:31:19 +02:00
|
|
|
" --dump-live-ranges - dump live ranges\n"
|
2022-04-27 13:47:52 +02:00
|
|
|
#ifdef IR_DEBUG
|
|
|
|
" --debug-sccp - debug SCCP optimization pass\n"
|
|
|
|
" --debug-gcm - debug GCM optimization pass\n"
|
2022-05-24 17:04:38 +02:00
|
|
|
" --debug-schedule - debug SCHEDULE optimization pass\n"
|
2022-04-27 13:47:52 +02:00
|
|
|
" --debug-ra - debug register allocator\n"
|
|
|
|
" --debug-regset <bit-mask> - restrict available register set\n"
|
|
|
|
#endif
|
2022-05-25 16:38:22 +02:00
|
|
|
" --target - print JIT target\n"
|
2022-04-05 23:19:23 +02:00
|
|
|
" --version\n"
|
|
|
|
" --help\n",
|
|
|
|
cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IR_DUMP_SAVE (1<<0)
|
|
|
|
#define IR_DUMP_DUMP (1<<1)
|
|
|
|
#define IR_DUMP_DOT (1<<2)
|
|
|
|
#define IR_DUMP_USE_LISTS (1<<3)
|
|
|
|
#define IR_DUMP_CFG (1<<4)
|
2022-05-25 08:33:47 +02:00
|
|
|
#define IR_DUMP_CFG_MAP (1<<5)
|
2022-04-05 23:19:23 +02:00
|
|
|
#define IR_DUMP_LIVE_RANGES (1<<6)
|
2023-06-21 21:36:36 +02:00
|
|
|
#define IR_DUMP_CODEGEN (1<<7)
|
2022-04-05 23:19:23 +02:00
|
|
|
|
|
|
|
#define IR_DUMP_AFTER_LOAD (1<<16)
|
|
|
|
#define IR_DUMP_AFTER_SCCP (1<<17)
|
|
|
|
#define IR_DUMP_AFTER_GCM (1<<18)
|
|
|
|
#define IR_DUMP_AFTER_SCHEDULE (1<<19)
|
|
|
|
#define IR_DUMP_AFTER_LIVE_RANGES (1<<20)
|
|
|
|
#define IR_DUMP_AFTER_COALESCING (1<<21)
|
|
|
|
|
|
|
|
#define IR_DUMP_AFTER_ALL (1<<29)
|
|
|
|
#define IR_DUMP_FINAL (1<<30)
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
static int _save(ir_ctx *ctx, uint32_t dump, uint32_t pass, FILE *f, const char *func_name)
|
2022-04-05 23:19:23 +02:00
|
|
|
{
|
|
|
|
char fn[4096];
|
2023-10-11 21:55:25 +02:00
|
|
|
bool close = 0;
|
2022-04-05 23:19:23 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
if (!f) {
|
2022-04-05 23:19:23 +02:00
|
|
|
if (dump & IR_DUMP_AFTER_ALL) {
|
|
|
|
if (pass == IR_DUMP_AFTER_LOAD) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "01-load-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_AFTER_SCCP) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "02-sccp-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_AFTER_GCM) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "03-gcm-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_AFTER_SCHEDULE) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "04-schedule-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_AFTER_LIVE_RANGES) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "05-live-ranges-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_AFTER_COALESCING) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "06-coalescing-%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (pass == IR_DUMP_FINAL) {
|
2023-06-21 21:36:36 +02:00
|
|
|
if (dump & IR_DUMP_CODEGEN) {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "07-codegen-%s.ir", func_name);
|
2023-06-21 21:36:36 +02:00
|
|
|
} else {
|
2023-10-11 21:55:25 +02:00
|
|
|
snprintf(fn, sizeof(fn)-1, "07-final-%s.ir", func_name);
|
2023-06-21 21:36:36 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
} else {
|
|
|
|
f = stderr; // TODO:
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
} else {
|
|
|
|
snprintf(fn, sizeof(fn)-1, "%s.ir", func_name);
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
f = fopen(fn, "w+");
|
2022-04-05 23:19:23 +02:00
|
|
|
if (!f) {
|
2023-10-11 21:55:25 +02:00
|
|
|
fprintf(stderr, "ERROR: Cannot create file '%s'\n", fn);
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
close = 1;
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-06-21 21:36:36 +02:00
|
|
|
if (pass == IR_DUMP_FINAL && (dump & IR_DUMP_CODEGEN)) {
|
|
|
|
ir_dump_codegen(ctx, f);
|
|
|
|
} else if (dump & IR_DUMP_SAVE) {
|
2022-04-05 23:19:23 +02:00
|
|
|
ir_save(ctx, f);
|
|
|
|
}
|
|
|
|
if (dump & IR_DUMP_DUMP) {
|
|
|
|
ir_dump(ctx, f);
|
|
|
|
}
|
|
|
|
if (dump & IR_DUMP_DOT) {
|
|
|
|
ir_dump_dot(ctx, f);
|
|
|
|
}
|
|
|
|
if (dump & IR_DUMP_USE_LISTS) {
|
|
|
|
ir_dump_use_lists(ctx, f);
|
|
|
|
}
|
|
|
|
if (dump & IR_DUMP_CFG) {
|
|
|
|
ir_dump_cfg(ctx, f);
|
|
|
|
}
|
2022-05-25 08:33:47 +02:00
|
|
|
if (dump & IR_DUMP_CFG_MAP) {
|
|
|
|
ir_dump_cfg_map(ctx, f);
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
|
|
|
if (dump & IR_DUMP_LIVE_RANGES) {
|
|
|
|
ir_dump_live_ranges(ctx, f);
|
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
if (close) {
|
2022-04-05 23:19:23 +02:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
int ir_compile_func(ir_ctx *ctx, int opt_level, uint32_t dump, FILE *dump_file, const char *func_name)
|
2022-04-05 23:19:23 +02:00
|
|
|
{
|
|
|
|
if ((dump & (IR_DUMP_AFTER_LOAD|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_LOAD, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-28 19:44:45 +02:00
|
|
|
if (opt_level > 0 || (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE))) {
|
2023-03-22 21:58:08 +01:00
|
|
|
ir_build_def_use_lists(ctx);
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:57:55 +02:00
|
|
|
ir_check(ctx);
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
/* Global Optimization */
|
|
|
|
if (opt_level > 1) {
|
|
|
|
ir_sccp(ctx);
|
|
|
|
if ((dump & (IR_DUMP_AFTER_SCCP|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_SCCP, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 19:44:45 +02:00
|
|
|
if (opt_level > 0 || (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE))) {
|
2023-03-22 21:58:08 +01:00
|
|
|
ir_build_cfg(ctx);
|
|
|
|
}
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
/* Schedule */
|
|
|
|
if (opt_level > 0) {
|
|
|
|
ir_build_dominators_tree(ctx);
|
|
|
|
ir_find_loops(ctx);
|
|
|
|
ir_gcm(ctx);
|
|
|
|
if ((dump & (IR_DUMP_AFTER_GCM|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_GCM, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ir_schedule(ctx);
|
|
|
|
if ((dump & (IR_DUMP_AFTER_SCHEDULE|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_SCHEDULE, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->flags & IR_GEN_NATIVE) {
|
|
|
|
ir_match(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_level > 0) {
|
|
|
|
ir_assign_virtual_registers(ctx);
|
|
|
|
ir_compute_live_ranges(ctx);
|
|
|
|
|
|
|
|
if ((dump & (IR_DUMP_AFTER_LIVE_RANGES|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_LIVE_RANGES, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ir_coalesce(ctx);
|
|
|
|
|
|
|
|
if ((dump & (IR_DUMP_AFTER_COALESCING|IR_DUMP_AFTER_ALL))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_AFTER_COALESCING, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:35:29 +02:00
|
|
|
if (ctx->flags & IR_GEN_NATIVE) {
|
|
|
|
ir_reg_alloc(ctx);
|
|
|
|
}
|
2022-05-23 23:43:35 +02:00
|
|
|
|
|
|
|
ir_schedule_blocks(ctx);
|
2023-09-28 19:44:45 +02:00
|
|
|
} else if (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE)) {
|
2022-04-19 21:35:29 +02:00
|
|
|
ir_assign_virtual_registers(ctx);
|
|
|
|
ir_compute_dessa_moves(ctx);
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 21:36:36 +02:00
|
|
|
if ((dump & (IR_DUMP_FINAL|IR_DUMP_AFTER_ALL|IR_DUMP_CODEGEN))
|
2023-10-11 21:55:25 +02:00
|
|
|
&& !_save(ctx, dump, IR_DUMP_FINAL, dump_file, func_name)) {
|
2022-04-05 23:19:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:57:55 +02:00
|
|
|
ir_check(ctx);
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
typedef struct _ir_sym {
|
|
|
|
void *addr;
|
|
|
|
} ir_sym;
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
typedef struct _ir_main_loader {
|
|
|
|
ir_loader loader;
|
|
|
|
int opt_level;
|
|
|
|
uint32_t mflags;
|
|
|
|
uint64_t debug_regset;
|
|
|
|
uint32_t dump;
|
|
|
|
bool dump_asm;
|
|
|
|
bool dump_size;
|
|
|
|
bool run;
|
|
|
|
size_t size;
|
|
|
|
void *main;
|
|
|
|
FILE *dump_file;
|
|
|
|
FILE *c_file;
|
|
|
|
FILE *llvm_file;
|
2023-10-20 00:09:46 +02:00
|
|
|
ir_strtab symtab;
|
|
|
|
ir_sym *sym;
|
|
|
|
ir_ref sym_count;
|
|
|
|
void *data;
|
2023-10-11 21:55:25 +02:00
|
|
|
} ir_main_loader;
|
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
static bool ir_loader_add_sym(ir_main_loader *l, const char *name, void *addr)
|
|
|
|
{
|
|
|
|
uint32_t len = (uint32_t)strlen(name);
|
|
|
|
ir_ref val = ir_strtab_count(&l->symtab) + 1;
|
|
|
|
ir_ref old_val = ir_strtab_lookup(&l->symtab, name, len, val);
|
|
|
|
if (old_val != val) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (val >= l->sym_count) {
|
|
|
|
l->sym_count += 16;
|
|
|
|
l->sym = ir_mem_realloc(l->sym, sizeof(ir_sym) * l->sym_count);
|
|
|
|
}
|
|
|
|
l->sym[val].addr = addr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* ir_loader_resolve_sym_name(ir_loader *loader, const char *name)
|
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*)loader;
|
|
|
|
uint32_t len = (uint32_t)strlen(name);
|
|
|
|
ir_ref val = ir_strtab_find(&l->symtab, name, len);
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
return l->sym[val].addr;
|
|
|
|
}
|
|
|
|
addr = ir_resolve_sym_name(name);
|
|
|
|
ir_loader_add_sym(l, name, addr); /* cache */
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static bool ir_loader_external_sym_dcl(ir_loader *loader, const char *name, uint32_t flags)
|
2023-10-11 21:55:25 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "extern %s %s;\n", (flags & IR_CONST) ? "const" : "var", name);
|
2023-10-11 21:55:25 +02:00
|
|
|
}
|
2023-10-20 00:09:46 +02:00
|
|
|
if (l->dump_asm || l->dump_size || l->run) {
|
|
|
|
void *addr = ir_loader_resolve_sym_name(loader, name);
|
|
|
|
|
|
|
|
if (!addr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (l->dump_asm) {
|
|
|
|
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static void ir_dump_func_dcl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types, FILE *f)
|
|
|
|
{
|
|
|
|
if (flags & IR_EXTERN) {
|
|
|
|
fprintf(f, "extern ");
|
|
|
|
} else if (flags & IR_STATIC) {
|
|
|
|
fprintf(f, "static ");
|
|
|
|
}
|
|
|
|
fprintf(f, "func %s(", name);
|
|
|
|
if (params_count) {
|
|
|
|
ir_type *p = param_types;
|
|
|
|
|
|
|
|
fprintf(f, "%s", ir_type_cname[*p]);
|
|
|
|
p++;
|
|
|
|
while (--params_count) {
|
|
|
|
fprintf(f, ", %s", ir_type_cname[*p]);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (flags & IR_VARARG_FUNC) {
|
|
|
|
fprintf(f, ", ...");
|
|
|
|
}
|
|
|
|
} else if (flags & IR_VARARG_FUNC) {
|
|
|
|
fprintf(f, "...");
|
|
|
|
} else {
|
|
|
|
fprintf(f, "void");
|
|
|
|
}
|
|
|
|
fprintf(f, "): %s;\n", ir_type_cname[ret_type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ir_loader_external_func_dcl(ir_loader *loader, const char *name,
|
|
|
|
uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types)
|
2023-10-11 21:55:25 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
2023-10-20 16:44:45 +02:00
|
|
|
ir_dump_func_dcl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->dump_file);
|
|
|
|
}
|
|
|
|
if (l->c_file) {
|
|
|
|
ir_emit_c_func_decl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->c_file);
|
|
|
|
}
|
|
|
|
if (l->llvm_file) {
|
|
|
|
ir_emit_llvm_func_decl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->llvm_file);
|
|
|
|
}
|
|
|
|
if (l->dump_asm || l->dump_size || l->run) {
|
|
|
|
void *addr = ir_loader_resolve_sym_name(loader, name);
|
|
|
|
|
|
|
|
if (!addr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (l->dump_asm) {
|
|
|
|
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ir_loader_forward_func_dcl(ir_loader *loader, const char *name,
|
|
|
|
uint32_t flags, ir_type ret_type, uint32_t params_count, ir_type *param_types)
|
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
|
|
|
ir_dump_func_dcl(name, flags, ret_type, params_count, param_types, l->dump_file);
|
|
|
|
}
|
|
|
|
if (l->c_file) {
|
|
|
|
ir_emit_c_func_decl(name, flags, ret_type, params_count, param_types, l->c_file);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ir_loader_sym_dcl(ir_loader *loader, const char *name, uint32_t flags, size_t size, bool has_data)
|
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
|
|
|
if (flags & IR_STATIC) {
|
|
|
|
fprintf(l->dump_file, "static ");
|
|
|
|
}
|
|
|
|
fprintf(l->dump_file, "%s %s [%ld]%s\n", (flags & IR_CONST) ? "const" : "var", name, size, has_data ? " = {" : ";");
|
2023-10-11 21:55:25 +02:00
|
|
|
}
|
2023-10-20 00:09:46 +02:00
|
|
|
if (l->dump_asm || l->dump_size || l->run) {
|
|
|
|
void *data = ir_mem_malloc(size);
|
|
|
|
|
|
|
|
if (!ir_loader_add_sym((ir_main_loader*)loader, name, data)) {
|
|
|
|
ir_mem_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(data, 0, size);
|
|
|
|
if (has_data) {
|
|
|
|
l->data = data;
|
|
|
|
}
|
|
|
|
if (l->dump_asm) {
|
|
|
|
ir_disasm_add_symbol(name, (uintptr_t)data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static bool ir_loader_sym_data(ir_loader *loader, ir_type type, uint32_t count, const void *data)
|
2023-10-20 00:09:46 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
2023-10-20 16:44:45 +02:00
|
|
|
const void *p = data;
|
|
|
|
uint32_t i;
|
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
switch (ir_type_size[type]) {
|
|
|
|
case 1:
|
|
|
|
for (i = 0; i < count; i++) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "\t%s 0x%02x,\n", ir_type_cname[type], (uint32_t)*(uint8_t*)p);
|
|
|
|
p = (void*)((uintptr_t)p + 1);
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (i = 0; i < count; i++) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "\t%s 0x%04x,\n", ir_type_cname[type], (uint32_t)*(uint16_t*)p);
|
|
|
|
p = (void*)((uintptr_t)p + 1);
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
for (i = 0; i < count; i++) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "\t%s 0x%08x,\n", ir_type_cname[type], *(uint32_t*)p);
|
|
|
|
p = (void*)((uintptr_t)p + 4);
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
for (i = 0; i < count; i++) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "\t%s 0x%016lx,\n", ir_type_cname[type], *(uint64_t*)p);
|
|
|
|
p = (void*)((uintptr_t)p + 8);
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l->dump_asm || l->dump_size || l->run) {
|
2023-10-20 16:44:45 +02:00
|
|
|
size_t size = ir_type_size[type] * count;
|
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
if (!l->data) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// TODO: alignement
|
|
|
|
memcpy(l->data, data, size);
|
|
|
|
l->data = (void*)((uintptr_t)l->data + size);
|
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static bool ir_loader_sym_data_end(ir_loader *loader)
|
2023-10-11 21:55:25 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
|
|
|
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
|
2023-10-20 16:44:45 +02:00
|
|
|
fprintf(l->dump_file, "};\n");
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static bool ir_loader_func_init(ir_loader *loader, ir_ctx *ctx, const char *name)
|
2023-10-11 21:55:25 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
ir_init(ctx, loader->default_func_flags, 256, 1024);
|
2023-10-11 21:55:25 +02:00
|
|
|
ctx->mflags = l->mflags;
|
|
|
|
ctx->fixed_regset = ~l->debug_regset;
|
2023-10-20 00:09:46 +02:00
|
|
|
ctx->loader = loader;
|
2023-10-11 21:55:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
static bool ir_loader_func_process(ir_loader *loader, ir_ctx *ctx, const char *name)
|
2023-10-11 21:55:25 +02:00
|
|
|
{
|
|
|
|
ir_main_loader *l = (ir_main_loader*) loader;
|
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
// TODO: remove this
|
2023-10-20 16:44:45 +02:00
|
|
|
if (ctx->ret_type == (ir_type)-1) {
|
|
|
|
ir_ref ref = ctx->ir_base[1].op1;
|
|
|
|
while (ref) {
|
|
|
|
ir_insn *insn = &ctx->ir_base[ref];
|
|
|
|
if (insn->op == IR_RETURN) {
|
|
|
|
ctx->ret_type = insn->op2 ? ctx->ir_base[insn->op2].type : IR_VOID;
|
|
|
|
break;
|
|
|
|
} else if (insn->op == IR_UNREACHABLE && ctx->ir_base[insn->op1].op == IR_TAILCALL) {
|
|
|
|
ctx->ret_type = ctx->ir_base[insn->op1].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ref = ctx->ir_base[ref].op3;
|
|
|
|
}
|
2023-10-20 00:09:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 16:44:45 +02:00
|
|
|
if (name == NULL) {
|
|
|
|
name = (l->run) ? "main" : "test";
|
|
|
|
} else if ((l->dump & IR_DUMP_SAVE) && l->dump_file) {
|
|
|
|
if (ctx->flags & IR_STATIC) {
|
|
|
|
fprintf(l->dump_file, "static ");
|
|
|
|
}
|
|
|
|
fprintf(l->dump_file, "func %s(", name);
|
|
|
|
if (ctx->ir_base[2].op == IR_PARAM) {
|
|
|
|
ir_insn *insn = &ctx->ir_base[2];
|
|
|
|
|
|
|
|
fprintf(l->dump_file, "%s", ir_type_cname[insn->type]);
|
|
|
|
insn++;
|
|
|
|
while (insn->op == IR_PARAM) {
|
|
|
|
fprintf(l->dump_file, ", %s", ir_type_cname[insn->type]);
|
|
|
|
insn++;;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(l->dump_file, "void");
|
|
|
|
}
|
|
|
|
fprintf(l->dump_file, "): %s\n", ir_type_cname[ctx->ret_type != (ir_type)-1 ? ctx->ret_type : IR_VOID]);
|
2023-10-11 21:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ir_compile_func(ctx, l->opt_level, l->dump, l->dump_file, name)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->c_file) {
|
|
|
|
if (!ir_emit_c(ctx, name, l->c_file)) {
|
|
|
|
fprintf(stderr, "\nERROR: %d\n", ctx->status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->llvm_file) {
|
|
|
|
if (!ir_emit_llvm(ctx, name, l->llvm_file)) {
|
|
|
|
fprintf(stderr, "\nERROR: %d\n", ctx->status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->dump_asm || l->dump_size || l->run) {
|
|
|
|
size_t size;
|
|
|
|
void *entry = ir_emit_code(ctx, &size);
|
|
|
|
|
|
|
|
l->size += size;
|
|
|
|
if (entry) {
|
2023-10-20 00:09:46 +02:00
|
|
|
if (!ir_loader_add_sym(l, name, entry)) {
|
|
|
|
fprintf(stderr, "\nERROR: Symbol redefinition: %s\n", name);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
if (l->dump_asm) {
|
|
|
|
ir_ref i;
|
|
|
|
ir_insn *insn;
|
|
|
|
|
|
|
|
ir_disasm_add_symbol(name, (uintptr_t)entry, size);
|
|
|
|
|
|
|
|
for (i = IR_UNUSED + 1, insn = ctx->ir_base - i; i < ctx->consts_count; i++, insn--) {
|
2023-10-20 00:09:46 +02:00
|
|
|
if (insn->op == IR_FUNC) {
|
2023-10-11 21:55:25 +02:00
|
|
|
const char *name = ir_get_str(ctx, insn->val.i32);
|
2023-10-20 00:09:46 +02:00
|
|
|
void *addr = ir_loader_resolve_sym_name(loader, name);
|
2023-10-11 21:55:25 +02:00
|
|
|
|
|
|
|
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
|
2023-10-20 00:09:46 +02:00
|
|
|
//TODO: } else if (insn->op == IR_SYM) {
|
2023-10-11 21:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ir_disasm(name, entry, size, 0, ctx, stderr);
|
|
|
|
}
|
|
|
|
if (l->run) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
ir_perf_map_register(name, entry, size);
|
|
|
|
ir_perf_jitdump_open();
|
|
|
|
ir_perf_jitdump_register(name, entry, size);
|
|
|
|
|
|
|
|
ir_mem_unprotect(entry, 4096);
|
|
|
|
ir_gdb_register(name, entry, size, sizeof(void*), 0);
|
|
|
|
ir_mem_protect(entry, 4096);
|
|
|
|
#endif
|
|
|
|
if (strcmp(name, "main") == 0) {
|
|
|
|
l->main = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "\nERROR: %d\n", ctx->status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 23:19:23 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *input = NULL;
|
2023-09-28 19:44:45 +02:00
|
|
|
char *dump_file = NULL, *c_file = NULL, *llvm_file = 0;
|
2022-04-05 23:19:23 +02:00
|
|
|
FILE *f;
|
2023-10-11 21:55:25 +02:00
|
|
|
bool emit_c = 0, emit_llvm = 0, dump_size = 0, dump_asm = 0, run = 0;
|
2022-04-05 23:19:23 +02:00
|
|
|
uint32_t dump = 0;
|
|
|
|
int opt_level = 2;
|
2023-04-18 08:54:35 +02:00
|
|
|
uint32_t flags = 0;
|
2022-04-05 23:19:23 +02:00
|
|
|
uint32_t mflags = 0;
|
2022-06-21 15:13:14 +02:00
|
|
|
uint64_t debug_regset = 0xffffffffffffffff;
|
2023-03-01 18:31:14 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
bool abort_fault = 1;
|
2023-10-11 11:46:31 +02:00
|
|
|
#endif
|
|
|
|
#if HAVE_LLVM
|
|
|
|
bool load_llvm_bitcode = 0;
|
|
|
|
bool load_llvm_asm = 0;
|
2023-03-01 18:31:14 +01:00
|
|
|
#endif
|
2023-10-11 21:55:25 +02:00
|
|
|
ir_main_loader loader;
|
|
|
|
|
2022-05-27 12:18:04 +02:00
|
|
|
ir_consistency_check();
|
2022-04-05 23:19:23 +02:00
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-h") == 0
|
|
|
|
|| strcmp(argv[i], "--help") == 0) {
|
|
|
|
help(argv[0]);
|
|
|
|
return 0;
|
|
|
|
} else if (strcmp(argv[i], "--version") == 0) {
|
|
|
|
printf("IR %s\n", IR_VERSION);
|
|
|
|
return 0;
|
2022-05-25 16:38:22 +02:00
|
|
|
} else if (strcmp(argv[i], "--target") == 0) {
|
|
|
|
printf("%s\n", IR_TARGET);
|
|
|
|
return 0;
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (argv[i][0] == '-' && argv[i][1] == 'O' && strlen(argv[i]) == 3) {
|
|
|
|
if (argv[i][2] == '0') {
|
|
|
|
opt_level = 0;
|
|
|
|
} else if (argv[i][2] == '1') {
|
|
|
|
opt_level = 1;
|
|
|
|
} else if (argv[i][2] == '2') {
|
|
|
|
opt_level = 2;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--emit-c") == 0) {
|
|
|
|
emit_c = 1;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
c_file = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
2023-09-28 19:44:45 +02:00
|
|
|
} else if (strcmp(argv[i], "--emit-llvm") == 0) {
|
|
|
|
emit_llvm = 1;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
llvm_file = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (strcmp(argv[i], "--save") == 0) {
|
|
|
|
// TODO: check save/dot/dump/... conflicts
|
|
|
|
dump |= IR_DUMP_SAVE;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
dump_file = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--dot") == 0) {
|
|
|
|
dump |= IR_DUMP_DOT;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
dump_file = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--dump") == 0) {
|
|
|
|
dump |= IR_DUMP_DUMP;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
dump_file = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--dump-use-lists") == 0) {
|
|
|
|
dump |= IR_DUMP_USE_LISTS;
|
|
|
|
} else if (strcmp(argv[i], "--dump-cfg") == 0) {
|
|
|
|
dump |= IR_DUMP_CFG;
|
2022-05-25 08:33:47 +02:00
|
|
|
} else if (strcmp(argv[i], "--dump-cfg-map") == 0) {
|
|
|
|
dump |= IR_DUMP_CFG_MAP;
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (strcmp(argv[i], "--dump-live-ranges") == 0) {
|
|
|
|
dump |= IR_DUMP_LIVE_RANGES;
|
2023-06-21 21:36:36 +02:00
|
|
|
} else if (strcmp(argv[i], "--dump-codegen") == 0) {
|
|
|
|
dump |= IR_DUMP_CODEGEN;
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (strcmp(argv[i], "--dump-after-load") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_LOAD;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-sccp") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_SCCP;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-gcm") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_GCM;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-schedule") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_SCHEDULE;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-live-ranges") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_LIVE_RANGES;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-coalescing") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_COALESCING;
|
|
|
|
} else if (strcmp(argv[i], "--dump-after-all") == 0) {
|
|
|
|
dump |= IR_DUMP_AFTER_ALL;
|
|
|
|
} else if (strcmp(argv[i], "--dump-final") == 0) {
|
|
|
|
dump |= IR_DUMP_FINAL;
|
2023-02-21 20:22:41 +01:00
|
|
|
} else if (strcmp(argv[i], "--dump-size") == 0) {
|
|
|
|
dump_size = 1;
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (strcmp(argv[i], "-S") == 0) {
|
|
|
|
dump_asm = 1;
|
2022-04-07 17:08:06 +02:00
|
|
|
} else if (strcmp(argv[i], "--run") == 0) {
|
|
|
|
run = 1;
|
2023-04-18 08:54:35 +02:00
|
|
|
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (strcmp(argv[i], "-mavx") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
mflags |= IR_X86_AVX;
|
|
|
|
#endif
|
2022-05-17 21:37:13 +02:00
|
|
|
} else if (strcmp(argv[i], "-muse-fp") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_USE_FRAME_POINTER;
|
2022-06-22 22:59:56 +02:00
|
|
|
} else if (strcmp(argv[i], "-mfastcall") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_FASTCALL_FUNC;
|
2022-04-27 13:47:52 +02:00
|
|
|
#ifdef IR_DEBUG
|
|
|
|
} else if (strcmp(argv[i], "--debug-sccp") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_DEBUG_SCCP;
|
2022-04-27 13:47:52 +02:00
|
|
|
} else if (strcmp(argv[i], "--debug-gcm") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_DEBUG_GCM;
|
2022-05-24 17:04:38 +02:00
|
|
|
} else if (strcmp(argv[i], "--debug-schedule") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_DEBUG_SCHEDULE;
|
2022-04-27 13:47:52 +02:00
|
|
|
} else if (strcmp(argv[i], "--debug-ra") == 0) {
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_DEBUG_RA;
|
2022-05-13 08:22:31 +02:00
|
|
|
#endif
|
2022-04-27 13:47:52 +02:00
|
|
|
} else if (strcmp(argv[i], "--debug-regset") == 0) {
|
|
|
|
if (i + 1 == argc || argv[i + 1][0] == '-') {
|
|
|
|
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-31 23:34:45 +02:00
|
|
|
debug_regset = strtoull(argv[i + 1], NULL, 0);
|
2022-04-27 13:47:52 +02:00
|
|
|
i++;
|
2023-03-01 18:31:14 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
} else if (strcmp(argv[i], "--no-abort-fault") == 0) {
|
|
|
|
abort_fault = 0;
|
2023-10-11 11:46:31 +02:00
|
|
|
#endif
|
|
|
|
#if HAVE_LLVM
|
|
|
|
} else if (strcmp(argv[i], "--llvm-bitcode") == 0) {
|
|
|
|
if (input || i + 1 == argc || argv[i + 1][0] == '-') {
|
|
|
|
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
load_llvm_bitcode = 1;
|
|
|
|
input = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "--llvm-asm") == 0) {
|
|
|
|
if (input || i + 1 == argc || argv[i + 1][0] == '-') {
|
|
|
|
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
load_llvm_asm = 1;
|
|
|
|
input = argv[++i];
|
2023-03-01 18:31:14 +01:00
|
|
|
#endif
|
2022-04-05 23:19:23 +02:00
|
|
|
} else if (argv[i][0] == '-') {
|
|
|
|
fprintf(stderr, "ERROR: Unknown option '%s' (use --help)\n", argv[i]);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (input) {
|
|
|
|
fprintf(stderr, "ERROR: Invalid usage' (use --help)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
input = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dump && !(dump & (IR_DUMP_AFTER_LOAD|IR_DUMP_AFTER_SCCP|
|
|
|
|
IR_DUMP_AFTER_GCM|IR_DUMP_AFTER_SCHEDULE|
|
|
|
|
IR_DUMP_AFTER_LIVE_RANGES|IR_DUMP_AFTER_COALESCING|IR_DUMP_FINAL))) {
|
|
|
|
dump |= IR_DUMP_FINAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input) {
|
|
|
|
fprintf(stderr, "ERROR: no input file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:54:35 +02:00
|
|
|
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
|
|
|
|
uint32_t cpuinfo = ir_cpuinfo();
|
|
|
|
|
|
|
|
if (!(cpuinfo & IR_X86_SSE2)) {
|
|
|
|
fprintf(stderr, "ERROR: incompatible CPU (SSE2 is not supported)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-09 03:06:36 +02:00
|
|
|
if ((mflags & IR_X86_AVX) && !(cpuinfo & IR_X86_AVX)) {
|
|
|
|
fprintf(stderr, "ERROR: -mavx is not compatible with CPU (AVX is not supported)\n");
|
2023-04-18 08:54:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-01 18:31:14 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!abort_fault) {
|
|
|
|
_set_abort_behavior(0, _WRITE_ABORT_MSG|_CALL_REPORTFAULT);
|
2023-03-07 08:54:46 +01:00
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
|
2023-03-01 18:31:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:54:35 +02:00
|
|
|
flags |= IR_FUNCTION;
|
2022-04-05 23:19:23 +02:00
|
|
|
|
|
|
|
if (opt_level > 0) {
|
2023-03-27 01:09:35 +02:00
|
|
|
flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN;
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-09-28 19:44:45 +02:00
|
|
|
if (emit_c || emit_llvm) {
|
|
|
|
flags |= IR_GEN_CODE;
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-10-18 10:45:57 +02:00
|
|
|
if (dump_asm || dump_size || run) {
|
2023-03-27 01:09:35 +02:00
|
|
|
flags |= IR_GEN_NATIVE;
|
2023-10-20 00:09:46 +02:00
|
|
|
if (emit_c) {
|
|
|
|
fprintf(stderr, "ERROR: --emit-c is incompatible with native code generator (-S, --dump-size, --run)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-10-11 11:46:31 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.loader.default_func_flags = flags;
|
|
|
|
loader.loader.init_module = NULL;
|
|
|
|
loader.loader.external_sym_dcl = ir_loader_external_sym_dcl;
|
|
|
|
loader.loader.external_func_dcl = ir_loader_external_func_dcl;
|
2023-10-20 16:44:45 +02:00
|
|
|
loader.loader.forward_func_dcl = ir_loader_forward_func_dcl;
|
|
|
|
loader.loader.sym_dcl = ir_loader_sym_dcl;
|
|
|
|
loader.loader.sym_data = ir_loader_sym_data;
|
|
|
|
loader.loader.sym_data_end = ir_loader_sym_data_end;
|
|
|
|
loader.loader.func_init = ir_loader_func_init;
|
|
|
|
loader.loader.func_process = ir_loader_func_process;
|
2023-10-20 00:09:46 +02:00
|
|
|
loader.loader.resolve_sym_name = ir_loader_resolve_sym_name;
|
2023-10-11 11:46:31 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.opt_level = opt_level;
|
|
|
|
loader.mflags = mflags;
|
|
|
|
loader.debug_regset = debug_regset;
|
|
|
|
loader.dump = dump;
|
|
|
|
loader.dump_asm = dump_asm;
|
|
|
|
loader.dump_size = dump_size;
|
|
|
|
loader.run = run;
|
2023-10-11 11:46:31 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.size = 0;
|
|
|
|
loader.main = NULL;
|
2022-04-05 23:19:23 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.dump_file = NULL;
|
|
|
|
loader.c_file = NULL;
|
|
|
|
loader.llvm_file = NULL;
|
2022-04-05 23:19:23 +02:00
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
ir_strtab_init(&loader.symtab, 16, 4096);
|
|
|
|
loader.sym = NULL;
|
|
|
|
loader.sym_count = 0;
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
if (dump_file) {
|
|
|
|
loader.dump_file = fopen(dump_file, "w+");
|
|
|
|
if (!loader.dump_file) {
|
|
|
|
fprintf(stderr, "ERROR: Cannot create file '%s'\n", dump_file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loader.dump_file = stderr;
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
|
|
|
if (emit_c) {
|
|
|
|
if (c_file) {
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.c_file = fopen(c_file, "w+");
|
|
|
|
if (!loader.c_file) {
|
2022-04-05 23:19:23 +02:00
|
|
|
fprintf(stderr, "ERROR: Cannot create file '%s'\n", c_file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.c_file = stderr;
|
2023-09-14 19:15:30 +02:00
|
|
|
}
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
2023-09-28 19:44:45 +02:00
|
|
|
if (emit_llvm) {
|
|
|
|
if (llvm_file) {
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.llvm_file = fopen(llvm_file, "w+");
|
|
|
|
if (!loader.llvm_file) {
|
2023-09-28 19:44:45 +02:00
|
|
|
fprintf(stderr, "ERROR: Cannot create file '%s'\n", llvm_file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-11 21:55:25 +02:00
|
|
|
loader.llvm_file = stderr;
|
2023-09-28 19:44:45 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_LLVM
|
|
|
|
if (load_llvm_bitcode) {
|
|
|
|
if (!ir_load_llvm_bitcode(&loader.loader, input)) {
|
|
|
|
fprintf(stderr, "ERROR: Cannot load LLVM file '%s'\n", input);
|
|
|
|
return 1;
|
2023-09-28 19:44:45 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
goto finish;
|
|
|
|
} else if (load_llvm_asm) {
|
|
|
|
if (!ir_load_llvm_asm(&loader.loader, input)) {
|
|
|
|
fprintf(stderr, "ERROR: Cannot load LLVM file '%s'\n", input);
|
|
|
|
return 1;
|
2023-09-28 19:44:45 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
goto finish;
|
2023-09-28 19:44:45 +02:00
|
|
|
}
|
2023-10-11 21:55:25 +02:00
|
|
|
#endif
|
2023-09-28 19:44:45 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
f = fopen(input, "rb");
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "ERROR: Cannot open input file '%s'\n", input);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-08-30 14:24:12 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
ir_loader_init();
|
2023-08-30 14:24:12 +02:00
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
if (!ir_load(&loader.loader, f)) {
|
2023-10-11 21:55:25 +02:00
|
|
|
fprintf(stderr, "ERROR: Cannot load input file '%s'\n", input);
|
|
|
|
}
|
2023-08-30 14:24:12 +02:00
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
fclose(f);
|
2022-04-07 17:08:06 +02:00
|
|
|
|
2023-10-11 22:01:15 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
ir_loader_free();
|
2022-04-07 17:08:06 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
#if HAVE_LLVM
|
|
|
|
finish:
|
2023-02-21 00:19:03 +01:00
|
|
|
#endif
|
2022-04-07 17:08:06 +02:00
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
if (loader.dump_file && loader.dump_file != stderr) {
|
|
|
|
fclose(loader.dump_file);
|
|
|
|
}
|
|
|
|
if (loader.c_file && loader.c_file != stderr) {
|
|
|
|
fclose(loader.c_file);
|
|
|
|
}
|
|
|
|
if (loader.llvm_file && loader.llvm_file != stderr) {
|
|
|
|
fclose(loader.llvm_file);
|
2022-04-05 23:19:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
if (dump_size) {
|
|
|
|
fprintf(stderr, "\ncode size = %lld\n", (long long int)loader.size);
|
|
|
|
}
|
2022-04-05 23:19:23 +02:00
|
|
|
|
2023-10-20 00:09:46 +02:00
|
|
|
ir_strtab_free(&loader.symtab);
|
|
|
|
if (loader.sym) {
|
|
|
|
ir_mem_free(loader.sym);
|
|
|
|
}
|
|
|
|
|
2023-10-11 21:55:25 +02:00
|
|
|
if (run && loader.main) {
|
|
|
|
int (*func)(void) = loader.main;
|
|
|
|
int ret = func();
|
|
|
|
fflush(stdout);
|
|
|
|
fprintf(stderr, "\nexit code = %d\n", ret);
|
|
|
|
}
|
2022-04-05 23:19:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|