From 44362494330ae6475e00d0439c7191703b41e323 Mon Sep 17 00:00:00 2001 From: CanardConfit Date: Fri, 14 Jun 2024 11:29:11 +0200 Subject: [PATCH] Add JMP instruction --- README.md | 1 + utils/objects.ts | 3 ++- utils/v1-compiler.ts | 6 ++++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1df39ef..a185d25 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ The cc-compiler project is developed as part of an exercise at [HEPIA in Geneva] | `}` | | ` | Brace to end if or while block | | `STORE R[0-7] R[0-7] x` | x = offset int | `STORE R0 R1 0` | Store from value into R[0-7] to RAM or peripheral (R[0-7] value pointer + offset) | | `LOAD R[0-7] R[0-7] x` | x = offset int | `LOAD R1 R2 0` | Store from RAM or peripheral (R[0-7] value pointer + offset) to R[0-7] variable | +| `JMP x` | x = Any int number (signed) | `JMP 2` | Jump unconditional relative. | ## Assembler instructions diff --git a/utils/objects.ts b/utils/objects.ts index 018a2fe..07834dc 100644 --- a/utils/objects.ts +++ b/utils/objects.ts @@ -106,7 +106,7 @@ while true `}]; export enum TreeType { - Entry, Assignation, While, IF, IF_SUB, IF_COND, IF_ELSE, ADD, SUB, DecG, DecD, DecA, AND, OR, NOT, START_BRACE, END_BRACE, STORE, LOAD + Entry, Assignation, While, IF, IF_SUB, IF_COND, IF_ELSE, ADD, SUB, DecG, DecD, DecA, AND, OR, NOT, START_BRACE, END_BRACE, STORE, LOAD, JUMP } export class Tree { @@ -173,6 +173,7 @@ export const instructionInfo = [ {id: TreeType.IF_COND, info: "If Operation. Conditional jump that checks whether one of the if conditions is satisfied. Can be inverted (so the “else” receives the “true” cases)."}, {id: TreeType.LOAD, info: "Load Operation. Can load data from a pointer + offset pointed to a peripheral or RAM to a registry variable."}, {id: TreeType.STORE, info: "Store Operation. Can store data from a registry variable to a pointer + offset pointed to a peripheral or RAM."}, + {id: TreeType.JUMP, info: "Jump unconditional relative."} ]; export function getInstructionInfo(type: TreeType) { diff --git a/utils/v1-compiler.ts b/utils/v1-compiler.ts index b722d09..bd9a8b9 100644 --- a/utils/v1-compiler.ts +++ b/utils/v1-compiler.ts @@ -1,5 +1,4 @@ import {Tree, CCLine, CCLineAsm, TreeType} from "~/utils/objects"; -import {weight} from "postcss-minify-font-values/types/lib/keywords"; function print_tree(tree: Tree, depth=0) { if (!tree) { @@ -18,7 +17,7 @@ function print_tree(tree: Tree, depth=0) { } const PATTERNS: {regex: RegExp, type: TreeType}[] = [ - {regex: /R([0-7])\s=\s*(-?\d+)/, type: TreeType.Assignation}, + {regex: /R([0-7])\s*=\s*(-?\d+)/, type: TreeType.Assignation}, {regex: /while\s+(not)?\s*([NZCV]|true)/, type: TreeType.While}, {regex: /if\s*R([0-7])\s*([!=><]=?)\s+R([0-7])/, type: TreeType.IF}, {regex: /R([0-7])\s*=\s*R([0-7])\s*\+\s*R([0-7])/, type: TreeType.ADD}, @@ -31,6 +30,7 @@ const PATTERNS: {regex: RegExp, type: TreeType}[] = [ {regex: /R([0-7])\s*=\s*not\s*R([0-7])/, type: TreeType.NOT}, {regex: /STORE\s*R([0-7])\s*R([0-7])\s*(-?\d*)/, type: TreeType.STORE}, {regex: /LOAD\s*R([0-7])\s*R([0-7])\s*(-?\d*)/, type: TreeType.LOAD}, + {regex: /JMP\s*(-?\d*)/, type: TreeType.JUMP}, {regex: /^{/, type: TreeType.START_BRACE}, {regex: /^}/, type: TreeType.END_BRACE}, ]; @@ -205,6 +205,8 @@ function compute_asm(tree: Tree): CCLine[] { return [l(tree, [cc("opcode", `1101`), cc("Rs", `${string_to_binary(tree.fields[0] as string)}`), cc("Rp", `${string_to_binary(tree.fields[1] as string)}`), cc("offset", `${string_to_binary(tree.fields[2] as string, 6)}`)])]; } else if (tree.type == TreeType.LOAD) { return [l(tree, [cc("opcode", `1100`), cc("Rd", `${string_to_binary(tree.fields[0] as string)}`), cc("Rp", `${string_to_binary(tree.fields[1] as string)}`), cc("offset", `${string_to_binary(tree.fields[2] as string, 6)}`)])]; + } else if (tree.type == TreeType.JUMP) { + return [l(tree, [cc("opcode", `1011`), cc("x", `0000`), cc("val", `${string_to_binary(tree.fields[0] as string, 8)}`)])]; } else if (tree.type == TreeType.ADD) { return [asm_3fields("0000", tree)]; } else if (tree.type == TreeType.SUB) {