Skip to content

Commit d4b324c

Browse files
committed
еще правки для 5 лабы
1 parent 860341c commit d4b324c

Some content is hidden

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

51 files changed

+2194
-1167
lines changed

bcomp-assembler/src/main/antlr4/ru.ifmo.cs.bcomp.grammar/BCompNG.g4

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ wordArguments
4343
wordArgument
4444
: number
4545
| '$' label
46+
| dupArgument
47+
| '?'
48+
;
49+
50+
dupArgument
51+
: count dup '(' wordArgument ')'
52+
;
53+
54+
count
55+
: number
4656
;
4757

4858
lbl
@@ -120,18 +130,20 @@ comment
120130

121131
addr: AND | OR | ADD | ADC | SUB | CMP | LOOP | LD | SWAM | JUMP | CALL | ST;
122132
nonaddr: NOP | HLT | CLA | NOT | CLC | CMC | ROL | ROR | ASL | ASR | SXTB | SWAB |
123-
INC | DEC | NEG | POP | POPF | RET | IRET | PUSH | PUSHF | SWAP;
133+
INC | DEC | NEG | POP | POPF | RET | IRET | PUSH | PUSHF | SWAP |
134+
EI | DI;
124135
branch: BEQ | BNE | BMI | BPL | BCS | BCC | BVS | BVC | BLT | BGE | BR;
125-
io: CLF | TSF | IN | OUT;
136+
io: IN | OUT | INT;
126137

127138
sp: SP;
128139
ip: IP;
129140

130141
org: ORG;
131142
word: WORD;
143+
dup: DUP;
132144
end: END;
133145

134-
fragment A : ('a' | 'A');
146+
fragment A : ('a' | 'A');
135147
fragment B : ('b' | 'B');
136148
fragment C : ('c' | 'C');
137149
fragment D : ('d' | 'D');
@@ -158,7 +170,7 @@ fragment X : ('x' | 'X');
158170
fragment Y : ('y' | 'Y');
159171
fragment Z : ('z' | 'Z');
160172

161-
fragment RA : ('а' | 'А');
173+
fragment RA : ('а' | 'А');
162174
fragment RB : ('б' | 'Б');
163175
fragment RV : ('в' | 'В');
164176
fragment RG : ('г' | 'Г');
@@ -201,6 +213,7 @@ fragment P0D : '0' D ;
201213
ORG: O R G;
202214
WORD: W O R D;
203215
END: E N D;
216+
DUP: ( D U P ) | ( D U P L I C A T E );
204217

205218
/*
206219
* opcodes
@@ -229,9 +242,9 @@ ROL: ( R O L ) ;
229242
ROR: ( R O R ) ;
230243
ASL: ( A S L ) ;
231244
ASR: ( A S R ) ;
232-
SXTB: ( S X T B ) ;
245+
SXTB: ( S X T B ) ;
233246
SWAB: ( S W A B ) ;
234-
INC: ( I N C ) ;
247+
INC: ( I N C ) ;
235248
DEC: ( D E C ) ;
236249
NEG: ( N E G ) ;
237250
POP: ( P O P ) ;
@@ -244,7 +257,7 @@ SWAP: ( S W A P ) ;
244257

245258
BEQ: ( B E Q ) | ( B Z S );
246259
BNE: ( B N E ) | ( B Z C );
247-
BMI: ( B M I ) | ( B N S );
260+
BMI: ( B M I ) | ( B N S );
248261
BPL: ( B P L ) | ( B N C );
249262
BCS: ( B C S ) | ( B L O );
250263
BCC: ( B C C ) | ( B H I S );
@@ -255,24 +268,28 @@ BGE: ( B G E ) ;
255268
BR: ( B R ) ; //syntetic insturction, jump with direct relative addressing mode
256269

257270

258-
CLF: ( C L F ) ;
259-
TSF: ( T S F ) ;
271+
DI: ( D I ) ;
272+
EI: ( E I ) ;
260273
IN: ( I N ) ;
261274
OUT: ( O U T ) ;
275+
INT: ( I N T ) ;
262276

263277
SP: ( S P ) ;
264278
IP: ( I P ) ;
265279

266280
NAME
267-
: [a-zA-Zа-яА-Я] [a-zA-Zа-яА-Я0-9."]*
281+
: [a-zA-Zа-яА-Я_] [a-zA-Zа-яА-Я_0-9."]*
268282
;
269283
270284
DECIMAL
271-
: P0D? DECDIGIT+
285+
: ( P0D? DECDIGIT+ )
286+
| ( '-' P0D? DECDIGIT+ )
287+
| ( P0D? '-' DECDIGIT+ )
272288
;
273289
274290
HEX
275291
: HEXDIGIT+ | (P0X HEXDIGIT+ ) | ( HEXDIGIT+ H )
292+
| ( '-' HEXDIGIT+ ) | ( '-' P0X HEXDIGIT+ ) | (P0X '-' HEXDIGIT+ ) | ( '-' HEXDIGIT+ H )
276293
;
277294
278295
COMMENT

bcomp-assembler/src/main/java/ru/ifmo/cs/bcomp/assembler/AsmNg.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.HashMap;
2020
import java.util.LinkedList;
21+
import java.util.List;
2122

2223
/**
2324
*
@@ -29,6 +30,7 @@ public class AsmNg {
2930
* from base_address
3031
*/
3132
public static final int BASE_ADDRESS = 0x10;
33+
private List<String> errors;
3234

3335
public static void main(String[] args) throws Exception {
3436
AsmNg asmng = new AsmNg(
@@ -74,6 +76,10 @@ public BCompNGParser getParser() {
7476
return parser;
7577
}
7678

79+
public List<String> getErrors() {
80+
return errors;
81+
}
82+
7783
public Program compile () {
7884
//decode commands and collect all labels
7985
//System.out.println("first pass");
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ru.ifmo.cs.bcomp.assembler;
7+
8+
import org.antlr.v4.runtime.DefaultErrorStrategy;
9+
import org.antlr.v4.runtime.Parser;
10+
import org.antlr.v4.runtime.RecognitionException;
11+
import org.antlr.v4.runtime.misc.IntervalSet;
12+
13+
/**
14+
*
15+
* @author serge
16+
*/
17+
public class AssemblerAntlrErrorStrategy extends DefaultErrorStrategy {
18+
19+
@Override
20+
public void reportError(Parser recognizer, RecognitionException e) {
21+
//System.err.println(e.getMessage());
22+
if (!(e instanceof AssemblerException)) {
23+
super.reportError(recognizer, e);
24+
return;
25+
}
26+
AssemblerException ae = (AssemblerException)e;
27+
// if we've already reported an error and have not matched a token
28+
// yet successfully, don't report any errors.
29+
//if (inErrorRecoveryMode(recognizer)) {
30+
//System.err.print("[SPURIOUS] ");
31+
// return; // don't report spurious errors
32+
//}
33+
beginErrorCondition(recognizer);
34+
recognizer.notifyErrorListeners(ae.getOffendingToken(),ae.getMessage(), ae);
35+
}
36+
37+
@Override
38+
public void recover(Parser recognizer, RecognitionException e) {
39+
// System.out.println("recover in "+recognizer.getRuleInvocationStack()+
40+
// " index="+recognizer.getInputStream().index()+
41+
// ", lastErrorIndex="+
42+
// lastErrorIndex+
43+
// ", states="+lastErrorStates);
44+
if ( lastErrorIndex==recognizer.getInputStream().index() &&
45+
lastErrorStates != null &&
46+
lastErrorStates.contains(recognizer.getState()) ) {
47+
// uh oh, another error at same token index and previously-visited
48+
// state in ATN; must be a case where LT(1) is in the recovery
49+
// token set so nothing got consumed. Consume a single token
50+
// at least to prevent an infinite loop; this is a failsafe.
51+
// System.err.println("seen error condition before index="+
52+
// lastErrorIndex+", states="+lastErrorStates);
53+
// System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]);
54+
recognizer.consume();
55+
}
56+
lastErrorIndex = recognizer.getInputStream().index();
57+
if ( lastErrorStates==null ) lastErrorStates = new IntervalSet();
58+
lastErrorStates.add(recognizer.getState());
59+
IntervalSet followSet = getErrorRecoverySet(recognizer);
60+
consumeUntil(recognizer, followSet);
61+
}
62+
63+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ru.ifmo.cs.bcomp.assembler;
7+
8+
import org.antlr.v4.runtime.Parser;
9+
import org.antlr.v4.runtime.ParserRuleContext;
10+
import org.antlr.v4.runtime.RecognitionException;
11+
12+
/**
13+
*
14+
* @author serge
15+
*/
16+
public class AssemblerException extends RecognitionException {
17+
18+
19+
public AssemblerException(Parser recognizer) { // LL(1) error
20+
super(recognizer, recognizer.getInputStream(), recognizer.getContext());
21+
this.setOffendingToken(recognizer.getCurrentToken());
22+
}
23+
24+
25+
public AssemblerException(String msg, Parser recognizer, ParserRuleContext ctx) {
26+
super(msg, recognizer, recognizer.getInputStream(), ctx);
27+
//System.out.println(msg+recognizer.getCurrentToken());
28+
this.setOffendingToken(recognizer.getCurrentToken());
29+
}
30+
31+
public AssemblerException(String msg, Parser recognizer) {
32+
super(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
33+
this.setOffendingToken(recognizer.getCurrentToken());
34+
}
35+
36+
public ParserRuleContext getParserRuleContext() {
37+
return (ParserRuleContext)getCtx();
38+
}
39+
40+
41+
42+
}

bcomp-assembler/src/main/java/ru/ifmo/cs/bcomp/assembler/Instruction.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author Dmitry Afanasiev <[email protected]>
1111
*/
1212
public enum Instruction {
13-
13+
1414
//address
1515
AND(0x2000,ADDR), OR(0x3000 ,ADDR), ADD(0x4000,ADDR), ADC(0x5000,ADDR),
1616
SUB(0x6000,ADDR), CMP(0x7000,ADDR), LOOP(0x8000,ADDR), LD(0xA000,ADDR),
@@ -28,6 +28,8 @@ public enum Instruction {
2828
BEQ(0xF000,BRANCH), BNE(0xF100,BRANCH), BMI(0xF200,BRANCH), BPL(0xF300,BRANCH),
2929
BCS(0xF400,BRANCH), BCC(0xF500,BRANCH), BVS(0xF600,BRANCH), BVC(0xF700,BRANCH),
3030
BLT(0xF800,BRANCH), BGE(0xF900,BRANCH), BR(0xCE00, BRANCH),
31+
//io
32+
DI(0x1000,NONADDR),EI(0x1100,NONADDR),IN(0x1200,IO),OUT(0x1300,IO),INT(0x1800,IO),
3133

3234
END(1,Type.NONADDR);
3335

@@ -40,15 +42,15 @@ public enum Instruction {
4042
}
4143

4244
public enum Type {
43-
ADDR, NONADDR, BRANCH, IO
45+
ADDR, NONADDR, BRANCH, IO
4446
};
4547

4648
public final int opcode;
4749
public final String mnemonic;
4850
public Type type;
4951

5052
public String getTypeString() {
51-
return type.name();
53+
return type.name();
5254
}
53-
54-
}
55+
56+
}

bcomp-assembler/src/main/java/ru/ifmo/cs/bcomp/assembler/InstructionWord.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
* @author serge
1111
*/
1212
public class InstructionWord extends MemoryWord {
13-
public volatile Instruction instruction;
14-
public volatile AddressingMode operand = null;
15-
13+
public volatile Instruction instruction = null;
14+
public volatile AddressingMode operand = null; //only for address command
15+
public volatile Integer device = UNDEFINED; //only for io command IN OUT INTR
16+
1617
@Override
1718
public String toString() {
1819
return Integer.toHexString(address+0x100000).substring(3) + "| " +
1920
(label != null ? label.name + ": " : "" ) +
2021
instruction.mnemonic + " " +
2122
(instruction.type == Instruction.Type.ADDR? operand.toString(): "") +
2223
(instruction.type == Instruction.Type.BRANCH? " label="+operand.reference: "") +
23-
" \t; type=" + instruction.type +
24+
" \t; type=" + instruction.type +
2425
(value != UNDEFINED? " value=0x"+Integer.toHexString(value): "");
25-
}
26-
27-
}
26+
}
27+
28+
}

0 commit comments

Comments
 (0)