1 | package toolchain.b2k.lang.builtin; |
---|
2 | |
---|
3 | import static toolchain.b2k.lang.paramBuiltin.idisa.IDISABuiltin.*; |
---|
4 | import static toolchain.b2k.lang.type.PrimitiveType.BITBLOCK_SIZE; |
---|
5 | |
---|
6 | import java.util.List; |
---|
7 | import b2k.ast.*; |
---|
8 | import b2k.inputHandler.*; |
---|
9 | import b2k.ast.IntegerConstantNode; |
---|
10 | import toolchain.b2k.ast.Accessors; |
---|
11 | import toolchain.b2k.ast.Generators; |
---|
12 | import toolchain.b2k.ast.Mutators; |
---|
13 | import toolchain.b2k.lang.builtin.B2KBuiltins; |
---|
14 | import toolchain.s2k.transformer.visitors.S2K2B2K.Context; |
---|
15 | |
---|
16 | |
---|
17 | // BuiltinTranslators translate s2k builtin calls from s2k to b2k. The translation of b2k calls |
---|
18 | // uses a C or CPP 'pablo_blk_call' name. |
---|
19 | // The enum values follow the Command pattern, with the benefit of the client not having to |
---|
20 | // instantiate the commands. |
---|
21 | |
---|
22 | public enum B2KBuiltinsTranslator { |
---|
23 | |
---|
24 | CARRY_TRANSLATION { |
---|
25 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
26 | |
---|
27 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
28 | |
---|
29 | String b2kBlkCallFuncName = context.getCode(builtin); |
---|
30 | ASTNode b2kBlkFuncCall = Generators.makeFuncCallNode(locator, b2kBlkCallFuncName, arrayFromList(args)); |
---|
31 | ASTNode srliValue = Generators.makeIntegerConstantNode(locator, BITBLOCK_SIZE - 1); |
---|
32 | |
---|
33 | return Generators.makeIdisaFuncCallNode(locator, BITBLOCK_SRLI, BITBLOCK_SIZE, srliValue, b2kBlkFuncCall); |
---|
34 | } |
---|
35 | }, |
---|
36 | |
---|
37 | ADVANCEN_TRANSLATION { |
---|
38 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
39 | Locator locator = Generators.makeTextLocation(snode.getLocation()); |
---|
40 | |
---|
41 | ASTNode formatValue = args.remove(1); |
---|
42 | String value = Accessors.lexeme(formatValue); |
---|
43 | String templateCode = context.getCode(builtin); |
---|
44 | |
---|
45 | String b2kBlkCallFuncName = String.format(templateCode, value); |
---|
46 | ASTNode b2kBlkFuncCall = Generators.makeFuncCallNode(locator, b2kBlkCallFuncName, arrayFromList(args)); |
---|
47 | ASTNode srliValue = Generators.makeIntegerConstantNode(locator, BITBLOCK_SIZE - 1); |
---|
48 | |
---|
49 | return Generators.makeIdisaFuncCallNode(locator, BITBLOCK_SRLI, BITBLOCK_SIZE, srliValue, b2kBlkFuncCall); |
---|
50 | } |
---|
51 | }, |
---|
52 | |
---|
53 | SCANTO_FIRST_TRANSLATION { |
---|
54 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
55 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
56 | |
---|
57 | if(context.isFinalBlockMode()) { |
---|
58 | args.set(0, Generators.makeExprOrNotEofMaskCall(locator, args.get(0))); |
---|
59 | } |
---|
60 | |
---|
61 | return CARRY_TRANSLATION.translate(B2KBuiltins.FLOW_SCAN_TO_FIRST, snode, context, args); |
---|
62 | } |
---|
63 | }, |
---|
64 | |
---|
65 | SCANTO_TRANSLATION { |
---|
66 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
67 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
68 | |
---|
69 | args.set(1, translateScanToArgs(locator, context, args.get(1))); |
---|
70 | |
---|
71 | return CARRY_TRANSLATION.translate(B2KBuiltins.FLOW_SCAN_THRU, snode, context, args); |
---|
72 | } |
---|
73 | }, |
---|
74 | |
---|
75 | ADVANCE_THEN_SCANTO_TRANSLATION { |
---|
76 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
77 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
78 | |
---|
79 | args.set(1, translateScanToArgs(locator, context, args.get(1))); |
---|
80 | |
---|
81 | return CARRY_TRANSLATION.translate(B2KBuiltins.FLOW_ADVANCE_THEN_SCAN_THRU, snode, context, args); |
---|
82 | } |
---|
83 | |
---|
84 | }, |
---|
85 | |
---|
86 | ASSERT_ZERO_TRANSLATION { |
---|
87 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
88 | |
---|
89 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
90 | |
---|
91 | ASTNode errorStream = args.get(0); |
---|
92 | ASTNode errorMsg = args.get(1); |
---|
93 | |
---|
94 | String assert0CallName = context.getBuiltinEncoder().getCode(B2KBuiltins.FLOW_ASSERT_ZERO); |
---|
95 | |
---|
96 | ASTNode assert0Call = Generators.makeFuncCallNode(locator, assert0CallName, errorStream, errorMsg); |
---|
97 | |
---|
98 | BlockStmtNode blockStmt = Generators.makeBlockStmtNode(locator, assert0Call); |
---|
99 | IfStmtNode replacement = Generators.makeIfStmtNode(locator, errorStream.deepCopy(), blockStmt); |
---|
100 | |
---|
101 | Mutators.surroundConditionWithBitBlockAny(replacement, Accessors.condition(replacement)); |
---|
102 | |
---|
103 | return replacement; |
---|
104 | } |
---|
105 | }, |
---|
106 | |
---|
107 | ATEOF_TRANSLATION { |
---|
108 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
109 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
110 | |
---|
111 | if(context.isFinalBlockMode()) { |
---|
112 | |
---|
113 | return Generators.makeExprAndCEofMaskCall(locator, args.get(0)); |
---|
114 | } else { |
---|
115 | |
---|
116 | FieldWidthNode fieldWidth = Generators.makeIdisaFieldWidthNode(locator, 1); |
---|
117 | ASTNode value = Generators.makeIntegerConstantNode(locator, 0); |
---|
118 | return Generators.makeIdisaFuncCallNode(locator, SIMD_CONSTANT, fieldWidth, value); |
---|
119 | } |
---|
120 | } |
---|
121 | }, |
---|
122 | |
---|
123 | INFILE_TRANSLATION { |
---|
124 | |
---|
125 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
126 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
127 | |
---|
128 | if(context.isFinalBlockMode()) { |
---|
129 | return Generators.makeExprAndEofMaskCall(locator, args.get(0)); |
---|
130 | } else { |
---|
131 | return args.get(0); |
---|
132 | } |
---|
133 | } |
---|
134 | }, |
---|
135 | |
---|
136 | MASK_TRANSLATION { |
---|
137 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode snode, Context context, List<ASTNode> args) { |
---|
138 | |
---|
139 | Locator locator = Generators.makeToken(snode.getToken()); |
---|
140 | IntegerConstantNode n = (IntegerConstantNode)args.get(0); |
---|
141 | ASTNode fieldWidth = Generators.makeIdisaFieldWidthNode(locator, Accessors.intValue(n)); |
---|
142 | ASTNode maskValue = args.get(1); |
---|
143 | return Generators.makeIdisaFuncCallNode(locator, SIMD_CONSTANT, (FieldWidthNode) fieldWidth, maskValue); |
---|
144 | } |
---|
145 | }, |
---|
146 | |
---|
147 | NULL_TRANSLATION { |
---|
148 | @SuppressWarnings("unused") |
---|
149 | public ASTNode translate() { |
---|
150 | throw new RuntimeException("B2K builtin translation not unimplemented."); |
---|
151 | } |
---|
152 | }; |
---|
153 | |
---|
154 | // Interface |
---|
155 | public ASTNode translate(B2KBuiltins builtin, s2k.ast.FuncCallNode node, Context context, List<ASTNode> args) { |
---|
156 | assert false : "'pablo_blk_' S2KBuiltin translate not implemented for S2KBuitin: " + this.name(); |
---|
157 | return null; |
---|
158 | } |
---|
159 | |
---|
160 | // Helpers |
---|
161 | private static ASTNode translateScanToArgs(Locator locator, Context context, ASTNode arg) { |
---|
162 | |
---|
163 | if(context.isFinalBlockMode()) { |
---|
164 | return Generators.makeEofMaskAndCExprCall(locator, arg.deepCopy()); |
---|
165 | } else { |
---|
166 | return Generators.makeIdisaFuncCallNode(locator, SIMD_NOT, BITBLOCK_SIZE, arg.deepCopy()); |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | private static ASTNode[] arrayFromList(List<ASTNode> list) { |
---|
172 | return list.toArray(new ASTNode [list.size()]); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | } |
---|