Skip to content

Commit da2247c

Browse files
committed
Added already existing project files
0 parents  commit da2247c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2224
-0
lines changed

.gitignore

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Project specific excludes
3+
#
4+
5+
tomcat
6+
7+
#
8+
# Default excludes
9+
#
10+
11+
# Binaries
12+
*.7z
13+
*.dmg
14+
*.gz
15+
*.iso
16+
*.jar
17+
*.rar
18+
*.tar
19+
*.zip
20+
*.war
21+
*.ear
22+
*.sar
23+
*.class
24+
25+
# Maven
26+
target/
27+
28+
# IntelliJ project files
29+
*.iml
30+
*.iws
31+
*.ipr
32+
.idea/
33+
34+
# eclipse project file
35+
.settings/
36+
.classpath
37+
.project
38+
39+
# NetBeans specific
40+
nbproject/private/
41+
build/
42+
nbbuild/
43+
dist/
44+
nbdist/
45+
nbactions.xml
46+
nb-configuration.xml
47+
48+
49+
# OS
50+
.DS_Store
51+
52+
# Misc
53+
*.swp
54+
release.properties
55+
pom.xml.releaseBackup
56+
pom.xml.tag

Language Specification.docx

18.6 KB
Binary file not shown.

bin/example.vm

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
////// This is the Java code we wish to translate into bytecode
2+
// int numPrimes = 0;
3+
// outer:
4+
// for(int n = 0; n < 100; n++) {
5+
// if (n % 2 == 0) continue;
6+
// for(int i = 3; i * i <= n; i += 2) {
7+
// if(n % i == 0)
8+
// continue outer;
9+
// }
10+
// numPrimes++;
11+
// }
12+
13+
////// Here is the bytecode
14+
#define END_VALUE = 0x64
15+
16+
// Variable definitions
17+
#define n = 0x00
18+
19+
literal 0
20+
21+
[start]
22+
load #n
23+
literal #END_VALUE
24+
cmp
25+
jmpGE end
26+
call is_prime
27+
jmpEQ skip_inc
28+
literal 1
29+
add
30+
31+
[skip_inc]
32+
load #n
33+
literal 1
34+
add
35+
store #n
36+
jmp start
37+
38+
[is_prime]
39+
load #n
40+
literal 0x02
41+
mod
42+
jmpEQ is_mod_2
43+
call check_mod_i
44+
return
45+
46+
[is_mod_2]
47+
literal 0x00
48+
return
49+
50+
[check_mod_i]
51+
literal 3
52+
[check_mod_i_start]
53+
dup
54+
dup
55+
mul
56+
load #n
57+
cmp
58+
jmpGT check_mod_i_exit
59+
dup
60+
load #n
61+
swap
62+
mod
63+
jmpNE not_zero
64+
discard
65+
literal 0x00
66+
return
67+
68+
[not_zero]
69+
literal 2
70+
add
71+
jmp check_mod_i_start
72+
73+
[check_mod_i_exit]
74+
discard
75+
literal 0xff
76+
return
77+
[end]

examples/grammar.gr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Parsing rules
2+
compilationUnit := definitions expressions EOF
3+
4+
definitions := definition*
5+
definition := DEFTAG DEFINE IDENTIFIER EQUALS NUMBER NEXTOP
6+
7+
expressions := expression*
8+
expression := (instruction | label) NEXTOP
9+
10+
instruction := IDENTIFIER (IDENTIFIER | NUMBER | taggedId)*
11+
label := LEFTBRACKET IDENTIFIER RIGHTBRACKET
12+
13+
taggedId := defRef
14+
defRef := DEFTAG IDENTIFIER
15+
16+
// Lexer rules
17+
DEFTAG := "#"
18+
DEFINE := "define"
19+
EQUALS := "="
20+
LEFTBRACKET := "["
21+
RIGHTBRACKET := "]"
22+
23+
IDENTIFIER := "[a-zA-Z][a-zA-Z0-9]*"
24+
NUMBER := "[0-9](\.[0-9]+)?"
25+
26+
NEXTOP := "[\n\r\f]+"
27+
EOF := "\Z"
28+
WHITESPACE := "[ \t]+" -> ignore
29+
COMMENT := "//[^\r\n\f]*" -> ignore

readme.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<style>
2+
table {
3+
font-family: arial, sans-serif;
4+
border-collapse: collapse;
5+
width: 100%;
6+
}
7+
8+
td, th {
9+
border: 1px solid #dddddd;
10+
text-align: left;
11+
padding: 8px;
12+
}
13+
14+
tr:nth-child(even) {
15+
background-color: #dddddd;
16+
}
17+
18+
table, th, td {
19+
border: 1px solid black;
20+
}
21+
</style>
22+
23+
<table>
24+
<tr>
25+
<th> First Name </th>
26+
<th> Middle Name </th>
27+
<th> Last Name </th>
28+
</tr>
29+
<tr>
30+
<td> First Name </th>
31+
<td> Middle Name </th>
32+
<td> Last Name </th>
33+
</tr>
34+
<tr>
35+
<td> First Name </th>
36+
<td> Middle Name </th>
37+
<td> Last Name </th>
38+
</tr>
39+
</table>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package se.student.liu.jessp088.vm;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import se.student.liu.jessp088.vm.instructions.Instruction;
7+
8+
public class Bytecode {
9+
private final List<Instruction> instructions;
10+
private int ptr;
11+
12+
public Bytecode(final List<Instruction> instructions) {
13+
this.instructions = new ArrayList<>(instructions);
14+
}
15+
16+
public boolean hasNext() {
17+
return ptr < instructions.size();
18+
}
19+
20+
public Instruction next() {
21+
if (!hasNext()) throw new IllegalStateException("End of bytecode");
22+
return instructions.get(ptr++);
23+
}
24+
25+
public int getPtr() {
26+
return ptr;
27+
}
28+
29+
public void setPtr(final int ptr) {
30+
this.ptr = ptr;
31+
}
32+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package se.student.liu.jessp088.vm;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.stream.IntStream;
5+
6+
import se.student.liu.jessp088.vm.exceptions.StackException;
7+
8+
public class Stack {
9+
private final int maxStackSize;
10+
11+
private int[] stack;
12+
private int stackPtr;
13+
14+
public Stack(final int maxStackSize) {
15+
this(16, maxStackSize);
16+
}
17+
18+
public Stack(final int startSize, final int maxStackSize) {
19+
this.maxStackSize = maxStackSize;
20+
stack = new int[startSize];
21+
}
22+
23+
public void push(final int value) {
24+
if (stackPtr >= maxStackSize)
25+
throw new StackException("Trying to push past max stack size!");
26+
if (stackPtr >= getCurrentSize()) expand(1);
27+
stack[stackPtr++] = value;
28+
}
29+
30+
public int pop() {
31+
if (stackPtr <= 0) throw new StackException("Trying to pop from an empty stack!");
32+
return stack[--stackPtr];
33+
}
34+
35+
public void expand(final int size) {
36+
final int newSize = getCurrentSize() + size;
37+
if (newSize < 0) throw new StackException("Cannot resize to negative stack size!");
38+
if (newSize > maxStackSize) throw new StackException("Cannot resize above max stack size!");
39+
40+
final int[] newStack = new int[newSize];
41+
final int len = Math.min(newStack.length, getCurrentSize());
42+
System.arraycopy(stack, 0, newStack, 0, len);
43+
stack = newStack;
44+
}
45+
46+
public void shrink(final int size) {
47+
expand(-size);
48+
}
49+
50+
public void sizeTo(final int size) {
51+
if (size < 0) throw new StackException("Cannot resize to negative stack size!");
52+
if (size > maxStackSize) throw new StackException("Cannot resize above max stack size!");
53+
54+
final int delta = size - getCurrentSize();
55+
if (delta == 0) return;
56+
if (delta > 0) {
57+
// expand
58+
expand(delta);
59+
} else {
60+
// shrink
61+
shrink(delta);
62+
}
63+
}
64+
65+
public void clear() {
66+
stackPtr = 0;
67+
}
68+
69+
public int getStackPtr() {
70+
return stackPtr;
71+
}
72+
73+
public int getCurrentSize() {
74+
return stack.length;
75+
}
76+
77+
public int getMaxSize() {
78+
return maxStackSize;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
final IntStream stream = IntStream.of(stack);
84+
return stream.limit(stackPtr).mapToObj(v -> String.format("0x%02X", v))
85+
.collect(Collectors.toList()).toString();
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package se.student.liu.jessp088.vm;
2+
3+
/** All instructions that the VM can process.
4+
*
5+
* @author Charanor */
6+
public enum VMInstruction {
7+
// Arithmetic instructions
8+
LITERAL(1),
9+
ADD(0),
10+
SUB(0),
11+
DIV(0),
12+
MUL(0),
13+
MOD(0),
14+
15+
// Control Flow Instructions
16+
CMP(0),
17+
JMP(1),
18+
JMPGT(1),
19+
JMPLT(1),
20+
JMPEQ(1),
21+
JMPNE(1),
22+
JMPGE(1),
23+
JMPLE(1),
24+
CALL(1),
25+
BREAK(0),
26+
RETURN(0),
27+
28+
// Data Transfer Instructions
29+
DUP(0),
30+
SWAP(0),
31+
LOAD(1),
32+
STORE(1),
33+
DISCARD(0),
34+
35+
// Meta Control Instructions
36+
MALLOC(1),
37+
FREE(1),
38+
SIZETO(1),;
39+
40+
public final int numArguments;
41+
42+
private VMInstruction(final int numArguments) {
43+
this.numArguments = numArguments;
44+
}
45+
}

0 commit comments

Comments
 (0)