⚡ optimize vm
This commit is contained in:
parent
d690c779b8
commit
527cb5277b
|
@ -25,6 +25,8 @@ set(NASAL_OBJECT_SOURCE_FILE
|
|||
${CMAKE_SOURCE_DIR}/src/bits_lib.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/io_lib.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/math_lib.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/dylib_lib.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/unix_lib.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/nasal_codegen.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/nasal_dbg.cpp
|
||||
${CMAKE_SOURCE_DIR}/src/nasal_err.cpp
|
||||
|
|
16
makefile
16
makefile
|
@ -21,6 +21,8 @@ NASAL_HEADER=\
|
|||
src/bits_lib.h\
|
||||
src/io_lib.h\
|
||||
src/math_lib.h\
|
||||
src/dylib_lib.h\
|
||||
src/unix_lib.h\
|
||||
src/coroutine.h
|
||||
|
||||
NASAL_OBJECT=\
|
||||
|
@ -42,6 +44,8 @@ NASAL_OBJECT=\
|
|||
build/fg_props.o\
|
||||
build/io_lib.o\
|
||||
build/math_lib.o\
|
||||
build/unix_lib.o\
|
||||
build/dylib_lib.o\
|
||||
build/coroutine.o\
|
||||
build/nasal_vm.o\
|
||||
build/nasal_dbg.o\
|
||||
|
@ -119,6 +123,18 @@ build/io_lib.o: \
|
|||
src/io_lib.h src/io_lib.cpp | build
|
||||
$(CXX) -std=$(STD) -c -O3 src/io_lib.cpp -fno-exceptions -fPIC -o build/io_lib.o -I .
|
||||
|
||||
build/dylib_lib.o: \
|
||||
src/nasal.h\
|
||||
src/nasal_gc.h\
|
||||
src/dylib_lib.h src/dylib_lib.cpp | build
|
||||
$(CXX) -std=$(STD) -c -O3 src/dylib_lib.cpp -fno-exceptions -fPIC -o build/dylib_lib.o -I .
|
||||
|
||||
build/unix_lib.o: \
|
||||
src/nasal.h\
|
||||
src/nasal_gc.h\
|
||||
src/unix_lib.h src/unix_lib.cpp | build
|
||||
$(CXX) -std=$(STD) -c -O3 src/unix_lib.cpp -fno-exceptions -fPIC -o build/unix_lib.o -I .
|
||||
|
||||
build/fg_props.o: \
|
||||
src/nasal.h\
|
||||
src/nasal_gc.h\
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
#include "dylib_lib.h"
|
||||
|
||||
nasal_builtin_table dylib_lib_native[] = {
|
||||
{nullptr, nullptr}
|
||||
};
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "nasal.h"
|
||||
#include "nasal_gc.h"
|
||||
#include "nasal_builtin.h"
|
||||
|
||||
extern nasal_builtin_table dylib_lib_native[];
|
|
@ -1,5 +1,14 @@
|
|||
#include "io_lib.h"
|
||||
|
||||
const auto file_type_name = "file";
|
||||
|
||||
void filehandle_destructor(void* ptr) {
|
||||
if ((FILE*)ptr==stdin) {
|
||||
return;
|
||||
}
|
||||
fclose((FILE*)ptr);
|
||||
}
|
||||
|
||||
var builtin_readfile(var* local, gc& ngc) {
|
||||
var val = local[1];
|
||||
if (val.type!=vm_str) {
|
||||
|
@ -48,13 +57,13 @@ var builtin_open(var* local, gc& ngc) {
|
|||
return nas_err("open", "failed to open file <"+name.str()+">");
|
||||
}
|
||||
var ret = ngc.alloc(vm_obj);
|
||||
ret.obj().set("file", filehandle_destructor, res);
|
||||
ret.obj().set(file_type_name, filehandle_destructor, res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
var builtin_close(var* local, gc& ngc) {
|
||||
var fd = local[1];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("close", "not a valid filehandle");
|
||||
}
|
||||
fd.obj().clear();
|
||||
|
@ -65,7 +74,7 @@ var builtin_read(var* local, gc& ngc) {
|
|||
var fd = local[1];
|
||||
var buf = local[2];
|
||||
var len = local[3];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("read", "not a valid filehandle");
|
||||
}
|
||||
if (buf.type!=vm_str || buf.val.gcobj->unmut) {
|
||||
|
@ -91,7 +100,7 @@ var builtin_read(var* local, gc& ngc) {
|
|||
var builtin_write(var* local, gc& ngc) {
|
||||
var fd = local[1];
|
||||
var str = local[2];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("write", "not a valid filehandle");
|
||||
}
|
||||
if (str.type!=vm_str) {
|
||||
|
@ -104,7 +113,7 @@ var builtin_seek(var* local, gc& ngc) {
|
|||
var fd = local[1];
|
||||
var pos = local[2];
|
||||
var whence = local[3];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("seek", "not a valid filehandle");
|
||||
}
|
||||
return var::num((f64)fseek((FILE*)fd.obj().ptr, pos.num(), whence.num()));
|
||||
|
@ -112,7 +121,7 @@ var builtin_seek(var* local, gc& ngc) {
|
|||
|
||||
var builtin_tell(var* local, gc& ngc) {
|
||||
var fd = local[1];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("tell", "not a valid filehandle");
|
||||
}
|
||||
return var::num((f64)ftell((FILE*)fd.obj().ptr));
|
||||
|
@ -120,7 +129,7 @@ var builtin_tell(var* local, gc& ngc) {
|
|||
|
||||
var builtin_readln(var* local, gc& ngc) {
|
||||
var fd = local[1];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("readln", "not a valid filehandle");
|
||||
}
|
||||
var str = ngc.alloc(vm_str);
|
||||
|
@ -168,7 +177,7 @@ var builtin_stat(var* local, gc& ngc) {
|
|||
|
||||
var builtin_eof(var* local, gc& ngc) {
|
||||
var fd = local[1];
|
||||
if (!fd.objchk("file")) {
|
||||
if (!fd.objchk(file_type_name)) {
|
||||
return nas_err("readln", "not a valid filehandle");
|
||||
}
|
||||
return var::num((f64)feof((FILE*)fd.obj().ptr));
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#define F_OK 0 // fuck msc
|
||||
#endif
|
||||
|
||||
void filehandle_destructor(void*);
|
||||
|
||||
var builtin_readfile(var*, gc&);
|
||||
var builtin_fout(var*, gc&);
|
||||
var builtin_exists(var*, gc&);
|
||||
|
|
|
@ -26,6 +26,8 @@ void codegen::init_native_function() {
|
|||
load_native_function_table(bits_native);
|
||||
load_native_function_table(coroutine_native);
|
||||
load_native_function_table(flight_gear_native);
|
||||
load_native_function_table(dylib_lib_native);
|
||||
load_native_function_table(unix_lib_native);
|
||||
}
|
||||
|
||||
void codegen::check_id_exist(identifier* node) {
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
#include "math_lib.h"
|
||||
#include "fg_props.h"
|
||||
#include "io_lib.h"
|
||||
#include "dylib_lib.h"
|
||||
#include "unix_lib.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <list>
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
#include "nasal_gc.h"
|
||||
|
||||
void filehandle_destructor(void* ptr) {
|
||||
if ((FILE*)ptr==stdin) {
|
||||
return;
|
||||
}
|
||||
fclose((FILE*)ptr);
|
||||
}
|
||||
|
||||
void dir_entry_destructor(void* ptr) {
|
||||
#ifndef _MSC_VER
|
||||
closedir((DIR*)ptr);
|
||||
|
|
|
@ -180,7 +180,6 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
void filehandle_destructor(void*);
|
||||
void dir_entry_destructor(void*);
|
||||
void dylib_destructor(void*);
|
||||
void func_addr_destructor(void*);
|
||||
|
|
|
@ -23,6 +23,7 @@ protected:
|
|||
const f64* cnum = nullptr; // constant numbers
|
||||
const std::string* cstr = nullptr; // constant symbols and strings
|
||||
std::vector<u32> imm; // immediate number table
|
||||
std::vector<nasal_builtin_table> native;
|
||||
|
||||
/* garbage collector */
|
||||
gc ngc;
|
||||
|
@ -33,7 +34,6 @@ protected:
|
|||
/* values used for debugger */
|
||||
const std::string* files = nullptr; // file name list
|
||||
const opcode* bytecode = nullptr; // bytecode buffer address
|
||||
std::vector<nasal_builtin_table> native;
|
||||
|
||||
/* vm initializing function */
|
||||
void init(
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
#include "unix_lib.h"
|
||||
|
||||
nasal_builtin_table unix_lib_native[] = {
|
||||
{nullptr, nullptr}
|
||||
};
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "nasal.h"
|
||||
#include "nasal_gc.h"
|
||||
#include "nasal_builtin.h"
|
||||
|
||||
extern nasal_builtin_table unix_lib_native[];
|
Loading…
Reference in New Issue