Debian Codegraph @

include/fcml_stateful_assembler.hpp

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /** @file fcml_stateful_assembler.hpp
21 * Stateful FCML assembler implementation.
22 *
23 * @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved.
24 * This project is released under the GNU Lesser General Public License.
25 */
26
27 #ifndef FCML_STATEFUL_ASSEMBLER_HPP_
28 #define FCML_STATEFUL_ASSEMBLER_HPP_
29
30 #include <vector>
31
32 #include "fcml_assembler.hpp"
33 #include "fcml_parser.hpp"
34
35 namespace fcml {
36
37 /**
38 * It's a stateful assembler which can be used to assemble instructions one by
39 * one on the fly. By default is works with the general instruction models as
40 * well as with instruction builders, but it can be also configured to parse
41 * whole statements using internally managed parser. It holds it's own state,
42 * so it's not necessary to update instruction pointer etc while it's working.
43 * Assembled instructions are placed inside a dedicated vector, but generated
44 * machine code is accessing
45 * directly by the dedicated CodeIterator.
46 *
47 * @since 1.1.0
48 * @remarks This class isn't thread-safe.
49 */
50 class StatefulAssembler {
51 public:
52
53 /** Used only to indicate the need of the flush operation. */
54 class SAFlush {
55 };
56
57 /**
58 * Creates a stateful assembler for the given assembler and assembler
59 * context. Bear in mind that assembler and context are not copied in
60 * any way and have to be valid as long as the stateful assembler in in
61 * use. Parsing support can be enabled optionally using the third parameter.
62 *
63 * @param assembler The assembled that will be used to assemble the code.
64 * @param context The assembler context.
65 * @param enableParser Enables parsing support.
66 * @since 1.1.0
67 */
68 StatefulAssembler(Assembler &assembler, AssemblerContext &context,
69 bool enableParser = false) :
70 _instructionBuilder(IB(FCML_TEXT("")), false),
71 _assembler(assembler),
72 _context(context),
73 _codeLength(0) {
74 // Create parser if needed.
75 _parser = enableParser ? new Parser(assembler.getDialect()) : NULL;
76 }
77
78 /** Destructor.
79 * @since 1.1.0
80 */
81 virtual ~StatefulAssembler() {
82 if (_parser) {
83 delete _parser;
84 }
85 }
86
87 /**
88 * Adds instruction builder to the stateful assembler. Such a instruction
89 * builder will be used then to assemble an instruction it represents.
90 * Before any operation is performed, pending instruction is flushed if
91 * there is any available.
92 *
93 * @param ib The instruction builder to be flushed.
94 * @return The stateful assembler itself.
95 * @throw AssemblingFailedException
96 * @since 1.1.0
97 */
98 StatefulAssembler& operator <<(const IB &ib) {
99 return add(ib);
100 }
101
102 /**
103 * Adds instruction builder to the stateful assembler. Such a instruction
104 * builder will be used then to assemble an instruction it represents.
105 * Before any operation is performed, pending instruction is flushed if
106 * there is any available.
107 *
108 * @param ib The instruction builder to be flushed.
109 * @return The stateful assembler itself.
110 * @throw AssemblingFailedException
111 * @since 1.1.0
112 */
113 StatefulAssembler& add(const IB &ib) {
114 flush();
115 _instructionBuilder.setNotNull(true);
116 _instructionBuilder.setValue(ib);
117 return *this;
118 }
119
120 /**
121 * Creates an instruction builder for the given mnemonic.
122 *
123 * @param mnemonic The instruction mnemonic.
124 * @return The stateful assembler itself.
125 * @throw AssemblingFailedException, ParsingFailedException
126 * @since 1.1.0
127 */
128 StatefulAssembler& operator <<(const fcml_cstring &mnemonic) {
129 return inst(mnemonic);
130 }
131
132 /**
133 * Creates an instruction builder for the given mnemonic.
134 *
135 * @param mnemonic The instruction mnemonic.
136 * @return The stateful assembler itself.
137 * @throw AssemblingFailedException, ParsingFailedException
138 * @since 1.1.0
139 */
140 StatefulAssembler& inst(const fcml_cstring &mnemonic) {
141 flush();
142 if (_parser) {
143 // Parse instruction and then pass it to the assembler.
144 _parserContext.setIp(_context.getEntryPoint().getIP());
145 ParserConfig &config = _parserContext.getConfig();
146 config.setThrowExceptionOnError(true);
147 _parser->parse(_parserContext, mnemonic, _parserResult);
148 *this << _parserResult.getInstruction();
149 } else {
150 // Parser is not available, so treat this string as a full
151 // instruction which have to be parsed.
152 _instructionBuilder.setNotNull(true);
153 _instructionBuilder.setValue(IB(mnemonic));
154 }
155 return *this;
156 }
157
158 /**
159 * Adds the new register operand to the instruction builder associated
160 * with the buffer.
161 *
162 * @param reg The register.
163 * @return The stateful assembler itself.
164 * @throw IllegalStateException
165 * @since 1.1.0
166 */
167 StatefulAssembler& operator <<(const Register &reg) {
168 return op(Operand(reg));
169 }
170
171 /**
172 * Adds the new immediate operand to the instruction builder associated
173 * with the buffer.
174 *
175 * @param imm The immediate value.
176 * @return The stateful assembler itself.
177 * @throw IllegalStateException
178 * @since 1.1.0
179 */
180 StatefulAssembler& operator <<(const Integer &imm) {
181 return op(Operand(imm));
182 }
183
184 /**
185 * Adds the new address operand to the instruction builder associated
186 * with the buffer.
187 *
188 * @param address The address.
189 * @return The stateful assembler itself.
190 * @throw IllegalStateException
191 * @since 1.1.0
192 */
193 StatefulAssembler& operator <<(const Address &address) {
194 return op(Operand(address));
195 }
196
197 /**
198 * Adds the new far pointer operand to the instruction builder associated
199 * with the buffer.
200 *
201 * @param pointer The far pointer.
202 * @return The stateful assembler itself.
203 * @throw IllegalStateException
204 * @since 1.1.0
205 */
206 StatefulAssembler& operator <<(const FarPointer &pointer) {
207 return op(Operand(pointer));
208 }
209
210 /**
211 * Adds an operand to the instruction builder associated with the buffer.
212 *
213 * @param operand The operand to be added.
214 * @return The stateful assembler itself.
215 * @throw IllegalStateException
216 * @since 1.1.0
217 */
218 StatefulAssembler& operator <<(const Operand &operand) {
219 return op(operand);
220 }
221
222 /**
223 * Adds an operand to the instruction builder associated with the buffer.
224 *
225 * @param operand The operand to be added..
226 * @return The stateful assembler itself.
227 * @throw IllegalStateException
228 * @since 1.1.0
229 */
230 StatefulAssembler& op(const Operand &operand) {
231 if (!_instructionBuilder.isNotNull()) {
232 throw IllegalStateException(
233 FCML_TEXT("No instruction builder available."));
234 }
235 IB &ib = _instructionBuilder.getValue();
236 ib.op(operand);
237 return *this;
238 }
239
240 /**
241 * Adds the new register operand to the instruction builder associated
242 * with the buffer.
243 *
244 * @param reg The register.
245 * @return The stateful assembler itself.
246 * @throw IllegalStateException
247 * @since 1.1.0
248 */
249 StatefulAssembler& op(const Register &reg) {
250 return op(Operand(reg));
251 }
252
253 /**
254 * Adds the new immediate operand to the instruction builder associated
255 * with the buffer.
256 *
257 * @param imm The immediate value.
258 * @return The stateful assembler itself.
259 * @throw IllegalStateException
260 * @since 1.1.0
261 */
262 StatefulAssembler& op(const Integer &imm) {
263 return op(Operand(imm));
264 }
265
266 /**
267 * Adds the new address operand to the instruction builder associated
268 * with the buffer.
269 *
270 * @param address The address.
271 * @return The stateful assembler itself.
272 * @throw IllegalStateException
273 * @since 1.1.0
274 */
275 StatefulAssembler& op(const Address &address) {
276 return op(Operand(address));
277 }
278
279 /**
280 * Adds the new far pointer operand to the instruction builder associated
281 * with the buffer.
282 *
283 * @param pointer The far pointer.
284 * @return The stateful assembler itself.
285 * @throw IllegalStateException
286 * @since 1.1.0
287 */
288 StatefulAssembler& op(const FarPointer &pointer) {
289 return op(Operand(pointer));
290 }
291
292 /**
293 * Flushes the instruction builder.
294 * @param indic Flush indicator.
295 * @return The stateful assembler itself.
296 * @throw AssemblingFailedException
297 * @since 1.1.0
298 */
299 StatefulAssembler& operator <<(const SAFlush &indic) {
300 flush();
301 return *this;
302 }
303
304 /**
305 * Assembles an instruction in the given instruction builder.
306 *
307 * @param instruction Instruction to be assembled.
308 * @return Stateful assembler.
309 * @throw AssemblingFailedException
310 * @since 1.1.0
311 */
312 StatefulAssembler& operator <<(const Instruction &instruction) {
313 return inst(instruction);
314 }
315
316 /**
317 * Adds a prefix to the instruction being built.
318 * @param prefix The prefix to be added.
319 * @return The instruction builder with the new prefix set for it.
320 * @since 1.1.0
321 */
322 StatefulAssembler& operator <<(const InstructionPrefix &prefix) {
323 return set(prefix);
324 }
325
326 /**
327 * Adds an instruction level hint to the instruction being built.
328 * @param hint The hint to be added.
329 * @return The instruction builder with the new hint added to it.
330 * @since 1.1.0
331 */
332 StatefulAssembler& operator <<(const InstructionHint &hint) {
333 return set(hint);
334 }
335
336 /**
337 * Adds an operand level hint to the instruction being built.
338 * @param hint The hint to be added.
339 * @return The instruction builder with the new hint added to it.
340 * @since 1.1.0
341 */
342 StatefulAssembler& operator <<(const OperandHint &hint) {
343 return set(hint);
344 }
345
346 /**
347 * Adds a prefix to the instruction being built.
348 * @param prefix The prefix to be added.
349 * @return The instruction builder with the new prefix set for it.
350 * @since 1.1.0
351 */
352 StatefulAssembler& set(const InstructionPrefix &prefix) {
353 if (!_instructionBuilder.isNotNull()) {
354 throw IllegalStateException(
355 FCML_TEXT("No instruction builder available."));
356 }
357 _instructionBuilder.getValue() << prefix;
358 return *this;
359 }
360
361 /**
362 * Adds an instruction level hint to the instruction being built.
363 * @param hint The hint to be added.
364 * @return The instruction builder with the new hint added to it.
365 * @since 1.1.0
366 */
367 StatefulAssembler& set(const InstructionHint &hint) {
368 if (!_instructionBuilder.isNotNull()) {
369 throw IllegalStateException(
370 FCML_TEXT("No instruction builder available."));
371 }
372 _instructionBuilder.getValue() << hint;
373 return *this;
374 }
375
376 /**
377 * Adds an operand level hint to the instruction being built.
378 * @param hint The hint to be added.
379 * @return The instruction builder with the new hint added to it.
380 * @since 1.1.0
381 */
382 StatefulAssembler& set(const OperandHint &hint) {
383 if (!_instructionBuilder.isNotNull()) {
384 throw IllegalStateException(
385 FCML_TEXT("No instruction builder available."));
386 }
387 _instructionBuilder.getValue() << hint;
388 return *this;
389 }
390
391 /**
392 * Assembles an instruction in the given instruction builder.
393 *
394 * @param instruction Instruction to be assembled.
395 * @return Stateful assembler.
396 * @throw AssemblingFailedException
397 * @since 1.1.0
398 */
399 StatefulAssembler& inst(const Instruction &instruction) {
400
401 // Flush pending instruction if there is any.
402 flush();
403
404 // Just in case.
405 AssemblerConf &config = _context.getConfig();
406 config.setIncrementIp(true);
407 config.setThrowExceptionOnError(true);
408
409 // Assembler the instruction.
410 _assembler.assemble(_context, instruction, _result);
411
412 // Store the chosen assembled instruction for future use.
413 const AssembledInstruction *assembledInstruction =
414 _result.getChosenInstruction();
415 if (assembledInstruction) {
416 _assembledInstructions.push_back(*assembledInstruction);
417 _codeLength += assembledInstruction->getCodeLength();
418 } else {
419 throw AssemblingFailedException(
420 FCML_TEXT("Chosen instruction hasn't been set. It seems "
421 "that the instruction chooser isn't working "
422 "correctly."));
423 }
424
425 return *this;
426 }
427
428 /**
429 * Gets iterator which allows to iterate through the whole machine
430 * code byte by byte.
431 *
432 * @return Iterator instance.
433 * @since 1.1.0
434 */
435 CodeIterator getCodeIterator() {
436 flush();
437 return CodeIterator(_assembledInstructions);
438 }
439
440 /**
441 * Gets all chosen assembled instructions.
442 *
443 * @return All assembled instructions available in the buffer.
444 * @since 1.1.0
445 */
446 std::vector<AssembledInstruction>& getAssembledInstructions() {
447 flush();
448 return _assembledInstructions;
449 }
450
451 /**
452 * Gets the code length of all assembled instructions available.
453 *
454 * @return The code length.
455 * @since 1.1.0
456 */
457 fcml_usize getCodeLength() {
458 flush();
459 return _codeLength;
460 }
461
462 /**
463 * Assembles all pending instructions.
464 * @since 1.1.0
465 * @throw AssemblingFailedException
466 */
467 void flush() {
468 if (_instructionBuilder.isNotNull()) {
469 // Build an instruction using the instruction builder.
470 Instruction instruction = _instructionBuilder.getValue().build();
471 // And clean the builder, is everything succeed.
472 _instructionBuilder.setNotNull(false);
473 // Assemble the instruction.
474 *this << instruction;
475 }
476 }
477
478 /**
479 * Creates flush indicated for "shift" operators.
480 * @return Flush indicator.
481 * @since 1.1.0
482 */
483 static SAFlush FLUSH() {
484 return SAFlush();
485 }
486
487 /** Gets configuration used by parser if parsing is supported.
488 * @return Parser configuration.
489 */
490 const ParserConfig& getParserConfig() const {
491 return _parserContext.getConfig();
492 }
493
494 /** Gets configuration used by parser if parsing is supported.
495 * @return Parser configuration.
496 * @since 1.1.0
497 */
498 ParserConfig& getParserConfig() {
499 return _parserContext.getConfig();
500 }
501
502 /**
503 * Gets symbol table used together with the parser.
504 * @return The symbol table used by the parser.
505 * @since 1.1.0
506 */
507 const SymbolTable* getSymbolTable() const {
508 return _parserContext.getSymbolTable();
509 }
510
511 /**
512 * Gets symbol table used together with the parser.
513 * @return The symbol table used by the parser.
514 * @since 1.1.0
515 */
516 SymbolTable* getSymbolTable() {
517 return _parserContext.getSymbolTable();
518 }
519
520 /**
521 * Sets a new symbol table for the parser.
522 * @param symbolTable The new symbol table.
523 * @since 1.1.0
524 */
525 void setSymbolTable(SymbolTable *symbolTable) {
526 _parserContext.setSymbolTable(symbolTable);
527 }
528
529 private:
530
531 /** An instruction parser. */
532 Parser *_parser;
533 /** Parser result used when parsing is supported. */
534 ParserResult _parserResult;
535 /** Parser context. */
536 ParserContext _parserContext;
537 /** A disassembled result used by the assembler when needed. */
538 AssemblerResult _result;
539 /** Currently used instruction builder. */
540 Nullable<IB> _instructionBuilder;
541 /* Assembler used to assemble code. */
542 Assembler &_assembler;
543 /** Assembler context. */
544 AssemblerContext &_context;
545 // Assembled instructions.
546 std::vector<AssembledInstruction> _assembledInstructions;
547 /** Length of the code assembled so far. */
548 fcml_usize _codeLength;
549
550 };
551
552 }
553
554 #endif /* FCML_STATEFUL_ASSEMBLER_HPP_ */