Adds "external" script file instead of inline code.
This commit is contained in:
parent
23e33261c0
commit
8959a7c35d
8 changed files with 96 additions and 119 deletions
11
Depfile
11
Depfile
|
@ -3,6 +3,7 @@ Artifact=kernel
|
|||
TempDir=obj
|
||||
SourceDir=asm src
|
||||
Files=trainscript/trainscript.y trainscript/trainscript.l trainscript/tsvm.cpp
|
||||
AdditionalObjects=obj/main.o
|
||||
|
||||
LexUseCpp
|
||||
YaccUseCpp
|
||||
|
@ -23,6 +24,16 @@ CXXFLAGS=-g -std=c++11 -Wall -g -fno-stack-protector -fno-use-cxa-atexit -nostdl
|
|||
LDFLAGS=-g -m32 -nostdlib -fno-builtin -Tkernel.ld
|
||||
|
||||
--
|
||||
obj/main.o: scripts/main.ts
|
||||
objcopy -B i386 -I binary -O elf32-i386 \
|
||||
scripts/main.ts obj/main.o
|
||||
objcopy \
|
||||
--redefine-sym _binary_scripts_main_ts_start=mainscript_start \
|
||||
--redefine-sym _binary_scripts_main_ts_end=mainscript_end \
|
||||
--redefine-sym _binary_scripts_main_ts_size=mainscript_size \
|
||||
obj/main.o
|
||||
|
||||
|
||||
.PHONY: run
|
||||
run:
|
||||
qemu-system-i386 -kernel kernel kernel
|
||||
|
|
18
Makefile
18
Makefile
|
@ -13,7 +13,7 @@ YACC = bison
|
|||
SRCS_AS = asm/intr_common_handler.S asm/multiboot.S asm/start.S
|
||||
SRCS_CC = src/console.c src/init.c src/interrupts.c src/malloc.c src/pmm.c src/stdlib.c src/timer.c src/vmm.c
|
||||
SRCS_CXX = trainscript/tsvm.cpp src/cplusplus.cpp src/vm.cpp obj/trainscript.yy.cpp obj/trainscript.tab.cpp
|
||||
OBJS = obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o
|
||||
OBJS = obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o obj/main.o
|
||||
|
||||
# Flags
|
||||
FLAGS = -m32 -Dnullptr=0
|
||||
|
@ -27,10 +27,10 @@ all: kernel
|
|||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(RM) obj/trainscript.yy.cpp obj/trainscript.tab.cpp obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o
|
||||
$(RM) obj/trainscript.yy.cpp obj/trainscript.tab.cpp obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o obj/main.o
|
||||
|
||||
kernel: obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o
|
||||
$(LD) $(FLAGS) $(LDFLAGS) -o $@ obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o
|
||||
kernel: obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o obj/main.o
|
||||
$(LD) $(FLAGS) $(LDFLAGS) -o $@ obj/tsvm.o obj/intr_common_handler.o obj/multiboot.o obj/start.o obj/console.o obj/init.o obj/interrupts.o obj/malloc.o obj/pmm.o obj/stdlib.o obj/timer.o obj/vmm.o obj/cplusplus.o obj/vm.o obj/trainscript.yy.o obj/trainscript.tab.o obj/main.o
|
||||
|
||||
# src/console.c
|
||||
obj/console.o: src/console.c include/console.h include/stdlib.h \
|
||||
|
@ -129,6 +129,16 @@ obj/trainscript.tab.cpp: trainscript/trainscript.y
|
|||
$(YACC) -o obj/trainscript.tab.cpp -d trainscript/trainscript.y
|
||||
|
||||
# Custom Targets
|
||||
obj/main.o: scripts/main.ts
|
||||
objcopy -B i386 -I binary -O elf32-i386 \
|
||||
scripts/main.ts obj/main.o
|
||||
objcopy \
|
||||
--redefine-sym _binary_scripts_main_ts_start=mainscript_start \
|
||||
--redefine-sym _binary_scripts_main_ts_end=mainscript_end \
|
||||
--redefine-sym _binary_scripts_main_ts_size=mainscript_size \
|
||||
obj/main.o
|
||||
|
||||
|
||||
.PHONY: run
|
||||
run:
|
||||
qemu-system-i386 -kernel kernel kernel
|
||||
|
|
57
scripts/main.ts
Normal file
57
scripts/main.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
VAR global : INT;
|
||||
|
||||
PUB main() | i : INT
|
||||
BEGIN
|
||||
afraid(15, 34) → i;
|
||||
0 -> i;
|
||||
WHILE i < 5 DO
|
||||
BEGIN
|
||||
print(i);
|
||||
i + 1 -> i;
|
||||
END
|
||||
|
||||
print(factorial(4), fibonacci(8), problem1(10));
|
||||
|
||||
REPEAT
|
||||
BEGIN
|
||||
|
||||
END
|
||||
END
|
||||
|
||||
# Calculates factorial (number!)
|
||||
PRI factorial(number : INT) → result : INT
|
||||
BEGIN
|
||||
IF number > 1 THEN
|
||||
number * factorial(number - 1) → result;
|
||||
ELSE
|
||||
1 → result;
|
||||
END
|
||||
|
||||
# Recursive test
|
||||
PRI fibonacci(n : INT) → f : INT
|
||||
BEGIN
|
||||
IF n = 0 THEN
|
||||
0 → f;
|
||||
ELSEIF n = 1 THEN
|
||||
1 → f;
|
||||
ELSE
|
||||
fibonacci(n - 1) + fibonacci(n - 2) → f;
|
||||
END
|
||||
|
||||
# If we list all the natural numbers below 10 that are
|
||||
# multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of
|
||||
# these multiples is 23.
|
||||
# Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
PRI problem1(max : INT) → r : INT | iter : INT
|
||||
BEGIN
|
||||
1 → iter;
|
||||
0 → r;
|
||||
WHILE iter < max DO
|
||||
BEGIN
|
||||
IF (iter % 5) = 0 THEN
|
||||
r + iter → r;
|
||||
ELSEIF (iter % 3) = 0 THEN
|
||||
r + iter → r;
|
||||
iter + 1 → iter;
|
||||
END
|
||||
END
|
|
@ -201,10 +201,6 @@ void kprintf(const char *format, ...)
|
|||
i = va_arg(vl, int);
|
||||
kputs(itoa(i, buffer, 16));
|
||||
break;
|
||||
case 'f':
|
||||
f = va_arg(vl, double);
|
||||
kputs(ftoa(f, buffer, 3));
|
||||
break;
|
||||
case 'c':
|
||||
c = va_arg(vl, int);
|
||||
kputc(c);
|
||||
|
|
49
src/stdlib.c
49
src/stdlib.c
|
@ -97,55 +97,6 @@ double atod(const char *str)
|
|||
return 42.0;
|
||||
}
|
||||
|
||||
// Converts a given integer x to string str[]. d is the number
|
||||
// of digits required in output. If d is more than the number
|
||||
// of digits in x, then 0s are added at the beginning.
|
||||
static int intToStr(int x, char str[], int d)
|
||||
{
|
||||
int i = 0;
|
||||
while (x)
|
||||
{
|
||||
str[i++] = (x%10) + '0';
|
||||
x = x/10;
|
||||
}
|
||||
|
||||
// If number of digits required is more, then
|
||||
// add 0s at the beginning
|
||||
while (i < d)
|
||||
str[i++] = '0';
|
||||
|
||||
reverse(str, i);
|
||||
str[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
// Converts a floating point number to string.
|
||||
void ftoa(float n, char *res, int afterpoint)
|
||||
{
|
||||
// Extract integer part
|
||||
int ipart = (int)n;
|
||||
|
||||
// Extract floating part
|
||||
float fpart = n - (float)ipart;
|
||||
|
||||
// convert integer part to string
|
||||
int i = intToStr(ipart, res, 0);
|
||||
|
||||
// check for display option after point
|
||||
if (afterpoint != 0)
|
||||
{
|
||||
res[i] = '.'; // add dot
|
||||
|
||||
// Get the value of fraction part upto given no.
|
||||
// of points after dot. The third parameter is needed
|
||||
// to handle cases like 233.007
|
||||
//fpart = fpart * pow(10, afterpoint);
|
||||
|
||||
intToStr((int)fpart, res + i + 1, afterpoint);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *memmove( void *destination, const void *source, size_t num)
|
||||
{
|
||||
// TODO: Implement memmove
|
||||
|
|
35
src/vm.cpp
35
src/vm.cpp
|
@ -5,30 +5,11 @@ using namespace ker;
|
|||
using namespace trainscript;
|
||||
|
||||
extern "C" {
|
||||
// extern const char file01_start;
|
||||
// extern const char file01_end;
|
||||
// extern size_t file01_size;
|
||||
extern const char mainscript_start;
|
||||
extern const char mainscript_end;
|
||||
extern const char mainscript_size;
|
||||
}
|
||||
|
||||
char file01[] = R"code(
|
||||
VAR global : INT;
|
||||
PUB main() | i : INT
|
||||
BEGIN
|
||||
print(10, 20, 30);
|
||||
afraid(15, 34) → i;
|
||||
print(40, i, 60);
|
||||
0 -> i;
|
||||
WHILE i < 5 DO
|
||||
BEGIN
|
||||
print(i);
|
||||
i + 1 -> i;
|
||||
END
|
||||
REPEAT
|
||||
BEGIN
|
||||
|
||||
END
|
||||
END)code";
|
||||
|
||||
void cpp_test();
|
||||
|
||||
class PrintMethod :
|
||||
|
@ -155,10 +136,18 @@ extern "C" uint32_t __attribute__((cdecl)) cCodeFunction(int a, int b)
|
|||
|
||||
extern "C" void vm_start()
|
||||
{
|
||||
struct {
|
||||
const char *ptr;
|
||||
uint32_t size;
|
||||
} mainfile {
|
||||
&mainscript_start,
|
||||
(uint32_t)&mainscript_size
|
||||
};
|
||||
|
||||
// cpp_test();
|
||||
|
||||
kprintf("Parse kernel module: ");
|
||||
Module *module = VM::load(file01, sizeof(file01) - 1);
|
||||
Module *module = VM::load(mainfile.ptr, mainfile.size);
|
||||
if(module == nullptr) {
|
||||
kprintf("Could not load module :(\n");
|
||||
return;
|
||||
|
|
|
@ -53,7 +53,8 @@ DISTFILES += \
|
|||
trainscript/file02.ts \
|
||||
kernel.ld \
|
||||
Depfile \
|
||||
Makefile.new
|
||||
Makefile.new \
|
||||
scripts/main.ts
|
||||
|
||||
QMAKE_INCDIR =
|
||||
|
||||
|
|
|
@ -7,41 +7,3 @@ BEGIN
|
|||
# fibonacci(7) → res;
|
||||
problem1(1000) → res;
|
||||
END
|
||||
|
||||
PUB factorial(number : INT) → result : INT
|
||||
BEGIN
|
||||
IF number > 1 THEN
|
||||
number * factorial(number - 1) → result;
|
||||
ELSE
|
||||
1 → result;
|
||||
END
|
||||
|
||||
# Recursive test
|
||||
PUB fibonacci(n : INT) → f : INT
|
||||
BEGIN
|
||||
IF n = 0 THEN
|
||||
0 → f;
|
||||
ELSEIF n = 1 THEN
|
||||
1 → f;
|
||||
ELSE
|
||||
fibonacci(n - 1) + fibonacci(n - 2) → f;
|
||||
END
|
||||
|
||||
# If we list all the natural numbers below 10 that are
|
||||
# multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of
|
||||
# these multiples is 23.
|
||||
# Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
PUB problem1(max : INT) → r : INT | iter : INT
|
||||
BEGIN
|
||||
1 → iter;
|
||||
0 → r;
|
||||
WHILE iter < max DO
|
||||
# FOR iter FROM 1 TO max DO
|
||||
BEGIN
|
||||
IF (iter % 5) = 0 THEN
|
||||
r + iter → r;
|
||||
ELSEIF (iter % 3) = 0 THEN
|
||||
r + iter → r;
|
||||
iter + 1 → iter;
|
||||
END
|
||||
END
|
||||
|
|
Loading…
Reference in a new issue