summaryrefslogtreecommitdiff
path: root/src/spiglet/spiglet2kanga/SpgStmt.java
blob: e6298dcc1e35c2cb72cd73fa3cdfbe41ee247305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package spiglet.spiglet2kanga;

import java.util.HashSet;

public class SpgStmt extends SpgSym{
	public enum StmtType { NOOP, ERROR, CJUMP, JUMP, STORE, LOAD, MOVE, PRINT };
	StmtType type;
	public SpgTemp tmp1, tmp2;
	public int imm;
	public String jmptarget;
	public SpgExpr exp;
	
	public String lb;
	public SpgStmt succ1, succ2;
	
	public HashSet<SpgTemp> def, use;
	
	public SpgStmt(StmtType t) {
		type = t;
	}
	
	public String toString() {
		String str="";
		if (lb!=null) str = lb + " ";
		switch (type) {
		case CJUMP:
			return str + "CJUMP " + tmp1.toString() + " " + jmptarget;
		case ERROR:
			return str + "ERROR";
		case JUMP:
			return str + "JUMP " + jmptarget;
		case LOAD:
			return str + "LD " + tmp1.toString() + " " + tmp2.toString() + " " + imm;
		case MOVE:
			return str + "MOVE " + tmp1.toString() + " " + exp.toString();
		case NOOP:
			return str + "NOOP";
		case PRINT:
			return str + "PRINT " + exp.toString();
		case STORE:
			return str + "STORE " + tmp1.toString() + " " + imm + " " + tmp2.toString();
		default:
			return null;		
		}
	}
	
	public void getDefUse() {
		switch (type) {
		case CJUMP:
			def = null;
			use = tmp1.getTmpUsed();
			break;
		case ERROR:
			def = use = null;
			break;
		case JUMP:
			def = use = null;
			break;
		case LOAD:
			def = tmp1.getTmpUsed();
			use = tmp2.getTmpUsed();
			break;
		case MOVE:
			def = tmp1.getTmpUsed();
			use = exp.getTmpUsed();
			break;
		case NOOP:
			def = use = null;
			break;
		case PRINT:
			def = null;
			use = exp.getTmpUsed();
			break;
		case STORE:
			def = null;
			use = tmp1.getTmpUsed();
			use.add(tmp2);
			break;
		default:
			System.err.println("Unknown statement type");
			def = use = null;
			break;
		
		}
	}
	
	public void printDefUse() {
		if (def!=null) {
			System.err.print("def: ");
			SpgTemp[] d = def.toArray(new SpgTemp[0]);
			for (int i=0; i<d.length; i++) {
				System.err.print(d[i].num+" ");
			}
		}
		System.err.println();
		if (use!=null) {
			System.err.print("use: ");
			SpgTemp[] u = use.toArray(new SpgTemp[0]);
			for (int i=0; i<u.length; i++) {
				System.err.print(u[i].num+" ");
			}
		}
		System.err.println();
	}
}