From 9c07ac829ae4a6c26d1add8ec0e7d972aaa7091d Mon Sep 17 00:00:00 2001 From: "alexander.nutz" Date: Tue, 26 Nov 2024 16:09:04 +0100 Subject: [PATCH] c --- build.c | 13 +- build.sh | 2 +- ir/ir.c | 5 + ir/passes.c | 1 + irparser/match.c | 112 ----------------- irparser/parser.c | 310 ---------------------------------------------- irparser/parser.h | 98 --------------- irparser/serial.c | 31 ----- irparser/test.c | 11 -- 9 files changed, 8 insertions(+), 575 deletions(-) delete mode 100644 irparser/match.c delete mode 100644 irparser/parser.c delete mode 100644 irparser/parser.h delete mode 100644 irparser/serial.c delete mode 100644 irparser/test.c diff --git a/build.c b/build.c index 6dd43b1..a91ec2f 100644 --- a/build.c +++ b/build.c @@ -122,13 +122,6 @@ struct CompileData cg_files[] = { // add target }; -struct CompileData parser_files[] = { - DIR("build"), - DIR("build/irparser"), - SP(CT_C, "irparser/parser.c"), - SP(CT_C, "irparser/match.c"), -}; - struct CompileData always_files[] = { DEP("build/targets/etca/etca.cdef.o"), DEP("build/targets/x86/x86.cdef.o"), @@ -158,12 +151,9 @@ enum CompileResult target_lib() { if (source_changed(LI(ir_verify_files)) || all) comp = vaListConcat(comp, ASVAR(ir_verify_files)); - if (file_changed("cg/") || all) + if (file_changed("targets/") || all) comp = vaListConcat(comp, ASVAR(cg_files)); - if (file_changed("irparser/") || all) - comp = vaListConcat(comp, ASVAR(parser_files)); - DO(compile(VLI(comp))); VaList link = ASVAR(always_files); @@ -172,7 +162,6 @@ enum CompileResult target_lib() { link = vaListConcat(link, ASVAR(ir_transform_files)); link = vaListConcat(link, ASVAR(ir_verify_files)); link = vaListConcat(link, ASVAR(cg_files)); - link = vaListConcat(link, ASVAR(parser_files)); DO(linkTask(VLI(link), "build/lib.a")); diff --git a/build.sh b/build.sh index 7e58c3e..efdb8b6 100755 --- a/build.sh +++ b/build.sh @@ -60,7 +60,7 @@ CFLAGS="$CFLAGS $EX_CFLAGS" # fi #fi -FILES="ir/*.c common/*.c ir/opt/*.c ir/transform/*.c cg/x86_stupid/*.c irparser/*.c" +FILES="ir/*.c targets/*.c ir/opt/*.c ir/transform/*.c" # shellcheck disable=SC2086 diff --git a/ir/ir.c b/ir/ir.c index 8a84b00..3099e15 100644 --- a/ir/ir.c +++ b/ir/ir.c @@ -291,6 +291,11 @@ int vx_CU_compile(vx_CU * cu, } FOR_BLOCK({ + if (!block->is_root) { + fprintf(stderr, "only root blocks allowed in CU\n"); + exit(1); + } + vx_CIrBlock_fix(cu, block); // TODO: why... vx_CIrBlock_normalize(cu, block); vx_CIrBlock_mksa_states(cu, block); diff --git a/ir/passes.c b/ir/passes.c index e5bf5b5..dd7ceb3 100644 --- a/ir/passes.c +++ b/ir/passes.c @@ -86,6 +86,7 @@ void vx_opt_ll(vx_CU* cu, vx_IrBlock *block) { } void vx_llir_prep_lower(vx_CU* cu, vx_IrBlock *block) { + assert(block->is_root); vx_IrBlock_llir_fix_decl(cu, block); //vx_IrBlock_llir_compact(block); /TODO? vx_IrBlock_lifetimes(cu, block); diff --git a/irparser/match.c b/irparser/match.c deleted file mode 100644 index 3a3c63a..0000000 --- a/irparser/match.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "parser.h" - -static bool try_match_op(uint32_t i, OptPatternMatch* out, CompOperation* op, vx_IrOp* irop, bool *placeholders_matched) -{ - if (op->specific.op_type != irop->id) - return false; - if (op->specific.outputs.count != irop->outs_len) - return false; - - for (uint32_t i = 0; i < op->specific.operands.count; i ++) - { - CompOperand* operand = &op->specific.operands.items[i]; - - vx_IrValue* val = vx_IrOp_param(irop, operand->name); - if (!val) return false; - - switch (operand->type) - { - case OPERAND_TYPE_PLACEHOLDER: { - if (val->type != VX_IR_VAL_VAR) return false; - uint32_t ph = operand->v.placeholder; - if (placeholders_matched[ph]) { - if (out->matched_placeholders[ph] != val->var) - return false; - } else { - out->matched_placeholders[ph] = val->var; - placeholders_matched[ph] = true; - } - } break; - - case OPERAND_TYPE_IMM_INT: { - if (val->type != VX_IR_VAL_IMM_INT) return false; - if (val->imm_int != operand->v.imm_int) return false; - } break; - - case OPERAND_TYPE_IMM_FLT: { - if (val->type != VX_IR_VAL_IMM_FLT) return false; - if (val->imm_flt != operand->v.imm_flt) return false; - } break; - } - } - - return true; -} - -static bool try_match_at(OptPatternMatch* out, CompPattern pattern, vx_IrOp* irop, bool *placeholders_matched) -{ - for (uint32_t i = 0; i < pattern.count; i ++) { - CompOperation* current = &pattern.items[i]; - CompOperation* next = i + 1 < pattern.count ? &pattern.items[i + 1] : NULL; - - if (current->is_any && next && next->is_any) continue; - - if (current->is_any && next) { - vx_IrOp* mfirst = irop; - vx_IrOp* mlast = NULL; - while (!try_match_op(i + 1, out, next, irop, placeholders_matched)) { - mlast = irop; - irop = irop->next; - } - if (irop == mfirst) mfirst = NULL; - out->matched_instrs[i].first = mfirst; - out->matched_instrs[i].last = mlast; - i ++; // skip processing of next, since already matched - continue; - } - else if (current->is_any) { - out->matched_instrs[i].first = irop; - for (; irop; irop = irop->next); - out->matched_instrs[i].last = vx_IrOp_predecessor(irop); - break; // no next - } - else { - if (!try_match_op(i, out, current, irop, placeholders_matched)) - return false; - out->matched_instrs[i].first = irop; - out->matched_instrs[i].last = irop; - irop = irop->next; - } - } - - out->found = true; - out->last = vx_IrOp_predecessor(irop); - return true; -} - -OptPatternMatch Pattern_matchNext(vx_IrOp* first, CompPattern pattern) -{ - OptPatternMatch match; - match.found = false; - match.last = NULL; - - match.matched_placeholders = malloc(pattern.placeholders_count * sizeof(vx_IrVar)); - match.matched_instrs = malloc(pattern.count * sizeof(PatternInstMatch)); - bool *placeholders_matched = calloc(pattern.placeholders_count, sizeof(bool)); - - for (; first; first = first->next) { - memset(placeholders_matched, 0, pattern.placeholders_count * sizeof(bool)); - if (try_match_at(&match, pattern, first, placeholders_matched)) - break; - } - - free(placeholders_matched); - - return match; -} - -void OptPatternMatch_free(OptPatternMatch match) -{ - free(match.matched_placeholders); - free(match.matched_instrs); -} diff --git a/irparser/parser.c b/irparser/parser.c deleted file mode 100644 index 5210053..0000000 --- a/irparser/parser.c +++ /dev/null @@ -1,310 +0,0 @@ -#include -#include -#include -#include -#include "parser.h" - - -typedef struct { - vx_IrName name; - OperandType type; - union { - char * placeholder; - double imm_flt; - } v; -} Operand; - -typedef struct { - struct { - char ** items; - uint32_t count; - } outputs; - - const char * name; - uint32_t name_len; - - struct { - Operand * items; - uint32_t count; - } operands; -} Operation; - -typedef struct { - struct { - Operation * items; - uint32_t count; - } operations; -} Pattern; - -static Operation parse_op(char * line, uint32_t line_len) -{ - Operation op = (Operation) { - .outputs.items = NULL, - .outputs.count = 0, - - .operands.items = NULL, - .operands.count = 0 - }; - - while (isspace(*line)) line ++; - - char * assign = strchr(line, '='); - if (assign) { - while (true) - { - while (isspace(*line)) line ++; - - char * comma = strchr(line, ','); - char * last = comma ? comma : assign; - uint32_t count = last - line; - - char* buf = malloc(count + 1); - memcpy(buf, line, count); - buf[count] = '\0'; - op.outputs.items = realloc(op.outputs.items, (op.outputs.count + 1) * sizeof(char*)); - op.outputs.items[op.outputs.count ++] = buf; - - if (comma > assign) - break; - - if (comma) - line = comma + 1; - else - break; - } - - line = assign + 1; - } - - while (isspace(*line)) line ++; - - const char * name_begin = line; - uint32_t name_len = 0; - while (isalnum(*line)) { - name_len ++; - line ++; - } - op.name = name_begin; - op.name_len = name_len; - - while (isspace(*line)) line ++; - - assert(*line == '('); line ++; - - char * close = strchr(line, ')'); assert(close); - - while (true) - { - while (isspace(*line)) line ++; - - vx_IrName opnam; - { - char * eqsign = strchr(line, '='); - assert(eqsign); - assert(eqsign < close); - *eqsign = '\0'; - char * space = strchr(line, ' '); - if (space) *space = '\0'; - opnam = vx_IrName_parsec(line); - line = eqsign; - - } - while (isspace(*line)) line ++; - - char * comma = strchr(line, ','); - char * last = comma ? comma : (close - 1); - uint32_t count = last - line; - - char* buf = malloc(count + 1); - memcpy(buf, line, count); - buf[count] = '\0'; - - Operand operand; - operand.name = opnam; - if (isdigit(*buf)) { - operand.type = OPERAND_TYPE_IMM_FLT; - sscanf(buf, "%lf", &operand.v.imm_flt); - free(buf); - } - else { - operand.type = OPERAND_TYPE_PLACEHOLDER; - operand.v.placeholder = buf; - } - - op.operands.items = realloc(op.operands.items, sizeof(Operand) * (op.operands.count + 1)); - op.operands.items[op.operands.count ++] = operand; - - line = last + 1; - if (!comma) - break; - } - - while (isspace(*line)) line ++; - - assert(*line == ')'); line ++; assert(*line == '\0'); - - return op; -} - -static uint32_t placeholder(const char *** all_placeholders, uint32_t * all_placeholders_len, const char * name) -{ - for (uint32_t i = 0; i < *all_placeholders_len; i ++) { - if (strcmp((*all_placeholders)[i], name) == 0) { - return i; - } - } - uint32_t plLen = *all_placeholders_len ++; - *all_placeholders = realloc(*all_placeholders, sizeof(const char *) * (*all_placeholders_len)); - (*all_placeholders)[plLen] = name; - return plLen; -} - -static CompPattern compile(Pattern pat) -{ - const char ** all_placeholders = NULL; - uint32_t all_placeholders_len = 0; - - CompPattern res; - res.items = malloc(sizeof(CompOperation) * pat.operations.count); - res.count = pat.operations.count; - - for (uint32_t i = 0; i < pat.operations.count; i ++) - { - CompOperation* cop = &res.items[i]; - Operation* op = &pat.operations.items[i]; - - if (strcmp(op->name, "...") == 0) - { - cop->is_any = true; - assert(op->outputs.count == 0); - assert(op->operands.count == 0); - } - else - { - cop->is_any = false; - - bool found = vx_IrOpType_parse(&cop->specific.op_type, op->name, op->name_len); - if (!found) { - ((char*) op->name)[op->name_len] = '\0'; - fprintf(stderr, "No IrOpType \"%s\" found!\n", op->name); - exit(1); - } - - cop->specific.outputs.items = malloc(sizeof(uint32_t) * op->outputs.count); - cop->specific.outputs.count = op->outputs.count; - for (uint32_t i = 0; i < op->outputs.count; i ++) - { - cop->specific.outputs.items[i] = placeholder(&all_placeholders, &all_placeholders_len, op->outputs.items[i]); - free(op->outputs.items[i]); - } - free(op->outputs.items); - - cop->specific.operands.items = malloc(sizeof(CompOperand) * op->operands.count); - cop->specific.operands.count = op->operands.count; - for (uint32_t i = 0; i < op->operands.count; i ++) - { - CompOperand* co = &cop->specific.operands.items[i]; - co->name = op->operands.items[i].name; - if (op->operands.items[i].type == OPERAND_TYPE_IMM_FLT) { - double val = op->operands.items[i].v.imm_flt; - if (val - (long long int)val == 0) { - co->type = OPERAND_TYPE_IMM_INT; - co->v.imm_int = (long long int) val; - } else { - co->type = OPERAND_TYPE_IMM_FLT; - co->v.imm_flt = val; - } - } else { - co->type = OPERAND_TYPE_PLACEHOLDER; - co->v.placeholder = placeholder(&all_placeholders, &all_placeholders_len, op->operands.items[i].v.placeholder); - free(op->operands.items[i].v.placeholder); - } - } - free(op->operands.items); - } - } - free(pat.operations.items); - - res.placeholders_count = all_placeholders_len; - res.placeholders = malloc(sizeof(germanstr) * res.placeholders_count); - - for (uint32_t i = 0; i < res.placeholders_count; i ++) - res.placeholders[i] = germanstr_cstrdup(all_placeholders[i]); - - free(all_placeholders); - return res; -} - -/** - * example pattern: - * \code - * S = sub(B, C) - * A = add(S, C) - * \endcode - * - * example pattern: - * \code - * B = load(A) - * B = add(B, 1) - * store(A, B) - * \endcode - */ -CompPattern Pattern_compile(const char * source) -{ - Pattern out; - out.operations.items = NULL; - out.operations.count = 0; - - char** linebufs = NULL; - - while (*source) - { - const char * nt_ptr = strchr(source, '\n'); - if (nt_ptr == NULL) nt_ptr = source + strlen(source); - uint32_t line_len = nt_ptr - source; - char* linebuf = malloc(line_len + 1); - memcpy(linebuf, source, line_len+1); - linebuf[line_len] = '\0'; - Operation op = parse_op(linebuf, line_len); - out.operations.items = realloc(out.operations.items, (out.operations.count + 1) * sizeof(Operation)); - linebufs = realloc(linebufs, sizeof(char*) * (out.operations.count + 1)); - linebufs[out.operations.count] = linebuf; - out.operations.items[out.operations.count ++] = op; - source += line_len; - if (!*source) break; - source ++; - } - - CompPattern c = compile(out); - - for (uint32_t i = 0; i < out.operations.count; i ++) - free(linebufs[i]); - free(linebufs); - - return c; -} - -void Pattern_free(CompPattern p) -{ - for (uint32_t i = 0; i < p.count; i ++) - { - if (!p.items[i].is_any) - { - free(p.items[i].specific.operands.items); - free(p.items[i].specific.outputs.items); - } - } - free(p.items); - for (uint32_t i = 0; i < p.placeholders_count; i ++) - germanstr_libcfree(p.placeholders[i]); - free(p.placeholders); -} - -uint32_t Pattern_placeholderId(CompPattern pat, const char * name) -{ - germanstr gname = germanstr_fromc((char*) name); - for (uint32_t i = 0; i < pat.placeholders_count; i ++) - if (germanstr_eq(pat.placeholders[i], gname)) - return i; - assert(false); return 0; -} diff --git a/irparser/parser.h b/irparser/parser.h deleted file mode 100644 index 6179151..0000000 --- a/irparser/parser.h +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include - -#include "../ir/ir.h" -#include "../allib/germanstr/germanstr.h" - - -// serializable by memcpy -typedef uint8_t OperandType; -#define OPERAND_TYPE_PLACEHOLDER ((OperandType) 0) -#define OPERAND_TYPE_IMM_INT ((OperandType) 1) -#define OPERAND_TYPE_IMM_FLT ((OperandType) 2) - -// serializable by memcpy -typedef struct { - vx_IrName name; - OperandType type; - union { - uint32_t placeholder; - long long int imm_int; - double imm_flt; - } v; -} __attribute__((packed)) CompOperand; - -typedef struct { - bool is_any; - - union { - struct { - - } any; - - struct { - struct { - uint32_t * items; - uint32_t count; - } outputs; - - vx_IrOpType op_type; - - struct { - CompOperand * items; - uint32_t count; - } operands; - } specific; - }; -} CompOperation; - -typedef struct { - uint32_t placeholders_count; - germanstr* placeholders; - - CompOperation * items; - uint32_t count; -} CompPattern; - -/** if return 0, not enough space */ -size_t Pattern_serialize(CompPattern pat, void* dest, size_t destzu); - -uint32_t Pattern_placeholderId(CompPattern pat, const char * name); - -// TODO: NAMEDD ARGS - -/** - * `...` means zero or more instructions - * - * \code - * S = sub(B, C) - * ... - * A = add(S, C) - * \endcode - * - * \code - * B = load(A) - * ... - * B = add(B, 1) - * ... - * store(A, B) - * \endcode - */ -CompPattern Pattern_compile(const char * source); -void Pattern_free(CompPattern); - -/** is a range because of any matches; can be NULL (only for any matches) */ -typedef struct { - vx_IrOp* first; - vx_IrOp* last; -} PatternInstMatch; - -typedef struct { - bool found; - vx_IrVar* matched_placeholders; - PatternInstMatch* matched_instrs; - vx_IrOp* last; -} OptPatternMatch; - -OptPatternMatch Pattern_matchNext(vx_IrOp* first, CompPattern pattern); -void OptPatternMatch_free(OptPatternMatch); diff --git a/irparser/serial.c b/irparser/serial.c deleted file mode 100644 index 6280777..0000000 --- a/irparser/serial.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "../common/serializer.h" -#include "parser.h" - -SERIALI(CompOperation_serialize, CompOperation const* o, -({ - WRITEI(uint8_t, (uint8_t) o->is_any); - if (!o->is_any) - { - WRITEI(uint32_t, (uint32_t) o->specific.op_type); - - WRITEI(uint32_t, o->specific.outputs.count); - for (uint32_t i = 0; i < o->specific.outputs.count; i ++) - WRITEI(uint32_t, o->specific.outputs.items[i]); - - WRITEI(uint32_t, o->specific.operands.count); - for (uint32_t i = 0; i < o->specific.operands.count; i ++) - WRITEV(o->specific.operands.items[i]); - } -})); - -/** if return 0, not enough space */ -SERIALI(Pattern_serialize, CompPattern pat, -({ - WRITEI(uint32_t, pat.placeholders_count); - for (uint32_t i = 0; i < pat.placeholders_count; i ++) - WRITEGS(pat.placeholders[i]); - - WRITEI(uint32_t, pat.count); - for (uint32_t i = 0; i < pat.count; i ++) - WR(CompOperation_serialize, &pat.items[i]); -})); diff --git a/irparser/test.c b/irparser/test.c deleted file mode 100644 index 4214abc..0000000 --- a/irparser/test.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "parser.h" - -int main() -{ - CompPattern p = Pattern_compile( - "r = mul(a=a, b=b)" - "\n" "o = add(a=r, b=c)" - ); - - Pattern_free(p); -}