summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/cat/cat.c1
-rw-r--r--usr.bin/oasm/emit.c72
-rw-r--r--usr.bin/oasm/include/oasm/lex.h5
-rw-r--r--usr.bin/oasm/lex.c9
-rw-r--r--usr.bin/oasm/parse.c26
-rw-r--r--usr.bin/oemu/cpu.c60
-rw-r--r--usr.bin/osh/osh.c19
7 files changed, 182 insertions, 10 deletions
diff --git a/usr.bin/cat/cat.c b/usr.bin/cat/cat.c
index b51e6a0..35e9e95 100644
--- a/usr.bin/cat/cat.c
+++ b/usr.bin/cat/cat.c
@@ -62,4 +62,3 @@ main(int argc, char **argv)
return 0;
}
-
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c
index 139cf47..63f716a 100644
--- a/usr.bin/oasm/emit.c
+++ b/usr.bin/oasm/emit.c
@@ -377,6 +377,71 @@ emit_encode_nop(struct emit_state *state, struct oasm_token *tok)
return TAILQ_NEXT(tok, link);
}
+/*
+ * Encode a bitwise instruction:
+ *
+ * and r, r/imm
+ * or r, r/imm
+ * xor r, r/imm
+ */
+static struct oasm_token *
+emit_encode_bitw(struct emit_state *state, struct oasm_token *tok)
+{
+ inst_t curinst;
+ imm_t imm;
+ reg_t rd;
+ uint8_t opcode = OSMX64_AND;
+ char *inst_str = "and";
+
+ switch (tok->type) {
+ case TT_OR:
+ opcode = OSMX64_OR;
+ inst_str = "or";
+ break;
+ case TT_XOR:
+ opcode = OSMX64_XOR;
+ inst_str = "xor";
+ break;
+ }
+
+ /* Next token should be a register */
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ oasm_err("[emit error]: expected register for '%s'\n", inst_str);
+ return NULL;
+ }
+ if (!tok_is_xreg(tok->type)) {
+ oasm_err("[emit error]: bad register for '%s'\n", inst_str);
+ return NULL;
+ }
+
+ rd = ir_to_reg(tok->type);
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ oasm_err("[emit error]: missing operand in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ /*
+ * Check that the next token is an immediate
+ * value.
+ *
+ * TODO: Allow a register operand to be passed
+ * to these instructions.
+ */
+ if (tok->type != TT_IMM) {
+ oasm_err("[emit error]: expected <imm> for '%s'\n", inst_str);
+ return NULL;
+ }
+
+ imm = tok->imm;
+ curinst.opcode = opcode;
+ curinst.rd = rd;
+ curinst.imm = imm;
+ emit_bytes(state, &curinst, sizeof(curinst));
+ return TAILQ_NEXT(tok, link);
+}
+
int
emit_osmx64(struct emit_state *state, struct oasm_token *tp)
{
@@ -464,6 +529,11 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
case TT_DIV:
curtok = emit_encode_arith(emit, curtok);
break;
+ case TT_AND:
+ case TT_OR:
+ case TT_XOR:
+ curtok = emit_encode_bitw(emit, curtok);
+ break;
case TT_BR:
curtok = emit_encode_br(emit, curtok);
break;
@@ -471,7 +541,7 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
curtok = emit_encode_hlt(emit, curtok);
break;
default:
- if (lex_is_mro(curtok->type)) {
+ if (tok_is_mro(curtok->type)) {
curtok = emit_encode_mro(emit, curtok);
break;
}
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index 375771e..4c81bc4 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -103,6 +103,9 @@ typedef enum {
TT_MROW, /* 'mrow' */
TT_MROD, /* 'mrod' */
TT_MROQ, /* 'mroq' */
+ TT_AND, /* 'and' */
+ TT_OR, /* 'or' */
+ TT_XOR, /* 'xor' */
/* Register ops */
TT_MOV, /* 'mov' */
@@ -167,7 +170,7 @@ tok_is_xreg(tt_t tok)
* instruction. Returns true on match.
*/
__always_inline static inline bool
-lex_is_mro(tt_t tok)
+tok_is_mro(tt_t tok)
{
switch (tok) {
case TT_MROB:
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index fea9dc3..ce97400 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -54,6 +54,9 @@ static char putback = '\0';
#define S_IMN_MROW "mrow"
#define S_IMN_MROD "mrod"
#define S_IMN_MROQ "mroq"
+#define S_IMN_AND "and"
+#define S_IMN_OR "or"
+#define S_IMN_XOR "xor"
/* Instruction length */
#define OSMX64_INST_LEN 4
@@ -210,6 +213,8 @@ token_arith(char *p)
return TT_HLT;
} else if (strcmp(p, S_IMN_MUL) == 0) {
return TT_MUL;
+ } else if (strcmp(p, S_IMN_XOR) == 0) {
+ return TT_XOR;
}
return TT_UNKNOWN;
@@ -242,6 +247,10 @@ token_bitw_mro(char *p)
return TT_MROD;
} else if (strcmp(p, S_IMN_MROQ) == 0) {
return TT_MROQ;
+ } else if (strcmp(p, S_IMN_AND) == 0) {
+ return TT_AND;
+ } else if (strcmp(p, S_IMN_OR) == 0) {
+ return TT_OR;
}
return TT_UNKNOWN;
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index ba97306..ce5a446 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -58,7 +58,9 @@ static const char *tokstr[] = {
[ TT_MROW ] = "mrow",
[ TT_MROD ] = "mrod",
[ TT_MROQ ] = "mroq",
-
+ [ TT_AND ] = "and",
+ [ TT_OR ] = "or",
+ [ TT_XOR ] = "xor",
/* X<n> registers */
[ TT_X0 ] = "x0",
@@ -124,16 +126,18 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok)
case TT_MUL:
case TT_DIV:
case TT_BR:
+ case TT_AND:
+ case TT_OR:
+ case TT_XOR:
state->last = tok->type;
break;
default:
- if (lex_is_mro(state->last)) {
- state->last = tok->type;
+ if (tok_is_mro(state->last)) {
break;
}
p = tokstr[state->last];
- oasm_err("bad instruction '%s' for regop\n", p);
+ oasm_err("bad token '%s' for regop\n", p);
return -1;
}
@@ -167,6 +171,18 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
state->last = tok->type;
label_enter(tok->raw, state->pip);
break;
+ case TT_AND:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
+ case TT_OR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
+ case TT_XOR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
case TT_HLT:
state->last = tok->type;
emit_osmx64(&emit_state, tok);
@@ -205,7 +221,7 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
emit_osmx64(&emit_state, tok);
break;
default:
- if (lex_is_mro(tok->type)) {
+ if (tok_is_mro(tok->type)) {
state->last = tok->type;
emit_osmx64(&emit_state, tok);
return 0;
diff --git a/usr.bin/oemu/cpu.c b/usr.bin/oemu/cpu.c
index 49d4671..4aab553 100644
--- a/usr.bin/oemu/cpu.c
+++ b/usr.bin/oemu/cpu.c
@@ -187,6 +187,56 @@ cpu_mul(struct oemu_cpu *cpu, inst_t *inst)
printf("%d * %d -> X%d, new=%d\n",
imm, inst->imm, inst->rd, regs->xreg[inst->rd]);
}
+static void
+cpu_and(struct oemu_cpu *cpu, inst_t *inst)
+{
+ struct cpu_regs *regs = &cpu->regs;
+ imm_t imm;
+
+ if (inst->rd > NELEM(regs->xreg)) {
+ printf("bad register operand for 'and'\n");
+ return;
+ }
+
+ imm = inst->imm;
+ regs->xreg[inst->rd] &= inst->imm;
+ printf("X%d & %x -> X%d, new=%d\n",
+ inst->rd, inst->imm, inst->rd, regs->xreg[inst->rd]);
+}
+
+static void
+cpu_or(struct oemu_cpu *cpu, inst_t *inst)
+{
+ struct cpu_regs *regs = &cpu->regs;
+ imm_t imm;
+
+ if (inst->rd > NELEM(regs->xreg)) {
+ printf("bad register operand for 'or'\n");
+ return;
+ }
+
+ imm = inst->imm;
+ regs->xreg[inst->rd] |= imm;
+ printf("X%d | %x -> X%d, new=%d\n",
+ inst->rd, inst->imm, inst->rd, regs->xreg[inst->rd]);
+}
+
+static void
+cpu_xor(struct oemu_cpu *cpu, inst_t *inst)
+{
+ struct cpu_regs *regs = &cpu->regs;
+ imm_t imm;
+
+ if (inst->rd > NELEM(regs->xreg)) {
+ printf("bad register operand for 'xor'\n");
+ return;
+ }
+
+ imm = inst->imm;
+ regs->xreg[inst->rd] ^= imm;
+ printf("X%d ^ %x -> X%d, new=%d\n",
+ inst->rd, inst->imm, inst->rd, regs->xreg[inst->rd]);
+}
/*
* Decode the INST_DIV instruction
@@ -335,7 +385,6 @@ cpu_reset(struct oemu_cpu *cpu)
regs->ilr = 0x0;
memset(regs->xreg, 0x0, sizeof(regs->xreg));
}
-
void
cpu_regdump(struct oemu_cpu *cpu)
{
@@ -402,6 +451,15 @@ cpu_kick(struct oemu_cpu *cpu, struct sysmem *mem)
case INST_DIV:
cpu_div(cpu, inst);
break;
+ case INST_AND:
+ cpu_and(cpu, inst);
+ break;
+ case INST_OR:
+ cpu_or(cpu, inst);
+ break;
+ case INST_XOR:
+ cpu_xor(cpu, inst);
+ break;
case INST_BR:
cpu_br(cpu, inst);
break;
diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c
index 3b41747..545f95a 100644
--- a/usr.bin/osh/osh.c
+++ b/usr.bin/osh/osh.c
@@ -43,6 +43,9 @@
#define is_printable(C) ((C) >= 32 && (C) <= 126)
#define is_ascii(C) ((C) >= 0 && (C) <= 128)
+#define INPUT_SIZE 64
+
+#define REPEAT "!!"
#define COMMENT '@'
#define WELCOME \
":::::::::::::::::::::::::::::::::::::::\n" \
@@ -66,7 +69,8 @@
#define PROMPT "[%s::osmora]~ "
-static char buf[64];
+static char last_command[INPUT_SIZE];
+static char buf[INPUT_SIZE];
static int running;
static int bell_fd;
static bool bs_bell = true; /* Beep on backspace */
@@ -365,6 +369,18 @@ parse_line(char *input)
struct parse_state state = {0};
pid_t child;
+ /*
+ * If we are using the REPEAT shorthand,
+ * repeat the last command. We return -EAGAIN
+ * to indicate we did not parse a normal command
+ * so the repeat command isn't pushed into the last
+ * command buffer and we enter a recursive hell.
+ */
+ if (strcmp(input, REPEAT) == 0) {
+ parse_line(last_command);
+ return -EAGAIN;
+ }
+
/* Ensure the aux vector is zeored */
memset(argv, 0, sizeof(argv));
@@ -476,6 +492,7 @@ main(int argc, char **argv)
continue;
}
+ memcpy(last_command, buf, buf_i + 1);
buf[0] = '\0';
}
return 0;