lama_byterun/byterun/include/utils.h

54 lines
1.9 KiB
C
Raw Normal View History

2024-10-31 21:08:48 +03:00
#pragma once
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "../../runtime/runtime.h"
#include "../../runtime/runtime_common.h"
2024-10-31 21:08:48 +03:00
/* The unpacked representation of bytecode file */
typedef struct {
char *string_ptr; /* A pointer to the beginning of the string table */
int *public_ptr; /* A pointer to the beginning of publics table */
char *code_ptr; /* A pointer to the bytecode itself */
int *global_ptr; /* A pointer to the global area */
2024-11-12 21:08:41 +03:00
int code_size; /* The size (in bytes) of code */
2024-10-31 21:08:48 +03:00
int stringtab_size; /* The size (in bytes) of the string table */
int global_area_size; /* The size (in words) of global area */
int public_symbols_number; /* The number of public symbols */
char buffer[0];
} bytefile;
2024-11-14 00:50:43 +03:00
static inline void exec_failure(const char *cmd, int line, aint offset,
const char *msg) {
failure("*** RUNTIME ERROR: %i(0x%.8x):%s error: %s\n", line, offset, cmd,
msg);
}
2024-10-31 21:08:48 +03:00
/* Gets a string from a string table by an index */
2024-11-14 00:50:43 +03:00
static inline const char *get_string(const bytefile *f, size_t pos) {
if (pos >= f->stringtab_size) {
failure("strinpg pos is out of range: %zu >= %i\n", pos, f->stringtab_size);
}
return &f->string_ptr[pos];
}
2024-10-31 21:08:48 +03:00
/* Gets a name for a public symbol */
2024-11-14 00:50:43 +03:00
static inline const char *get_public_name(const bytefile *f, size_t i) {
if (i * 2 >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2,
f->public_symbols_number);
}
return get_string(f, f->public_ptr[i * 2]);
}
/* Gets an offset for a publie symbol */
2024-11-14 00:50:43 +03:00
static inline size_t get_public_offset(const bytefile *f, size_t i) {
if (i * 2 + 1 >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2 + 1,
f->public_symbols_number);
}
return f->public_ptr[i * 2 + 1];
}