1
0
Fork 0
mirror of https://github.com/pnx/m16vm synced 2026-06-16 03:44:55 +02:00

asm: rename instr_encode to codegen and do code generation from AST.

This commit is contained in:
Henrik Hautakoski 2018-12-10 10:06:12 +01:00
parent f2d54204ca
commit 96220537de
No known key found for this signature in database
GPG key ID: 839F3A7EAFAEAFAA
3 changed files with 67 additions and 56 deletions

61
src/as/codegen.c Normal file
View file

@ -0,0 +1,61 @@
/* codegen.c
*
* Copyright (C) 2018 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "codegen.h"
void codegen_emit(struct ast_instr* instr, symtab_t* symbols, uint8_t* out) {
out[0] = instr->opcode << 4;
if (instr->n_operands < 1)
// No operands, return
return;
// J-Type (We don't have labels yet, so this is just a address)
if (instr->operands[0].type == DATATYPE_NUMBER) {
//if (instr->operands[0].type == DATATYPE_STRING) {
uint16_t addr;
/* if (symtab_get(symbols, instr->operands[0].s, &addr)) {
out[0] |= (addr >> 8);
out[1] = addr;
}*/
addr = instr->operands[0].n;
out[0] |= (addr >> 8);
out[1] = addr;
}
// R/RI or I
else if (instr->operands[0].type == DATATYPE_REGISTER) {
out[0] |= instr->operands[0].r & 0xF;
// I
if (instr->operands[1].type == DATATYPE_NUMBER) {
out[1] = instr->operands[1].n;
}
// R/RI-Type
else if (instr->operands[1].type == DATATYPE_REGISTER &&
(instr->operands[2].type == DATATYPE_REGISTER || instr->operands[2].type == DATATYPE_NUMBER)) {
out[1] = (instr->operands[1].r << 4) | (instr->operands[2].r & 0xF);
}
}
}

View file

@ -1,4 +1,4 @@
/* instr_encode.h
/* codegen.h
*
* Copyright (C) 2018 Henrik Hautakoski <henrik.hautakoski@gmail.com>
*
@ -17,11 +17,11 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef ASM_INSTR_ENCODE_H
#define ASM_INSTR_ENCODE_H
#ifndef ASM_CODEGEN_H
#define ASM_CODEGEN_H
#include <instr.h>
#include "ast.h"
void instr_encode(struct instr* instr, uint16_t *out);
void codegen_emit(struct ast_instr* ast, symtab_t* symbols, uint8_t* out);
#endif /* ASM_INSTR_ENCODE_H */
#endif /* ASM_CODEGEN_H */

View file

@ -1,50 +0,0 @@
/* instr_encode.c
*
* Copyright (C) 2018 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include "instr_encode.h"
void instr_encode(struct instr* instr, uint16_t *out) {
uint8_t *p = (uint8_t *) out;
*out = 0;
*p = instr->opcode << 4;
if (instr->opcode == OP_NOOP) {
// Do nothing.
} else if (instr->opcode == OP_JMP) {
*p |= (instr->j.addr >> 8);
*(p+1) = instr->j.addr;
} else {
*p |= instr->r.rs;
// I-Type
if (instr->opcode == OP_MOVL || instr->opcode == OP_MOVH
|| instr->opcode == OP_JR || instr->opcode == OP_INT) {
*(p+1) = instr->i.imm;
}
// R/RI-Type
else {
*(p+1) = (instr->r.r0 << 4) | instr->r.r1;
}
}
}