lab
This commit is contained in:
parent
5211833f3f
commit
94b459ec45
23 changed files with 85298 additions and 0 deletions
80
anno2/Sem2/lft/lab/CodeGenerator.java
Normal file
80
anno2/Sem2/lft/lab/CodeGenerator.java
Normal file
|
@ -0,0 +1,80 @@
|
|||
import java.util.LinkedList;
|
||||
import java.io.*;
|
||||
|
||||
public class CodeGenerator {
|
||||
|
||||
LinkedList <Instruction> instructions = new LinkedList <Instruction>();
|
||||
|
||||
int label=0;
|
||||
|
||||
public void emit( OpCode opCode) {
|
||||
instructions.add( new Instruction(opCode));
|
||||
}
|
||||
|
||||
public void emit( OpCode opCode , int operand ) {
|
||||
instructions.add( new Instruction( opCode, operand ));
|
||||
}
|
||||
|
||||
public void emitLabel (int operand ) {
|
||||
emit( OpCode.label , operand );
|
||||
}
|
||||
|
||||
public int newLabel () {
|
||||
return label++;
|
||||
|
||||
}
|
||||
|
||||
public void toJasmin () throws IOException{
|
||||
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\sproston\\Documents\\LFT1718\\CommandLine\\Output\\Output.j"));
|
||||
String temp = "";
|
||||
temp = temp + header;
|
||||
while(instructions.size() > 0){
|
||||
Instruction tmp = instructions.remove();
|
||||
temp = temp + tmp.toJasmin();
|
||||
}
|
||||
temp = temp + footer;
|
||||
out.println(temp);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
private static final String header = ".class public Output \n"+ ".super java/lang/Object\n"
|
||||
+ "\n"
|
||||
+ ".method public <init>()V\n"
|
||||
+ " aload_0\n"
|
||||
+ " invokenonvirtual java/lang/Object/<init>()V\n"
|
||||
+ " return\n"
|
||||
+ ".end method\n"
|
||||
+ "\n"
|
||||
+ ".method public static print(I)V\n"
|
||||
+ " .limit stack 2\n"
|
||||
+ " getstatic java/lang/System/out Ljava/io/PrintStream;\n"
|
||||
+ " iload_0 \n"
|
||||
+ " invokestatic java/lang/Integer/toString(I)Ljava/lang/String;\n"
|
||||
+ " invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n"
|
||||
+ " return\n"
|
||||
+ ".end method\n"
|
||||
+ "\n"
|
||||
+ ".method public static read()I\n"
|
||||
+ " .limit stack 3\n"
|
||||
+ " new java/util/Scanner\n"
|
||||
+ " dup\n"
|
||||
+ " getstatic java/lang/System/in Ljava/io/InputStream;\n"
|
||||
+ " invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V\n"
|
||||
+ " invokevirtual java/util/Scanner/next()Ljava/lang/String;\n"
|
||||
+ " invokestatic java/lang/Integer.parseInt(Ljava/lang/String;)I\n"
|
||||
+ " ireturn\n"
|
||||
+ ".end method\n"
|
||||
+ ".method public static run()V\n"
|
||||
+ " .limit stack 1024\n"
|
||||
+ " .limit locals 256\n";
|
||||
|
||||
private static final String footer = " return\n"
|
||||
+ ".end method\n"
|
||||
+ "\n"
|
||||
+ ".method public static main([Ljava/lang/String;)V\n"
|
||||
+ " invokestatic Output/run()V\n"
|
||||
+ " return\n"
|
||||
+ ".end method\n";
|
||||
}
|
||||
|
38
anno2/Sem2/lft/lab/Instruction.java
Normal file
38
anno2/Sem2/lft/lab/Instruction.java
Normal file
|
@ -0,0 +1,38 @@
|
|||
public class Instruction {
|
||||
OpCode opCode ;
|
||||
int operand ;
|
||||
|
||||
public Instruction ( OpCode opCode) {
|
||||
this.opCode = opCode;
|
||||
}
|
||||
|
||||
public Instruction ( OpCode opCode , int operand ) {
|
||||
this.opCode = opCode;
|
||||
this.operand = operand;
|
||||
}
|
||||
|
||||
public String toJasmin () {
|
||||
String temp="";
|
||||
switch (opCode) {
|
||||
case ldc : temp = " ldc "+ operand + "\n"; break;
|
||||
case iadd : temp = " iadd " + "\n"; break;
|
||||
case invokestatic : if( operand == 1) temp = " invokestatic "+ "Output/print(I)V" + "\n";
|
||||
else temp =" invokestatic " + "Output/read()I" + "\n"; break;
|
||||
case imul : temp = " imul " + "\n"; break;
|
||||
case idiv : temp = " idiv " + "\n"; break;
|
||||
case isub : temp = " isub " + "\n"; break;
|
||||
case istore : temp = " istore " + operand + "\n"; break;
|
||||
case iload : temp = " iload " + operand + "\n"; break;
|
||||
case if_icmpeq : temp = " if_icmpeq L" + operand + "\n"; break;
|
||||
case if_icmple : temp = " if_icmple L" + operand + "\n"; break;
|
||||
case if_icmplt : temp = " if_icmplt L" + operand + "\n"; break;
|
||||
case if_icmpne : temp = " if_icmpne L" + operand + "\n"; break;
|
||||
case if_icmpge : temp = " if_icmpge L" + operand + "\n"; break;
|
||||
case if_icmpgt : temp = " if_icmpgt L" + operand + "\n"; break;
|
||||
case ifne : temp = " ifne L" + operand + "\n"; break;
|
||||
case GOto : temp = " goto L" + operand + "\n" ; break;
|
||||
case label : temp = "L" + operand + ":\n"; break;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
6
anno2/Sem2/lft/lab/OpCode.java
Normal file
6
anno2/Sem2/lft/lab/OpCode.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
public enum OpCode {
|
||||
ldc , imul , idiv , iadd ,
|
||||
isub , istore , iload ,
|
||||
if_icmpeq , if_icmple , if_icmplt , if_icmpne, if_icmpge ,
|
||||
if_icmpgt , ifne , GOto , invokestatic , label
|
||||
}
|
21
anno2/Sem2/lft/lab/SymbolTable.java
Normal file
21
anno2/Sem2/lft/lab/SymbolTable.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
import java.util.*;
|
||||
|
||||
public class SymbolTable {
|
||||
|
||||
Map <String, Integer> OffsetMap = new HashMap <String,Integer>();
|
||||
|
||||
public void insert( String s, int address ) {
|
||||
if( !OffsetMap.containsValue(address) )
|
||||
OffsetMap.put(s,address);
|
||||
else
|
||||
throw new IllegalArgumentException("Reference to a memory location already occupied by another variable");
|
||||
}
|
||||
|
||||
public int lookupAddress ( String s ) {
|
||||
if( OffsetMap.containsKey(s) )
|
||||
return OffsetMap.get(s);
|
||||
else
|
||||
return -1;
|
||||
// throw new IllegalArgumentException("Unknown variable");
|
||||
}
|
||||
}
|
102
anno2/Sem2/lft/lab/Translator.java
Normal file
102
anno2/Sem2/lft/lab/Translator.java
Normal file
|
@ -0,0 +1,102 @@
|
|||
import java.io.*;
|
||||
|
||||
public class Translator {
|
||||
private Lexer lex;
|
||||
private BufferedReader pbr;
|
||||
private Token look;
|
||||
|
||||
SymbolTable st = new SymbolTable();
|
||||
CodeGenerator code = new CodeGenerator();
|
||||
int count=0;
|
||||
|
||||
public Translator(Lexer l, BufferedReader br) {
|
||||
lex = l;
|
||||
pbr = br;
|
||||
move();
|
||||
}
|
||||
|
||||
void move() {
|
||||
// come in Esercizio 3.1
|
||||
}
|
||||
|
||||
void error(String s) {
|
||||
// come in Esercizio 3.1
|
||||
}
|
||||
|
||||
void match(int t) {
|
||||
// come in Esercizio 3.1
|
||||
}
|
||||
|
||||
public void prog() {
|
||||
// ... completare ...
|
||||
int lnext_prog = code.newLabel();
|
||||
statlist(lnext_prog);
|
||||
code.emitLabel(lnext_prog);
|
||||
match(Tag.EOF);
|
||||
try {
|
||||
code.toJasmin();
|
||||
}
|
||||
catch(java.io.IOException e) {
|
||||
System.out.println("IO error\n");
|
||||
};
|
||||
// ... completare ...
|
||||
}
|
||||
|
||||
public void stat(int lnext) {
|
||||
switch(look.tag) {
|
||||
// ... completare ...
|
||||
case Tag.PRINT:
|
||||
match(Tag.PRINT);
|
||||
match('(');
|
||||
expr();
|
||||
code.emit(OpCode.invokestatic,1);
|
||||
match(')');
|
||||
break;
|
||||
case Tag.READ:
|
||||
match(Tag.READ);
|
||||
match('(');
|
||||
if (look.tag==Tag.ID) {
|
||||
int read_id_addr = st.lookupAddress(((Word)look).lexeme);
|
||||
if (read_id_addr==-1) {
|
||||
read_id_addr = count;
|
||||
st.insert(((Word)look).lexeme,count++);
|
||||
}
|
||||
match(Tag.ID);
|
||||
match(')');
|
||||
code.emit(OpCode.invokestatic,0);
|
||||
code.emit(OpCode.istore,read_id_addr);
|
||||
}
|
||||
else
|
||||
error("Error in grammar (stat) after read( with " + look);
|
||||
break;
|
||||
// ... completare ...
|
||||
}
|
||||
|
||||
// ... completare ...
|
||||
|
||||
private void b_expr(int ltrue, int lfalse) {
|
||||
// ... completare ...
|
||||
expr();
|
||||
if (look == Word.eq) {
|
||||
match(Tag.RELOP);
|
||||
expr();
|
||||
// ... completare ...
|
||||
}
|
||||
// ... completare ...
|
||||
}
|
||||
|
||||
// ... completare ...
|
||||
|
||||
private void exprp() {
|
||||
switch(look.tag) {
|
||||
case '+':
|
||||
match('+');
|
||||
term();
|
||||
code.emit(OpCode.iadd);
|
||||
exprp();
|
||||
break;
|
||||
// ... completare ...
|
||||
}
|
||||
}
|
||||
// ... completare ...
|
||||
}
|
BIN
anno2/Sem2/lft/lab/jasmin.jar
Normal file
BIN
anno2/Sem2/lft/lab/jasmin.jar
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione1-DFA.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione1-DFA.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione1-intro.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione1-intro.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione2-DFA.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione2-DFA.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione3-lexer1.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione3-lexer1.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione4-lexer2.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione4-lexer2.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione5-parser1.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione5-parser1.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione6-parser2.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione6-parser2.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione7-valutatore.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione7-valutatore.pdf
Normal file
Binary file not shown.
BIN
anno2/Sem2/lft/lab/lft-vv-lezione8-genbytecode.pdf
Normal file
BIN
anno2/Sem2/lft/lab/lft-vv-lezione8-genbytecode.pdf
Normal file
Binary file not shown.
10832
anno2/Sem2/lft/lab/main-6.pdf
Normal file
10832
anno2/Sem2/lft/lab/main-6.pdf
Normal file
File diff suppressed because one or more lines are too long
12940
anno2/Sem2/lft/lab/main-7.pdf
Normal file
12940
anno2/Sem2/lft/lab/main-7.pdf
Normal file
File diff suppressed because one or more lines are too long
22067
anno2/Sem2/lft/lab/main-8.pdf
Normal file
22067
anno2/Sem2/lft/lab/main-8.pdf
Normal file
File diff suppressed because one or more lines are too long
4202
anno2/Sem2/lft/lab/main.pdf
Normal file
4202
anno2/Sem2/lft/lab/main.pdf
Normal file
File diff suppressed because it is too large
Load diff
4322
anno2/Sem2/lft/lab/main2.pdf
Normal file
4322
anno2/Sem2/lft/lab/main2.pdf
Normal file
File diff suppressed because it is too large
Load diff
9434
anno2/Sem2/lft/lab/main3.pdf
Normal file
9434
anno2/Sem2/lft/lab/main3.pdf
Normal file
File diff suppressed because one or more lines are too long
9560
anno2/Sem2/lft/lab/main4.pdf
Normal file
9560
anno2/Sem2/lft/lab/main4.pdf
Normal file
File diff suppressed because one or more lines are too long
11694
anno2/Sem2/lft/lab/main5.pdf
Normal file
11694
anno2/Sem2/lft/lab/main5.pdf
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue