Skip to content

Commit

Permalink
Add JMP instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
CanardConfit committed Jun 14, 2024
1 parent 910e8ee commit 4436249
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion utils/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions utils/v1-compiler.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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},
Expand All @@ -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},
];
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4436249

Please sign in to comment.