Compare commits
6 Commits
9da8732d93
...
1cae800a66
| Author | SHA1 | Date | |
|---|---|---|---|
| 1cae800a66 | |||
| a073d0ebd9 | |||
| bc54f34a4d | |||
| 9d2a26fae3 | |||
| 83172df776 | |||
| b8296a45a7 |
46
src/VM/VM.c
46
src/VM/VM.c
@ -6,13 +6,30 @@ void VM_init(VM* vm){
|
||||
vm->state = VMState_Initialized;
|
||||
}
|
||||
|
||||
void _VM_setError(VM* vm, const char* context, const char* format, ...){
|
||||
va_list argv;
|
||||
char position_str[32];
|
||||
sprintf(position_str, "[at 0x%x][", (u32)vm->current_pos);
|
||||
char* real_format = strcat_malloc(position_str, context, "] ", format);
|
||||
va_start(argv, format);
|
||||
char* NULLABLE(buf) = vsprintf_malloc(256, real_format, argv);
|
||||
va_end(argv);
|
||||
free(real_format);
|
||||
if(buf == NULL){
|
||||
buf = malloc(16);
|
||||
strcpy(buf, "SPRINTF FAILED");
|
||||
}
|
||||
vm->error_message = buf;
|
||||
vm->state = VMState_InternalError;
|
||||
}
|
||||
|
||||
bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
||||
if(data == NULL){
|
||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
|
||||
VM_setError(vm, "data == NULL");
|
||||
return false;
|
||||
}
|
||||
if(size == 0){
|
||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because size == 0");
|
||||
VM_setError(vm, "size == 0");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -23,30 +40,34 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
||||
|
||||
i32 VM_executeProgram(VM* vm){
|
||||
if(vm->data == NULL){
|
||||
VM_setErrorMessage(vm, "[VM_executeProgram] data is null");
|
||||
VM_setError(vm, "data == null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < vm->data_size){
|
||||
u8 opcode = vm->data[pos];
|
||||
vm->state = VMState_Executing;
|
||||
vm->current_pos = 0;
|
||||
while (vm->current_pos < vm->data_size){
|
||||
u8 opcode = vm->data[vm->current_pos];
|
||||
|
||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
||||
// printfe("[at 0x%x] %02X %s\n", (u32)vm->current_pos, opcode, instr->name);
|
||||
if(instr == NULL){
|
||||
|
||||
VM_setError(vm, "unknown opcode %02X", opcode);
|
||||
return -1;
|
||||
}
|
||||
vm->current_pos++;
|
||||
|
||||
pos++;
|
||||
i32 bytes_read = instr->implementation(vm, pos);
|
||||
i32 bytes_read = instr->implementation(vm);
|
||||
// internal error occured
|
||||
if(bytes_read < 0)
|
||||
return -1;
|
||||
|
||||
pos += bytes_read;
|
||||
if(vm->state == VMState_Exited)
|
||||
break;
|
||||
}
|
||||
|
||||
if(vm->state != VMState_Exited){
|
||||
VM_setErrorMessage(vm, "[%p] unexpected end of program", (void*)pos);
|
||||
VM_setError(vm, "unexpected end of the program");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -56,7 +77,8 @@ i32 VM_executeProgram(VM* vm){
|
||||
|
||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||
if(pos + size >= vm->data_size){
|
||||
VM_setErrorMessage(vm, "[%p] unexpected end of data", (void*)vm->data_size);
|
||||
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable",
|
||||
size, (u32)pos, vm->data_size - size);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
13
src/VM/VM.h
13
src/VM/VM.h
@ -31,6 +31,7 @@ typedef enum VMState {
|
||||
VMState_Initialized,
|
||||
VMState_Executing,
|
||||
VMState_Exited,
|
||||
VMState_InternalError
|
||||
} VMState;
|
||||
|
||||
typedef struct VM {
|
||||
@ -45,10 +46,11 @@ typedef struct VM {
|
||||
};
|
||||
|
||||
VMState state;
|
||||
char* error_message;
|
||||
char* NULLABLE(error_message); // not null on if state == VMState_InternalError
|
||||
|
||||
u8* data;
|
||||
size_t data_size;
|
||||
size_t current_pos;
|
||||
} VM;
|
||||
|
||||
void VM_init(VM* vm);
|
||||
@ -62,10 +64,7 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size);
|
||||
/// @return program exit code or -1 on error (check vm.error_message)
|
||||
i32 VM_executeProgram(VM* vm);
|
||||
|
||||
#define VM_setErrorMessage(V, FORMAT, ...) {\
|
||||
char* buf = malloc(256);\
|
||||
sprintf(buf, FORMAT, ##__VA_ARGS__);\
|
||||
vm->error_message = buf;\
|
||||
}
|
||||
|
||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
|
||||
|
||||
#define VM_setError(vm, format, ...) _VM_setError(vm, __func__, format ,##__VA_ARGS__)
|
||||
void _VM_setError(VM* vm, const char* context, const char* format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||
|
||||
20
src/collections/Array.h
Normal file
20
src/collections/Array.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define Array_declare(T)\
|
||||
typedef struct Array_##T {\
|
||||
T* data;\
|
||||
u32 len;\
|
||||
} Array_##T;\
|
||||
\
|
||||
static inline Array_##T Array_##T##_construct(T* data_ptr, u32 len) {\
|
||||
return (Array_##T){ .data = data_ptr, .len = len };\
|
||||
}\
|
||||
\
|
||||
static inline Array_##T Array_##T##_alloc(u32 len){\
|
||||
return Array_##T##_construct(malloc(len * sizeof(T)), len);\
|
||||
}\
|
||||
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
|
||||
ptr->data = realloc(ptr->data, new_len * sizeof(T));\
|
||||
ptr->len = new_len;\
|
||||
}
|
||||
41
src/collections/List.h
Normal file
41
src/collections/List.h
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define List_declare(T)\
|
||||
typedef struct List_##T {\
|
||||
T* data;\
|
||||
u32 len;\
|
||||
u32 max_len;\
|
||||
} List_##T;\
|
||||
\
|
||||
static inline List_##T List_##T##_construct(T* data_ptr, u32 len, u32 max_len) {\
|
||||
return (List_##T){ .data = data_ptr, .len = len, .max_len = max_len };\
|
||||
}\
|
||||
\
|
||||
static inline List_##T List_##T##_alloc(u32 len){\
|
||||
return List_##T##_construct(len > 0 ? malloc(len * sizeof(T)) : NULL, 0, 0);\
|
||||
}\
|
||||
\
|
||||
void List_##T##_push(List_##T* ptr, T value);
|
||||
|
||||
|
||||
#define List_define(T)\
|
||||
void List_##T##_push(List_##T* ptr, T value){\
|
||||
u32 max_len = ptr->max_len;\
|
||||
if(ptr->len == max_len){\
|
||||
max_len = max_len * 1.5;\
|
||||
max_len += __List_padding_in_sizeof_T(T);\
|
||||
/* branchless version of max(max_len, __List_min_size) */\
|
||||
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
|
||||
ptr->data = realloc(ptr->data, max_len * sizeof(T));\
|
||||
ptr->max_len = max_len;\
|
||||
}\
|
||||
ptr->data[ptr->len++] = value;\
|
||||
}
|
||||
|
||||
#define __List_min_size 16
|
||||
|
||||
// sizeof(T) == 1 - padding is 7 of sizeof(T)
|
||||
// sizeof(T) == 2 - padding is 3 of sizeof(T)
|
||||
// sizeof(T) == 4 - padding is 1 of sizeof(T)
|
||||
#define __List_padding_in_sizeof_T(T) ((8 - sizeof(T) % 8) / sizeof(T) )
|
||||
169
src/compiler/compiler.c
Normal file
169
src/compiler/compiler.c
Normal file
@ -0,0 +1,169 @@
|
||||
#include "compiler.h"
|
||||
|
||||
List_define(Token);
|
||||
|
||||
void _Compiler_setError(Compiler* cmp, const char* context, const char* format, ...){
|
||||
va_list argv;
|
||||
char position_str[32];
|
||||
sprintf(position_str, "[at %u:%u][", cmp->line, cmp->column);
|
||||
char* real_format = strcat_malloc(position_str, context, "] ", format);
|
||||
va_start(argv, format);
|
||||
char* NULLABLE(buf) = vsprintf_malloc(512, real_format, argv);
|
||||
va_end(argv);
|
||||
free(real_format);
|
||||
if(cmp->error_message == NULL){
|
||||
cmp->error_message = malloc(16);
|
||||
strcpy(cmp->error_message, "SPRINTF FAILED");
|
||||
}
|
||||
cmp->state = CompilerState_Error;
|
||||
cmp->error_message = buf;
|
||||
}
|
||||
|
||||
|
||||
#define setError(FORMAT, ...) Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);
|
||||
|
||||
#define returnError(FORMAT, ...) {\
|
||||
setError(FORMAT, ##__VA_ARGS__);\
|
||||
return false;\
|
||||
}
|
||||
|
||||
#define returnErrorIf(STATEMENT, FORMAT, ...) if(STATEMENT) returnError(FORMAT, ##__VA_ARGS__)
|
||||
|
||||
#define returnErrorIf_auto(STATEMENT) returnErrorIf(STATEMENT, #STATEMENT)
|
||||
|
||||
#define Error_UnexpectedCharacter(C) "unexpected character '%c'", C
|
||||
|
||||
#define Error_endOfFile() "unexpected end of file"
|
||||
|
||||
void Compiler_init(Compiler* cmp){
|
||||
memset(cmp, 0, sizeof(Compiler));
|
||||
cmp->state = CompilerState_Initial;
|
||||
cmp->tokens = List_Token_alloc(1024 * 8);
|
||||
}
|
||||
|
||||
void Compiler_free(Compiler* cmp){
|
||||
if(cmp->tokens.data)
|
||||
free(cmp->tokens.data);
|
||||
}
|
||||
|
||||
static void _recognizeLexema(Compiler* cmp, size_t pos, char c){
|
||||
switch(c){
|
||||
// skip blank characters
|
||||
case ' ': case '\t': case '\r': case '\n':
|
||||
break;
|
||||
// try read comment
|
||||
case '/':
|
||||
cmp->state = CompilerState_ReadingComment;
|
||||
break;
|
||||
// try read label
|
||||
case '.':
|
||||
cmp->state = CompilerState_ReadingLabel;
|
||||
break;
|
||||
default:
|
||||
// try read instruction
|
||||
if(isAlphabeticalL(c) || isAlphabeticalR(c)){
|
||||
cmp->state = CompilerState_ReadingInstruction;
|
||||
break;
|
||||
}
|
||||
else setError(Error_UnexpectedCharacter(c));
|
||||
}
|
||||
}
|
||||
|
||||
static void _readCommentChar(Compiler* cmp, size_t pos, char c, Token* tok){
|
||||
// begin comment
|
||||
if(tok->type == TokenType_Unset){
|
||||
if(c == '*')
|
||||
tok->type = TokenType_MultiLineComment;
|
||||
else if(c == '/')
|
||||
tok->type = TokenType_SingleLineComment;
|
||||
else {
|
||||
setError(Error_UnexpectedCharacter(c));
|
||||
return;
|
||||
}
|
||||
tok->begin = pos;
|
||||
tok->length = 0;
|
||||
}
|
||||
// end multi-line comment
|
||||
else if(c == '/' && tok->type == TokenType_SingleLineComment) {
|
||||
if(cmp->code[pos - 1] == '*' && pos - 1 > tok->begin){
|
||||
tok->length = pos - tok->begin - 2;
|
||||
}
|
||||
}
|
||||
// end single-line comment
|
||||
else if(c == '\n' && tok->type == TokenType_SingleLineComment) {
|
||||
tok->length = pos - tok->begin - 1;
|
||||
// remove trailing '\r'
|
||||
if(cmp->code[pos - 1] == '\r')
|
||||
tok->length--;
|
||||
List_Token_push(&cmp->tokens, *tok);
|
||||
}
|
||||
// at the end of file
|
||||
else if(pos == cmp->code_len - 1){
|
||||
// end single-line comment
|
||||
if(tok->type == TokenType_SingleLineComment){
|
||||
tok->length = pos - tok->begin;
|
||||
}
|
||||
// error on unclosed multiline comment
|
||||
else setError(Error_endOfFile());
|
||||
}
|
||||
// do nothing on comment content
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool _Compiler_lex(Compiler* cmp){
|
||||
cmp->line = 1;
|
||||
cmp->column = 1;
|
||||
Token tok = Token_construct(TokenType_Unset, 0, 0);
|
||||
for(size_t pos = 0; pos < cmp->code_len; pos++){
|
||||
char c = cmp->code[pos];
|
||||
|
||||
switch(cmp->state){
|
||||
case CompilerState_Error:
|
||||
return false;
|
||||
case CompilerState_LexemaRecognition:
|
||||
_recognizeLexema(cmp, pos, c);
|
||||
break;
|
||||
case CompilerState_ReadingComment:
|
||||
_readCommentChar(cmp, pos, c, &tok);
|
||||
break;
|
||||
default:
|
||||
returnError("Unexpected compiler state %i", cmp->state);
|
||||
}
|
||||
|
||||
if(c == '\n'){
|
||||
cmp->column = 0;
|
||||
cmp->line++;
|
||||
}
|
||||
cmp->column++;
|
||||
}
|
||||
|
||||
return cmp->state != CompilerState_Error;
|
||||
}
|
||||
|
||||
bool _Compiler_parse(Compiler* cmp){
|
||||
return true;
|
||||
}
|
||||
bool _Compiler_compile(Compiler* cmp){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Compiler_compileTasm(Compiler* cmp, const char* restrict code, size_t code_len, char* restrict out_buffer, size_t out_len){
|
||||
returnErrorIf_auto(code == NULL);
|
||||
returnErrorIf_auto(code_len == 0);
|
||||
returnErrorIf_auto(out_buffer == NULL);
|
||||
returnErrorIf_auto(out_len == 0);
|
||||
cmp->code = code;
|
||||
cmp->code_len = code_len;
|
||||
cmp->out_buffer = out_buffer;
|
||||
cmp->out_len = out_len;
|
||||
|
||||
if(!_Compiler_lex(cmp))
|
||||
return false;
|
||||
if(!_Compiler_parse(cmp))
|
||||
return false;
|
||||
if(!_Compiler_compile(cmp))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
41
src/compiler/compiler.h
Normal file
41
src/compiler/compiler.h
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
#include "../collections/List.h"
|
||||
#include "token.h"
|
||||
|
||||
List_declare(Token);
|
||||
|
||||
typedef enum CompilerState {
|
||||
CompilerState_Initial,
|
||||
CompilerState_LexemaRecognition,
|
||||
CompilerState_ReadingComment,
|
||||
CompilerState_ReadingLabel,
|
||||
CompilerState_ReadingInstruction,
|
||||
CompilerState_ReadingArguments,
|
||||
CompilerState_ReadingData,
|
||||
CompilerState_Error,
|
||||
CompilerState_Success
|
||||
} CompilerState;
|
||||
|
||||
typedef struct Compiler {
|
||||
const char* code;
|
||||
size_t code_len;
|
||||
char* out_buffer;
|
||||
size_t out_len;
|
||||
|
||||
u32 line; // > 0 if code parsing started
|
||||
u32 column; // > 0 if code parsing started
|
||||
NULLABLE(char* error_message);
|
||||
CompilerState state;
|
||||
List_Token tokens;
|
||||
} Compiler;
|
||||
|
||||
void Compiler_init(Compiler* cmp);
|
||||
void Compiler_free(Compiler* cmp);
|
||||
|
||||
/// @brief compile assembly language code to machine code
|
||||
/// @return true if no errors, false if any error occured (check cmp->error_message)
|
||||
bool Compiler_compileTasm(Compiler* cmp, const char* restrict code, size_t code_len, char* restrict out_buffer, size_t out_len);
|
||||
|
||||
#define Compiler_setError(cmp, format, ...) _Compiler_setError(cmp, __func__, format ,##__VA_ARGS__)
|
||||
void _Compiler_setError(Compiler* cmp, const char* context, const char* format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||
21
src/compiler/token.h
Normal file
21
src/compiler/token.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
typedef enum TokenType {
|
||||
TokenType_Unset,
|
||||
TokenType_SingleLineComment,
|
||||
TokenType_MultiLineComment,
|
||||
TokenType_Label,
|
||||
TokenType_Instruction,
|
||||
TokenType_Argument,
|
||||
TokenType_Data,
|
||||
/* there is a place for 2 values left (TokenType must occupy 4 bits) */
|
||||
} TokenType;
|
||||
|
||||
typedef struct Token {
|
||||
u32 begin; // some index in Compiler->code
|
||||
u32 length : 28; // length in characters (28 bits)
|
||||
TokenType type : 4; // type of token (4 bits)
|
||||
} Token;
|
||||
|
||||
#define Token_construct(TYPE, BEGIN, END) ((Token){ .type = TYPE, .begin = BEGIN, .length = END })
|
||||
52
src/cstr.c
Normal file
52
src/cstr.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include "std.h"
|
||||
|
||||
char* _strcat_malloc(size_t n, cstr str0, ...){
|
||||
va_list argv;
|
||||
va_start(argv, str0);
|
||||
char* heap_ptr = _vstrcat_malloc(n, str0, argv);
|
||||
va_end(argv);
|
||||
return heap_ptr;
|
||||
}
|
||||
|
||||
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv){
|
||||
size_t str0_len = strlen(str0);
|
||||
size_t total_len = str0_len;
|
||||
cstr* const parts = malloc(sizeof(cstr) * n);
|
||||
size_t* const part_lengths = malloc(sizeof(size_t) * n);
|
||||
for(size_t i = 0; i < n; i++){
|
||||
cstr part = va_arg(argv, cstr);
|
||||
size_t length = strlen(part);
|
||||
parts[i] = part;
|
||||
part_lengths[i] = length;
|
||||
total_len += length;
|
||||
}
|
||||
char* const buf = malloc(total_len + 1);
|
||||
memcpy(buf, str0, str0_len);
|
||||
char* walking_ptr = buf + str0_len;
|
||||
for(size_t i = 0; i < n; i++){
|
||||
memcpy(walking_ptr, parts[i], part_lengths[i]);
|
||||
walking_ptr += part_lengths[i];
|
||||
}
|
||||
buf[total_len] = '\0';
|
||||
free(parts);
|
||||
free(part_lengths);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
||||
va_list argv;
|
||||
va_start(argv, format);
|
||||
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
|
||||
va_end(argv);
|
||||
return heap_ptr;
|
||||
}
|
||||
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||
char* buf = malloc(buffer_size);
|
||||
int r = vsprintf_s(buf, buffer_size, format, argv);
|
||||
if(r < 0){
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
/// EXIT
|
||||
/// ax - exit code
|
||||
i32 EXIT_impl(VM* vm, size_t pos){
|
||||
i32 EXIT_impl(VM* vm){
|
||||
vm->state = VMState_Exited;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// MOV [dst_register] [src_register]
|
||||
i32 MOV_impl(VM* vm, size_t pos){
|
||||
i32 MOV_impl(VM* vm){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 src_register_i = 0;
|
||||
readRegisterVar(src_register_i);
|
||||
if(dst_register_i == src_register_i){
|
||||
VM_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, src_register_i);
|
||||
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// NOP
|
||||
i32 NOP_impl(VM* vm, size_t pos){
|
||||
i32 NOP_impl(VM* vm){
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,15 +1,16 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// PUSH [dst_register] [value_size] [value]
|
||||
i32 PUSH_impl(VM* vm, size_t pos){
|
||||
i32 PUSH_impl(VM* vm){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 value_size = 0;
|
||||
readValueSizeVar(value_size);
|
||||
|
||||
vm->registers[dst_register_i].u32v = 0;
|
||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, pos, value_size))
|
||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
||||
return -1;
|
||||
vm->current_pos += value_size;
|
||||
|
||||
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
||||
}
|
||||
|
||||
@ -1,41 +1,66 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
FILE* fileFromN(VM* vm, size_t pos, u32 file_n){
|
||||
FILE* NULLABLE(fileFromN)(VM* vm, u32 file_n){
|
||||
FILE* f = NULL;
|
||||
switch(file_n){
|
||||
case 0: f = stdin; break;
|
||||
case 1: f = stdout; break;
|
||||
case 2: f = stderr; break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n);
|
||||
VM_setError(vm, "invalid file_n (%x) ", file_n);
|
||||
break;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
// sys_read
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
i32 SYS_read(VM* vm){
|
||||
const u32 file_n = vm->bx.u32v;
|
||||
u8* const buf = vm->data + vm->cx.u32v;
|
||||
const u32 size = vm->dx.u32v;
|
||||
|
||||
if(buf + size > vm->data + vm->data_size)
|
||||
return 40;
|
||||
|
||||
FILE* f = fileFromN(vm, file_n);
|
||||
return fread(buf, 1, size, f);
|
||||
}
|
||||
|
||||
// sys_write
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
i32 SYS_write(VM* vm){
|
||||
const u32 file_n = vm->bx.u32v;
|
||||
u8* const buf = vm->data + vm->cx.u32v;
|
||||
const u32 size = vm->dx.u32v;
|
||||
|
||||
if(buf + size > vm->data + vm->data_size)
|
||||
return 41;
|
||||
|
||||
FILE* f = fileFromN(vm, file_n);
|
||||
return fwrite(buf, 1, size, f);
|
||||
}
|
||||
|
||||
/// SYS
|
||||
/// ax - func code
|
||||
i32 SYS_impl(VM* vm, size_t pos){
|
||||
/// before call: ax - func code
|
||||
/// after call: ax - result code
|
||||
i32 SYS_impl(VM* vm){
|
||||
u8 func_code = vm->ax.u8v0;
|
||||
size_t result_code = 0;
|
||||
u32 result_code = 0;
|
||||
switch(func_code){
|
||||
// sys_read
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 0:;
|
||||
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
case 0:
|
||||
result_code = SYS_read(vm);
|
||||
break;
|
||||
// sys_write
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 1:;
|
||||
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
result_code = SYS_write(vm);
|
||||
break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code);
|
||||
VM_setError(vm, "invalid system call (%x) ", func_code);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@ -2,14 +2,14 @@
|
||||
#include "../instructions.h"
|
||||
|
||||
#define readVar(VAR) {\
|
||||
if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \
|
||||
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
||||
return -1;\
|
||||
pos += sizeof(VAR);\
|
||||
vm->current_pos += sizeof(VAR);\
|
||||
}
|
||||
|
||||
#define validateRegisterIndex(VAR) {\
|
||||
if(VAR > sizeof(vm->registers)){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\
|
||||
VM_setError(vm, "invalid register index (%x)", VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
@ -21,7 +21,7 @@
|
||||
|
||||
#define validateValueSize(VAR) {\
|
||||
if(VAR < 1 || VAR > 4){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\
|
||||
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
|
||||
@ -22,26 +22,26 @@
|
||||
}
|
||||
|
||||
/// ADD [dst_register] [src_register]
|
||||
i32 ADD_impl(VM* vm, size_t pos){
|
||||
i32 ADD_impl(VM* vm){
|
||||
mathOperatorImpl(+);
|
||||
}
|
||||
|
||||
/// SUB [dst_register] [src_register]
|
||||
i32 SUB_impl(VM* vm, size_t pos){
|
||||
i32 SUB_impl(VM* vm){
|
||||
mathOperatorImpl(-);
|
||||
}
|
||||
|
||||
/// MUL [dst_register] [src_register]
|
||||
i32 MUL_impl(VM* vm, size_t pos){
|
||||
i32 MUL_impl(VM* vm){
|
||||
mathOperatorImpl(*)
|
||||
}
|
||||
|
||||
/// DIV [dst_register] [src_register]
|
||||
i32 DIV_impl(VM* vm, size_t pos){
|
||||
i32 DIV_impl(VM* vm){
|
||||
mathOperatorImpl(/)
|
||||
}
|
||||
|
||||
/// MOD [dst_register] [src_register]
|
||||
i32 MOD_impl(VM* vm, size_t pos){
|
||||
i32 MOD_impl(VM* vm){
|
||||
mathOperatorImpl(%)
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ const Instruction instructions[] = {
|
||||
};
|
||||
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
||||
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode){
|
||||
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode){
|
||||
if(opcode >= instructions_count)
|
||||
return NULL;
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
///@param program_pos position in vm->program next afrer opcode
|
||||
///@returns number of bytes read
|
||||
typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos);
|
||||
typedef i32 (*InstructionImplFunc_t)(VM* vm);
|
||||
|
||||
typedef struct Instruction {
|
||||
const char* name;
|
||||
@ -18,17 +18,17 @@ typedef struct Instruction {
|
||||
/// @brief get instruction info from table
|
||||
/// @param opcode any byte
|
||||
/// @return ptr to struct or NULL
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode);
|
||||
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode);
|
||||
|
||||
i32 NOP_impl(VM* vm, size_t pos);
|
||||
i32 PUSH_impl(VM* vm, size_t pos);
|
||||
i32 MOV_impl(VM* vm, size_t pos);
|
||||
i32 ADD_impl(VM* vm, size_t pos);
|
||||
i32 SUB_impl(VM* vm, size_t pos);
|
||||
i32 MUL_impl(VM* vm, size_t pos);
|
||||
i32 DIV_impl(VM* vm, size_t pos);
|
||||
i32 MOD_impl(VM* vm, size_t pos);
|
||||
i32 SYS_impl(VM* vm, size_t pos);
|
||||
i32 EXIT_impl(VM* vm, size_t pos);
|
||||
i32 JMP_impl(VM* vm, size_t pos);
|
||||
i32 CALL_impl(VM* vm, size_t pos);
|
||||
i32 NOP_impl(VM* vm);
|
||||
i32 PUSH_impl(VM* vm);
|
||||
i32 MOV_impl(VM* vm);
|
||||
i32 ADD_impl(VM* vm);
|
||||
i32 SUB_impl(VM* vm);
|
||||
i32 MUL_impl(VM* vm);
|
||||
i32 DIV_impl(VM* vm);
|
||||
i32 MOD_impl(VM* vm);
|
||||
i32 SYS_impl(VM* vm);
|
||||
i32 EXIT_impl(VM* vm);
|
||||
i32 JMP_impl(VM* vm);
|
||||
i32 CALL_impl(VM* vm);
|
||||
|
||||
104
src/main.c
104
src/main.c
@ -1,17 +1,34 @@
|
||||
#include "VM/VM.h"
|
||||
#include "instructions/instructions.h"
|
||||
#include "collections/List.h"
|
||||
|
||||
List_declare(cstr);
|
||||
List_define(cstr);
|
||||
|
||||
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
||||
|
||||
i32 main(const i32 argc, const char** argv){
|
||||
const char* filename = NULL;
|
||||
i32 compileSources(cstr out_file, List_cstr* sources);
|
||||
i32 bootFromImage(cstr image_file);
|
||||
|
||||
i32 main(const i32 argc, cstr* argv){
|
||||
if(argc < 2){
|
||||
printfe("ERROR: no arguments provided. Use --help to know more.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool boot = false;
|
||||
cstr NULLABLE(image_file) = NULL;
|
||||
bool compile = false;
|
||||
cstr NULLABLE(out_file) = NULL;
|
||||
List_cstr NULLABLE(sources) = List_cstr_construct(NULL, 0, 0);
|
||||
|
||||
for(i32 argi = 1; argi < argc; argi++){
|
||||
if(arg_is("-h") || arg_is("--help")){
|
||||
printf(
|
||||
"-h, --help Show this message\n"
|
||||
"-op, --opcodes Shows list of all instructions.\n"
|
||||
"-i, --image [FILE] Boot VM using image file\n"
|
||||
"-h, --help Show this message.\n"
|
||||
"-op, --opcodes Show list of all instructions.\n"
|
||||
"-i, --image [FILE] Boot VM using image file.\n"
|
||||
"-c, --compile [OUT_FILE] [SOURCES...] Compile assembly source files to machine code.\n"
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
@ -19,44 +36,72 @@ i32 main(const i32 argc, const char** argv){
|
||||
for(u8 opcode = 0; opcode < 255; opcode++){
|
||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
||||
if(instr != NULL){
|
||||
printf("%02x %s\n", opcode, instr->name);
|
||||
printf("%02X %s\n", opcode, instr->name);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if(arg_is("-i") || arg_is("--image")){
|
||||
if(++argi >= argc){
|
||||
printfe("ERROR: no image file specified");
|
||||
if(boot){
|
||||
printfe("--image flag is set already\n");
|
||||
return 1;
|
||||
}
|
||||
filename = argv[argi];
|
||||
if(++argi >= argc){
|
||||
printfe("ERROR: no image file specified\n");
|
||||
return 1;
|
||||
}
|
||||
image_file = argv[argi];
|
||||
boot = true;
|
||||
}
|
||||
else if(arg_is("-c") || arg_is("--compile")){
|
||||
if(compile){
|
||||
printfe("--compile flag is set already\n");
|
||||
return 1;
|
||||
}
|
||||
if(++argi >= argc){
|
||||
printfe("ERROR: no output file file specified\n");
|
||||
return 1;
|
||||
}
|
||||
out_file = argv[argi];
|
||||
sources = List_cstr_alloc(argc);
|
||||
compile = true;
|
||||
}
|
||||
else if(compile){
|
||||
List_cstr_push(&sources, argv[argi]);
|
||||
}
|
||||
else {
|
||||
printfe("ERROR: unknown argument '%s'", argv[argi]);
|
||||
printfe("ERROR: unknown argument '%s'\n", argv[argi]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(filename == NULL){
|
||||
printfe("ERROR: no arguments provided. Use --help to know more.");
|
||||
return 1;
|
||||
i32 exit_code = 0;
|
||||
if(compile){
|
||||
exit_code = compileSources(out_file, &sources);
|
||||
}
|
||||
if(exit_code == 0 && boot){
|
||||
exit_code = bootFromImage(image_file);
|
||||
}
|
||||
|
||||
FILE* file = fopen(filename, "rb");
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
i32 bootFromImage(cstr image_file){
|
||||
FILE* file = fopen(image_file, "rb");
|
||||
if(file == NULL){
|
||||
printfe("ERROR: can't open file '%s'", filename);
|
||||
printfe("ERROR: can't open file '%s'\n", image_file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const size_t buffer_size = 1024*1024;
|
||||
u8* buffer = malloc(buffer_size);
|
||||
memset(buffer, 0, buffer_size);
|
||||
u8* vm_memory = malloc(buffer_size);
|
||||
memset(vm_memory, 0, buffer_size);
|
||||
|
||||
size_t bytes_read = fread(buffer, 1, buffer_size, file);
|
||||
size_t bytes_read = fread(vm_memory, 1, buffer_size, file);
|
||||
fclose(file);
|
||||
if(bytes_read == (size_t)EOF){
|
||||
printfe("ERROR: can't read file '%s'", filename);
|
||||
free(buffer);
|
||||
printfe("ERROR: can't read file '%s'\n", image_file);
|
||||
free(vm_memory);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -64,14 +109,25 @@ i32 main(const i32 argc, const char** argv){
|
||||
VM_init(&vm);
|
||||
|
||||
i32 exit_code = 1;
|
||||
if(VM_loadProgram(&vm, buffer, bytes_read)){
|
||||
if(VM_loadProgram(&vm, vm_memory, bytes_read)){
|
||||
exit_code = VM_executeProgram(&vm);
|
||||
}
|
||||
if(vm.error_message != NULL){
|
||||
printfe("VM ERROR: %s", vm.error_message);
|
||||
if(vm.state == VMState_InternalError){
|
||||
if(vm.error_message){
|
||||
printfe("VM ERROR: %s\n", vm.error_message);
|
||||
free(vm.error_message);
|
||||
}
|
||||
else printfe("VM ERROR: unknown error (error_message is null)\n");
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
if(exit_code != 0){
|
||||
printfe("program exited with code %i\n", exit_code);
|
||||
}
|
||||
|
||||
free(vm_memory);
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
i32 compileSources(cstr out_file, List_cstr* sources){
|
||||
return -1;
|
||||
}
|
||||
|
||||
30
src/std.h
30
src/std.h
@ -24,4 +24,34 @@ typedef u8 bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
typedef const char* cstr;
|
||||
|
||||
#define __count_args( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7 , a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, a24,a25,a26,a27,a28,a29,a30,a31, \
|
||||
a32,a33,a34,a35,a36,a37,a38,a39, a40,a41,a42,a43,a44,a45,a46,a47, \
|
||||
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||
a64,...) a64
|
||||
// Macro for counting variadic arguments (max 64)
|
||||
// (see usage in kprint.h)
|
||||
#define count_args(ARGS...) __count_args(ARGS, \
|
||||
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
|
||||
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
|
||||
32,31,30,29,28,27,26,25, 24,23,22,21,20,19,18,17, \
|
||||
16,15,14,13,12,11,10,9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||
|
||||
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||
|
||||
/// @warning pointer can be null
|
||||
#define NULLABLE(NAME) NAME
|
||||
|
||||
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
|
||||
char* _strcat_malloc(size_t n, const char* str0, ...);
|
||||
char* _vstrcat_malloc(size_t n, const char* str0, va_list argv);
|
||||
|
||||
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...) __attribute__((__format__(__printf__, 2, 3)));
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv);
|
||||
|
||||
static inline bool isAlphabeticalL(char c) { return c - 'a' <= 'z'; }
|
||||
static inline bool isAlphabeticalR(char c) { return c - 'A' <= 'Z'; }
|
||||
static inline bool isDigit(char c) { return c - '0' <= '9'; }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user