Compare commits
17 Commits
38f06b1575
...
dscd
| Author | SHA1 | Date | |
|---|---|---|---|
| 240e93a90f | |||
| bc41dcda4a | |||
| f62030772a | |||
| 43663714ca | |||
| 38bfd5c7d9 | |||
|
|
f348174229 | ||
|
|
bc8a037a30 | ||
|
|
958c383f85 | ||
|
|
f3829559d4 | ||
|
|
9155216fd2 | ||
|
|
b6320ecba7 | ||
|
|
3bd8b9ac20 | ||
|
|
147415beef | ||
|
|
5fb42ff984 | ||
|
|
d7bb53687d | ||
|
|
ee27de7481 | ||
|
|
eaab6ab023 |
@@ -5,7 +5,8 @@ set(CMAKE_C_STANDARD 99)
|
||||
|
||||
add_compile_options(-g -Wall -pedantic)
|
||||
|
||||
add_executable(njvm njvm.c)
|
||||
add_executable(njvm njvm.c
|
||||
njvm.h)
|
||||
|
||||
# Include directories for njvm executable
|
||||
target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||
@@ -13,7 +14,9 @@ target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build
|
||||
# Add the library
|
||||
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
||||
support.c
|
||||
GC.c)
|
||||
GC.c
|
||||
heap.c
|
||||
heap.h)
|
||||
|
||||
# Include directories for the bigint library
|
||||
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||
|
||||
2
GC.c
2
GC.c
@@ -21,7 +21,7 @@ struct sda sda;
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
#define SIZE 64
|
||||
#define SIZE 1000;
|
||||
|
||||
//Register
|
||||
struct stack reg;
|
||||
|
||||
1
SDA.c
1
SDA.c
@@ -15,6 +15,7 @@ ObjRef getSDA(int i, struct sda s) {
|
||||
}
|
||||
|
||||
void setSDA(int point, ObjRef val, struct sda s) {
|
||||
if (val == NULL) perror("Value is null");
|
||||
s.sda[point] = val;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11
debugMenu.c
11
debugMenu.c
@@ -29,11 +29,14 @@ void inspect(struct stack s, int fp){
|
||||
void list(void){
|
||||
//todo
|
||||
}
|
||||
void breakpoint(void){
|
||||
//todo
|
||||
void breakpoint(int *bp){
|
||||
printf("BREAKPOINT: ");
|
||||
char input[20];
|
||||
fgets(input,20,stdin);
|
||||
*bp = atoi(input);
|
||||
}
|
||||
|
||||
void debugMenu(int fp, struct stack stack, int* debug, int point){
|
||||
void debugMenu(int fp, struct stack stack, int* debug, int point, int* bp){
|
||||
char input[20];
|
||||
while (true) {
|
||||
printf("DEBUG(%i): inspect, list, breakpoint, run, step, quit?",point);
|
||||
@@ -41,7 +44,7 @@ void debugMenu(int fp, struct stack stack, int* debug, int point){
|
||||
printf("%s",input);
|
||||
if(input[0] == 'i') {inspect(stack,fp);}
|
||||
if(input[0] == 'l') list();
|
||||
if(input[0] == 'b') breakpoint();
|
||||
if(input[0] == 'b') breakpoint(bp);
|
||||
if(input[0] == 's') break;
|
||||
if(input[0] == 'r') {*debug = 0; break;};
|
||||
if(input[0] == 'q') exit(0);
|
||||
|
||||
8301
factor.asm
Normal file
8301
factor.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
factor.bin
Normal file
BIN
factor.bin
Normal file
Binary file not shown.
33
heap.c
Normal file
33
heap.c
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by Elias Bennour on 28.01.24.
|
||||
//
|
||||
|
||||
#ifndef HEAP
|
||||
#define HEAP
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int maxHeapSize = 8192 * 1024;
|
||||
|
||||
void* my_malloc(size_t size) {
|
||||
static size_t total_allocated = 0;
|
||||
|
||||
if (total_allocated + size > maxHeapSize) {
|
||||
perror("Memory limit exceeded\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void* ptr = malloc(size);
|
||||
if (ptr != NULL) {
|
||||
total_allocated += size;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void initHeap(int size) {
|
||||
maxHeapSize = size;
|
||||
}
|
||||
|
||||
#endif //NINJA_NJVM_H
|
||||
12
heap.h
Normal file
12
heap.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by Elias Bennour on 28.01.24.
|
||||
//
|
||||
|
||||
#ifndef NINJA_HEAP_H
|
||||
#define NINJA_HEAP_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void* my_malloc(size_t size);
|
||||
|
||||
#endif //NINJA_HEAP_H
|
||||
778
matinv.asm
Normal file
778
matinv.asm
Normal file
@@ -0,0 +1,778 @@
|
||||
//
|
||||
// version
|
||||
//
|
||||
.vers 8
|
||||
|
||||
//
|
||||
// execution framework
|
||||
//
|
||||
__start:
|
||||
call _main
|
||||
call _exit
|
||||
__stop:
|
||||
jmp __stop
|
||||
|
||||
//
|
||||
// Integer readInteger()
|
||||
//
|
||||
_readInteger:
|
||||
asf 0
|
||||
rdint
|
||||
popr
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeInteger(Integer)
|
||||
//
|
||||
_writeInteger:
|
||||
asf 0
|
||||
pushl -3
|
||||
wrint
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Character readCharacter()
|
||||
//
|
||||
_readCharacter:
|
||||
asf 0
|
||||
rdchr
|
||||
popr
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeCharacter(Character)
|
||||
//
|
||||
_writeCharacter:
|
||||
asf 0
|
||||
pushl -3
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Integer char2int(Character)
|
||||
//
|
||||
_char2int:
|
||||
asf 0
|
||||
pushl -3
|
||||
popr
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Character int2char(Integer)
|
||||
//
|
||||
_int2char:
|
||||
asf 0
|
||||
pushl -3
|
||||
popr
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void exit()
|
||||
//
|
||||
_exit:
|
||||
asf 0
|
||||
halt
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeString(String)
|
||||
//
|
||||
_writeString:
|
||||
asf 1
|
||||
pushc 0
|
||||
popl 0
|
||||
jmp _writeString_L2
|
||||
_writeString_L1:
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
call _writeCharacter
|
||||
drop 1
|
||||
pushl 0
|
||||
pushc 1
|
||||
add
|
||||
popl 0
|
||||
_writeString_L2:
|
||||
pushl 0
|
||||
pushl -3
|
||||
getsz
|
||||
lt
|
||||
brt _writeString_L1
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Integer gcd(Integer, Integer)
|
||||
//
|
||||
_gcd:
|
||||
asf 1
|
||||
jmp __2
|
||||
__1:
|
||||
pushl -4
|
||||
pushl -3
|
||||
mod
|
||||
popl 0
|
||||
pushl -3
|
||||
popl -4
|
||||
pushl 0
|
||||
popl -3
|
||||
__2:
|
||||
pushl -3
|
||||
pushc 0
|
||||
ne
|
||||
brt __1
|
||||
__3:
|
||||
pushl -4
|
||||
popr
|
||||
jmp __0
|
||||
__0:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } newFraction(Integer, Integer)
|
||||
//
|
||||
_newFraction:
|
||||
asf 4
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
brf __5
|
||||
pushc 0
|
||||
pushl -4
|
||||
sub
|
||||
popl 0
|
||||
jmp __6
|
||||
__5:
|
||||
pushl -4
|
||||
popl 0
|
||||
__6:
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
brf __7
|
||||
pushc 0
|
||||
pushl -3
|
||||
sub
|
||||
popl 1
|
||||
jmp __8
|
||||
__7:
|
||||
pushl -3
|
||||
popl 1
|
||||
__8:
|
||||
pushl 0
|
||||
pushl 1
|
||||
call _gcd
|
||||
drop 2
|
||||
pushr
|
||||
popl 2
|
||||
new 2
|
||||
popl 3
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
ne
|
||||
brf __9
|
||||
pushl 3
|
||||
pushc 0
|
||||
pushl 0
|
||||
sub
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
jmp __10
|
||||
__9:
|
||||
pushl 3
|
||||
pushl 0
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
__10:
|
||||
pushl 3
|
||||
pushl 1
|
||||
pushl 2
|
||||
div
|
||||
putf 1
|
||||
pushl 3
|
||||
popr
|
||||
jmp __4
|
||||
__4:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_writeFraction:
|
||||
asf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
call _writeInteger
|
||||
drop 1
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 47
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl -3
|
||||
getf 1
|
||||
call _writeInteger
|
||||
drop 1
|
||||
__11:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } negFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_negFraction:
|
||||
asf 0
|
||||
pushc 0
|
||||
pushl -3
|
||||
getf 0
|
||||
sub
|
||||
pushl -3
|
||||
getf 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __12
|
||||
__12:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } addFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_addFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
add
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __13
|
||||
__13:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } subFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_subFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
sub
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __14
|
||||
__14:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } mulFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_mulFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __15
|
||||
__15:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } divFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_divFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __16
|
||||
__16:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] newMatrix(record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_newMatrix:
|
||||
asf 1
|
||||
pushc 2
|
||||
newa
|
||||
popl 0
|
||||
pushl 0
|
||||
pushc 0
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -6
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -5
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -4
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -3
|
||||
putfa
|
||||
pushl 0
|
||||
popr
|
||||
jmp __17
|
||||
__17:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeMatrix(Fraction[][])
|
||||
//
|
||||
_writeMatrix:
|
||||
asf 2
|
||||
pushc 0
|
||||
popl 0
|
||||
jmp __20
|
||||
__19:
|
||||
pushc 0
|
||||
popl 1
|
||||
jmp __23
|
||||
__22:
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
pushl 1
|
||||
getfa
|
||||
call _writeFraction
|
||||
drop 1
|
||||
pushc 2
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 32
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 1
|
||||
pushc 1
|
||||
add
|
||||
popl 1
|
||||
__23:
|
||||
pushl 1
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
getsz
|
||||
lt
|
||||
brt __22
|
||||
__24:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 0
|
||||
pushc 1
|
||||
add
|
||||
popl 0
|
||||
__20:
|
||||
pushl 0
|
||||
pushl -3
|
||||
getsz
|
||||
lt
|
||||
brt __19
|
||||
__21:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
__18:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] invertMatrix(Fraction[][])
|
||||
//
|
||||
_invertMatrix:
|
||||
asf 1
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _subFraction
|
||||
drop 2
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
getf 0
|
||||
pushc 0
|
||||
eq
|
||||
brf __26
|
||||
pushc 33
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 2
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 3
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 4
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 5
|
||||
pushc 58
|
||||
putfa
|
||||
dup
|
||||
pushc 6
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 7
|
||||
pushc 109
|
||||
putfa
|
||||
dup
|
||||
pushc 8
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 9
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 10
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 11
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 12
|
||||
pushc 120
|
||||
putfa
|
||||
dup
|
||||
pushc 13
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 14
|
||||
pushc 99
|
||||
putfa
|
||||
dup
|
||||
pushc 15
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 16
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 17
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 18
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 19
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 20
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 21
|
||||
pushc 98
|
||||
putfa
|
||||
dup
|
||||
pushc 22
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 23
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 24
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 25
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 26
|
||||
pushc 118
|
||||
putfa
|
||||
dup
|
||||
pushc 27
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 28
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 29
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 30
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 31
|
||||
pushc 100
|
||||
putfa
|
||||
dup
|
||||
pushc 32
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
call _exit
|
||||
__26:
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popr
|
||||
jmp __25
|
||||
__25:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void main()
|
||||
//
|
||||
_main:
|
||||
asf 3
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushc 7
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 4
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 6
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 5
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 0
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 1
|
||||
pushl 1
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 1
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 2
|
||||
pushl 2
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
__27:
|
||||
rsf
|
||||
ret
|
||||
BIN
matinv.bin
Normal file
BIN
matinv.bin
Normal file
Binary file not shown.
146
matinv.nj
Normal file
146
matinv.nj
Normal file
@@ -0,0 +1,146 @@
|
||||
//
|
||||
// matinv.nj -- invert 2x2 matrices of fractions
|
||||
//
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// greatest common divisor
|
||||
|
||||
Integer gcd(Integer a, Integer b) {
|
||||
local Integer h;
|
||||
while (b != 0) {
|
||||
h = a % b;
|
||||
a = b;
|
||||
b = h;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// fractions
|
||||
|
||||
type Fraction = record {
|
||||
Integer num;
|
||||
Integer den;
|
||||
};
|
||||
|
||||
Fraction newFraction(Integer num, Integer den) {
|
||||
local Integer n;
|
||||
local Integer d;
|
||||
local Integer g;
|
||||
local Fraction r;
|
||||
if (num < 0) {
|
||||
n = -num;
|
||||
} else {
|
||||
n = num;
|
||||
}
|
||||
if (den < 0) {
|
||||
d = -den;
|
||||
} else {
|
||||
d = den;
|
||||
}
|
||||
g = gcd(n, d);
|
||||
r = new(Fraction);
|
||||
if ((num < 0) != (den < 0)) {
|
||||
r.num = -n / g;
|
||||
} else {
|
||||
r.num = n / g;
|
||||
}
|
||||
r.den = d / g;
|
||||
return r;
|
||||
}
|
||||
|
||||
void writeFraction(Fraction f) {
|
||||
writeInteger(f.num);
|
||||
writeString("/");
|
||||
writeInteger(f.den);
|
||||
}
|
||||
|
||||
Fraction negFraction(Fraction f) {
|
||||
return newFraction(-f.num, f.den);
|
||||
}
|
||||
|
||||
Fraction addFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction subFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den - f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction mulFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.num, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction divFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den, f1.den * f2.num);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// 2x2 matrices of fractions
|
||||
|
||||
type Matrix = Fraction[][];
|
||||
|
||||
Matrix newMatrix(Fraction a00, Fraction a01,
|
||||
Fraction a10, Fraction a11) {
|
||||
local Matrix m;
|
||||
m = new(Fraction[2][]);
|
||||
m[0] = new(Fraction[2]);
|
||||
m[1] = new(Fraction[2]);
|
||||
m[0][0] = a00;
|
||||
m[0][1] = a01;
|
||||
m[1][0] = a10;
|
||||
m[1][1] = a11;
|
||||
return m;
|
||||
}
|
||||
|
||||
void writeMatrix(Matrix m) {
|
||||
local Integer i;
|
||||
local Integer j;
|
||||
i = 0;
|
||||
while (i < sizeof(m)) {
|
||||
j = 0;
|
||||
while (j < sizeof(m[i])) {
|
||||
writeFraction(m[i][j]);
|
||||
writeString(" ");
|
||||
j = j + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
i = i + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
Matrix invertMatrix(Matrix m) {
|
||||
local Fraction det;
|
||||
det = subFraction(mulFraction(m[0][0], m[1][1]),
|
||||
mulFraction(m[0][1], m[1][0]));
|
||||
if (det.num == 0) {
|
||||
writeString("error: matrix cannot be inverted\n");
|
||||
exit();
|
||||
}
|
||||
return newMatrix(
|
||||
divFraction(m[1][1], det), divFraction(negFraction(m[0][1]), det),
|
||||
divFraction(negFraction(m[1][0]), det), divFraction(m[0][0], det)
|
||||
);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void main() {
|
||||
local Matrix matrix;
|
||||
local Matrix result1;
|
||||
local Matrix result2;
|
||||
writeString("\n");
|
||||
matrix = newMatrix(
|
||||
newFraction(7, 1), newFraction(4, 1),
|
||||
newFraction(6, 1), newFraction(5, 1)
|
||||
);
|
||||
writeMatrix(matrix);
|
||||
result1 = invertMatrix(matrix);
|
||||
writeMatrix(result1);
|
||||
result2 = invertMatrix(result1);
|
||||
writeMatrix(result2);
|
||||
}
|
||||
11070
njlisp.asm
Normal file
11070
njlisp.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
njlisp.bin
Normal file
BIN
njlisp.bin
Normal file
Binary file not shown.
17
njvm.c
17
njvm.c
@@ -1,3 +1,6 @@
|
||||
#ifndef NJVM
|
||||
#define NJVM
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -9,6 +12,7 @@
|
||||
#include "bigint.h"
|
||||
#include "record.c"
|
||||
#include "GC.c"
|
||||
#include "heap.c"
|
||||
|
||||
// Debug
|
||||
int debug = 0;
|
||||
@@ -16,7 +20,6 @@ int debug = 0;
|
||||
// Program
|
||||
struct program program;
|
||||
|
||||
|
||||
unsigned fp;
|
||||
|
||||
void version(void) {
|
||||
@@ -28,6 +31,7 @@ void help(void) {
|
||||
}
|
||||
|
||||
void execute(struct program program) {
|
||||
int bp = -1;
|
||||
int i;
|
||||
char charInput;
|
||||
StackSlot tempSlot;
|
||||
@@ -35,7 +39,8 @@ void execute(struct program program) {
|
||||
ObjRef tempObj2;
|
||||
int tempInt;
|
||||
for (i = 0; i < *program.size; ++i) {
|
||||
// if (debug == 1) debugMenu(fp, stack, &debug, i);
|
||||
if (debug == 1 || bp == i) debugMenu(fp, stack, &debug, i, &bp);
|
||||
if(debug == 1) printf("(%i)",i);
|
||||
switch (program.program[i] >> 24) {
|
||||
case HALT:
|
||||
if (debug == 1) printf("halt\n");
|
||||
@@ -304,14 +309,12 @@ void tests(void) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
|
||||
// Initialize the Stack
|
||||
int size = SIZE;
|
||||
int current = 0;
|
||||
stack.size = &size;
|
||||
stack.current = ¤t;
|
||||
stack.stack = malloc(SIZE * 1024);
|
||||
stack.stack = malloc(size * 1024);
|
||||
if (stack.stack == NULL) {
|
||||
perror("malloc");
|
||||
}
|
||||
@@ -350,7 +353,7 @@ int main(int argc, char *argv[]) {
|
||||
// TODO: implement stack size
|
||||
} else if (strcmp(argv[i], "--heap") == 0) {
|
||||
i++;
|
||||
// TODO: implement heap size
|
||||
initHeap(atoi(argv[i]));
|
||||
} else if (strcmp(argv[i], "--gcpurge") == 0) {
|
||||
// TODO: implement gcpurge
|
||||
} else {
|
||||
@@ -377,3 +380,5 @@ int main(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* ifndef NJVM */
|
||||
24
record.c
24
record.c
@@ -9,7 +9,7 @@ ObjRef newRecord(int size){
|
||||
ObjRef record;
|
||||
unsigned int objSize;
|
||||
objSize = sizeof(*record) + (size * sizeof(void *));
|
||||
if((record = malloc(objSize)) == NULL){
|
||||
if((record = my_malloc(objSize)) == NULL){
|
||||
perror("malloc");
|
||||
}
|
||||
record->size = MSB;
|
||||
@@ -20,29 +20,35 @@ ObjRef newRecord(int size){
|
||||
return record;
|
||||
}
|
||||
int getSize(ObjRef arr){
|
||||
if(arr == NULL) return 0;
|
||||
return GET_ELEMENT_COUNT(arr);
|
||||
}
|
||||
ObjRef getField(ObjRef arr, int point){
|
||||
if(arr == NULL) perror("Record is null");
|
||||
if(0 > point || point > getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
}
|
||||
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
|
||||
}
|
||||
void setField(ObjRef arr, int point, ObjRef value){
|
||||
bool isNull = false;
|
||||
if(value == NULL) isNull = true;
|
||||
if(0 > point || point >= getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(arr == NULL) perror("Record is null");
|
||||
if(IS_PRIMITIVE(arr)) perror("Record is a primitive");
|
||||
if(IS_PRIMITIVE(value)){
|
||||
int size = value->size;
|
||||
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
|
||||
GET_REFS_PTR(arr)[point]->size = size;
|
||||
}else{
|
||||
int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
|
||||
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
|
||||
GET_REFS_PTR(arr)[point] ->size = size;
|
||||
int size;
|
||||
if (isNull) size = sizeof(void *);
|
||||
else {
|
||||
if (IS_PRIMITIVE(value))
|
||||
size = value->size;
|
||||
else
|
||||
size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
|
||||
}
|
||||
if((GET_REFS_PTR(arr)[point] = my_malloc(size)) == NULL) perror("malloc");
|
||||
GET_REFS_PTR(arr)[point] ->size = size;
|
||||
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||
}
|
||||
|
||||
|
||||
5
stack.c
5
stack.c
@@ -8,9 +8,6 @@
|
||||
#include <stdlib.h>
|
||||
#include "stackslot.c"
|
||||
|
||||
|
||||
|
||||
|
||||
struct stack {
|
||||
int* size;
|
||||
int* current;
|
||||
@@ -26,6 +23,8 @@ void printStack(struct stack stack, int fp) {
|
||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||
for (int i = *stack.current -1; i >= 0; --i) {
|
||||
printf("%i\t",i);
|
||||
if(stack.stack[i].u.objRef == NULL) printf("|NULL|");
|
||||
else
|
||||
if(stack.stack[i].isObjRef){
|
||||
printf("|%p|", (void *)stack.stack[i].u.objRef);
|
||||
if(stack.stack[i].u.objRef->size == sizeof(int))
|
||||
|
||||
19
stackslot.c
19
stackslot.c
@@ -1,14 +1,13 @@
|
||||
#ifndef STACKSLOT
|
||||
#define STACKSLOT
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "objref.c"
|
||||
#include "heap.h"
|
||||
|
||||
#ifndef STACKSLOT
|
||||
#define STACKSLOT
|
||||
typedef int Object;
|
||||
typedef struct ObjRef{
|
||||
unsigned int size;
|
||||
unsigned char data[1];
|
||||
} *ObjRef;
|
||||
|
||||
|
||||
typedef struct {
|
||||
bool isObjRef;
|
||||
@@ -21,7 +20,7 @@ typedef struct {
|
||||
ObjRef getIntObj(int val) {
|
||||
ObjRef intObject;
|
||||
unsigned int objSize = sizeof(ObjRef) + sizeof(int);
|
||||
if ((intObject = malloc(objSize)) == NULL) {
|
||||
if ((intObject = my_malloc(objSize)) == NULL) {
|
||||
perror("malloc");
|
||||
}
|
||||
*(int *) intObject->data = val;
|
||||
@@ -30,6 +29,7 @@ ObjRef getIntObj(int val) {
|
||||
}
|
||||
|
||||
int getValFromIntObj(ObjRef iref) {
|
||||
if (iref == NULL) perror("ObjRef is null");
|
||||
return *(int *) iref->data;
|
||||
}
|
||||
|
||||
@@ -41,13 +41,14 @@ int getIntValfromStackSlot(StackSlot s) {
|
||||
}
|
||||
|
||||
void setValIntObj(ObjRef iref, int val) {
|
||||
if (iref == NULL) perror("ObjRef is null");
|
||||
iref->size = sizeof(int)+sizeof(ObjRef);
|
||||
*(int *) iref->data = val;
|
||||
}
|
||||
|
||||
StackSlot stackSlotWithObjRef(ObjRef val) {
|
||||
StackSlot *stackSlot;
|
||||
stackSlot = malloc(sizeof(StackSlot));
|
||||
stackSlot = my_malloc(sizeof(StackSlot));
|
||||
if(stackSlot == NULL) perror("malloc");
|
||||
stackSlot->isObjRef = true;
|
||||
stackSlot->u.objRef = val;
|
||||
@@ -56,7 +57,7 @@ StackSlot stackSlotWithObjRef(ObjRef val) {
|
||||
|
||||
StackSlot stackSlotWitchNumber(int val) {
|
||||
StackSlot *stackSlot;
|
||||
stackSlot = malloc(sizeof(StackSlot));
|
||||
stackSlot = my_malloc(sizeof(StackSlot));
|
||||
if(stackSlot == NULL) perror("malloc");
|
||||
stackSlot->isObjRef = false;
|
||||
stackSlot->u.number = val;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
|
||||
#include "support.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "objref.c"
|
||||
#include "heap.h"
|
||||
|
||||
void fatalError(char *msg){
|
||||
printf("Fatal error: %s\n", msg);
|
||||
@@ -13,7 +13,7 @@ void fatalError(char *msg){
|
||||
void * newPrimObject(int dataSize) {
|
||||
ObjRef bigObjRef;
|
||||
|
||||
bigObjRef = malloc(sizeof(unsigned int) +
|
||||
bigObjRef = my_malloc(sizeof(unsigned int) +
|
||||
dataSize * sizeof(unsigned char));
|
||||
if (bigObjRef == NULL) {
|
||||
fatalError("newPrimObject() got no memory");
|
||||
|
||||
BIN
test/tests/15.bin
Normal file
BIN
test/tests/15.bin
Normal file
Binary file not shown.
23
test/tests/objTest.asm
Normal file
23
test/tests/objTest.asm
Normal file
@@ -0,0 +1,23 @@
|
||||
pushc 3
|
||||
newa
|
||||
popg 0
|
||||
|
||||
pushg 0
|
||||
pushc 0
|
||||
pushc 9
|
||||
putfa
|
||||
pushg 0
|
||||
pushc 1
|
||||
pushc 22
|
||||
putfa
|
||||
|
||||
pushg 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushg 0
|
||||
pushc 1
|
||||
getfa
|
||||
add
|
||||
wrint
|
||||
halt
|
||||
// Sollte 21 + 9 ergeben
|
||||
Reference in New Issue
Block a user