This file is indexed.

/usr/include/fcml/fcml_stateful_assembler.hpp is in libfcml-dev 1.1.3-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * FCML - Free Code Manipulation Library.
 * Copyright (C) 2010-2015 Slawomir Wojtasiak
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/** @file fcml_stateful_assembler.hpp
 * Stateful FCML assembler implementation.
 *
 * @copyright Copyright (C) 2010-2015 Slawomir Wojtasiak. All rights reserved.
 * This project is released under the GNU Lesser General Public License.
 */

#ifndef FCML_STATEFUL_ASSEMBLER_HPP_
#define FCML_STATEFUL_ASSEMBLER_HPP_

#include <vector>

#include "fcml_assembler.hpp"
#include "fcml_parser.hpp"

namespace fcml {

/**
 * It's a stateful assembler which can be used to assemble instructions one by one on the fly.
 * By default is works with the general instruction models as well as with instruction builders,
 * but it can be also configured to parse whole statements using internally managed parser.
 * It holds it's own state, so it's not necessary to update instruction pointer etc while it's working.
 * Assembled instructions are placed inside a dedicated vector, but generated machine code is accessing
 * directly by the dedicated CodeIterator.
 *
 * @since 1.1.0
 * @remarks This class isn't thread-safe.
 */
class StatefulAssembler {
public:

    /** Used only to indicate the need of the flush operation. */
    class SAFlush {
    };

    /**
     * Creates a stateful assembler for the given assembler and assembler context. Bear in mind that
     * assembler and context are not copied in any way and have to be valid as long as the stateful assembler
     * in in use. Parsing support can be enabled optionally using the third parameter.
     *
     * @param assembler The assembled that will be used to assemble the code.
     * @param context The assembler context.
     * @param enableParser Enables parsing support.
     * @since 1.1.0
     */
    StatefulAssembler( Assembler &assembler, AssemblerContext &context, bool enableParser = false ) :
        _instructionBuilder(IB(FCML_TEXT(""))),
        _assembler(assembler),
        _context(context),
        _codeLength(0) {
        // Create parser if needed.
        _parser = enableParser ? new Parser( assembler.getDialect() ) : NULL;
    }

    /** Destructor.
     * @since 1.1.0
     */
    virtual ~StatefulAssembler() {
        if( _parser ) {
            delete _parser;
        }
    }

    /**
     * Adds instruction builder to the stateful assembler. Such a instruction
     * builder will be used then to assemble an instruction it represents.
     * Before any operation is performed, pending instruction is flushed if
     * there is any available.
     *
     * @param ib The instruction builder to be flushed.
     * @return The stateful assembler itself.
     * @throw AssemblingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const IB &ib) {
        return add( ib );
    }

    /**
     * Adds instruction builder to the stateful assembler. Such a instruction
     * builder will be used then to assemble an instruction it represents.
     * Before any operation is performed, pending instruction is flushed if
     * there is any available.
     *
     * @param ib The instruction builder to be flushed.
     * @return The stateful assembler itself.
     * @throw AssemblingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& add(const IB &ib) {
        flush();
        _instructionBuilder.setNotNull(true);
        _instructionBuilder.setValue(ib);
        return *this;
    }

    /**
     * Creates an instruction builder for the given mnemonic.
     *
     * @param mnemonic The instruction mnemonic.
     * @return The stateful assembler itself.
     * @throw AssemblingFailedException, ParsingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const fcml_cstring &mnemonic) {
        return inst( mnemonic );
    }

    /**
     * Creates an instruction builder for the given mnemonic.
     *
     * @param mnemonic The instruction mnemonic.
     * @return The stateful assembler itself.
     * @throw AssemblingFailedException, ParsingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& inst(const fcml_cstring &mnemonic) {
        flush();
        if( _parser ) {
            // Parse instruction and then pass it to the assembler.
            _parserContext.setIp( _context.getEntryPoint().getIP() );
            ParserConfig &config = _parserContext.getConfig();
            config.setThrowExceptionOnError(true);
            _parser->parse( _parserContext, mnemonic, _parserResult );
            *this << _parserResult.getInstruction();
        } else {
            // Parser is not available, so treat this string as a full instruction which
            // have to be parsed.
            _instructionBuilder.setNotNull(true);
            _instructionBuilder.setValue(IB(mnemonic));
        }
        return *this;
    }

    /**
     * Adds the new register operand to the instruction builder associated with the buffer.
     *
     * @param reg The register.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const Register &reg) {
        return op( Operand( reg ) );
    }

    /**
     * Adds the new immediate operand to the instruction builder associated with the buffer.
     *
     * @param imm The immediate value.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const Integer &imm) {
        return op( Operand( imm ) );
    }

    /**
     * Adds the new address operand to the instruction builder associated with the buffer.
     *
     * @param address The address.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const Address &address) {
        return op( Operand( address ) );
    }

    /**
     * Adds the new far pointer operand to the instruction builder associated with the buffer.
     *
     * @param pointer The far pointer.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const FarPointer &pointer) {
        return op( Operand( pointer ) );
    }

    /**
     * Adds an operand to the instruction builder associated with the buffer.
     *
     * @param operand The operand to be added.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const Operand &operand) {
        return op( operand );
    }

    /**
     * Adds an operand to the instruction builder associated with the buffer.
     *
     * @param operand The operand to be added..
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& op(const Operand &operand) {
        if( !_instructionBuilder.isNotNull() ) {
            throw IllegalStateException( FCML_TEXT( "No instruction builder available." ) );
        }
        IB &ib = _instructionBuilder.getValue();
        ib.op( operand );
        return *this;
    }

    /**
     * Adds the new register operand to the instruction builder associated with the buffer.
     *
     * @param reg The register.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& op(const Register &reg) {
        return op( Operand( reg ) );
    }

    /**
     * Adds the new immediate operand to the instruction builder associated with the buffer.
     *
     * @param imm The immediate value.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& op(const Integer &imm) {
        return op( Operand( imm ) );
    }

    /**
     * Adds the new address operand to the instruction builder associated with the buffer.
     *
     * @param address The address.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& op(const Address &address) {
        return op( Operand( address ) );
    }

    /**
     * Adds the new far pointer operand to the instruction builder associated with the buffer.
     *
     * @param pointer The far pointer.
     * @return The stateful assembler itself.
     * @throw IllegalStateException
     * @since 1.1.0
     */
    StatefulAssembler& op(const FarPointer &pointer) {
        return op( Operand( pointer ) );
    }

    /**
     * Flushes the instruction builder.
     * @param indic Flush indicator.
     * @return The stateful assembler itself.
     * @throw AssemblingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const SAFlush &indic) {
        flush();
        return *this;
    }

    /**
     * Assembles an instruction in the given instruction builder.
     *
     * @param instruction Instruction to be assembled.
     * @return Stateful assembler.
     * @throw AssemblingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const Instruction &instruction) {
        return inst( instruction );
    }

    /**
     * Adds a prefix to the instruction being built.
     * @param prefix The prefix to be added.
     * @return The instruction builder with the new prefix set for it.
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const InstructionPrefix &prefix) {
        return set(prefix);
    }

    /**
     * Adds an instruction level hint to the instruction being built.
     * @param hint The hint to be added.
     * @return The instruction builder with the new hint added to it.
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const InstructionHint &hint) {
        return set( hint );
    }

    /**
     * Adds an operand level hint to the instruction being built.
     * @param hint The hint to be added.
     * @return The instruction builder with the new hint added to it.
     * @since 1.1.0
     */
    StatefulAssembler& operator <<(const OperandHint &hint) {
       return set( hint );
    }

    /**
     * Adds a prefix to the instruction being built.
     * @param prefix The prefix to be added.
     * @return The instruction builder with the new prefix set for it.
     * @since 1.1.0
     */
    StatefulAssembler& set( const InstructionPrefix &prefix ) {
        if( !_instructionBuilder.isNotNull() ) {
            throw IllegalStateException( FCML_TEXT( "No instruction builder available." ) );
        }
        _instructionBuilder.getValue() << prefix;
        return *this;
    }

    /**
     * Adds an instruction level hint to the instruction being built.
     * @param hint The hint to be added.
     * @return The instruction builder with the new hint added to it.
     * @since 1.1.0
     */
    StatefulAssembler& set( const InstructionHint &hint ) {
        if( !_instructionBuilder.isNotNull() ) {
            throw IllegalStateException( FCML_TEXT( "No instruction builder available." ) );
        }
        _instructionBuilder.getValue() << hint;
        return *this;
    }

    /**
     * Adds an operand level hint to the instruction being built.
     * @param hint The hint to be added.
     * @return The instruction builder with the new hint added to it.
     * @since 1.1.0
     */
    StatefulAssembler& set( const OperandHint &hint ) {
        if( !_instructionBuilder.isNotNull() ) {
            throw IllegalStateException( FCML_TEXT( "No instruction builder available." ) );
        }
        _instructionBuilder.getValue() << hint;
        return *this;
    }

    /**
     * Assembles an instruction in the given instruction builder.
     *
     * @param instruction Instruction to be assembled.
     * @return Stateful assembler.
     * @throw AssemblingFailedException
     * @since 1.1.0
     */
    StatefulAssembler& inst(const Instruction &instruction) {

        // Flush pending instruction if there is any.
        flush();

        // Just in case.
        AssemblerConf& config = _context.getConfig();
        config.setIncrementIp( true );
        config.setThrowExceptionOnError( true );

        // Assembler the instruction.
        _assembler.assemble( _context, instruction, _result );

        // Store the chosen assembled instruction for future use.
        const AssembledInstruction *assembledInstruction = _result.getChosenInstruction();
        if( assembledInstruction ) {
            _assembledInstructions.push_back( *assembledInstruction );
            _codeLength += assembledInstruction->getCodeLength();
        } else {
            throw AssemblingFailedException( FCML_TEXT( "Chosen instruction hasn't been set. It seems that the instruction chooser isn't working correctly." ) );
        }

        return *this;
    }

    /**
     * Gets iterator which allows to iterate through the whole machine code byte by byte.
     *
     * @return Iterator instance.
     * @since 1.1.0
     */
    CodeIterator getCodeIterator() {
        flush();
        return CodeIterator( _assembledInstructions );
    }

    /**
     * Gets all chosen assembled instructions.
     *
     * @return All assembled instructions available in the buffer.
     * @since 1.1.0
     */
    std::vector<AssembledInstruction>& getAssembledInstructions() {
        flush();
        return _assembledInstructions;
    }

    /**
     * Gets the code length of all assembled instructions available.
     *
     * @return The code length.
     * @since 1.1.0
     */
    fcml_usize getCodeLength() {
        flush();
        return _codeLength;
    }

    /**
     * Assembles all pending instructions.
     * @since 1.1.0
     * @throw AssemblingFailedException
     */
    void flush() {
        if( _instructionBuilder.isNotNull() ) {
            // Build an instruction using the instruction builder.
            Instruction instruction = _instructionBuilder.getValue().build();
            // And clean the builder, is everything succeed.
            _instructionBuilder.setNotNull(false);
            // Assemble the instruction.
            *this << instruction;
        }
    }

    /**
     * Creates flush indicated for "shift" operators.
     * @return Flush indicator.
     * @since 1.1.0
     */
    static SAFlush FLUSH() {
        return SAFlush();
    }

    /** Gets configuration used by parser if parsing is supported.
     * @return Parser configuration.
     */
    const ParserConfig& getParserConfig() const {
        return _parserContext.getConfig();
    }

    /** Gets configuration used by parser if parsing is supported.
     * @return Parser configuration.
     * @since 1.1.0
     */
    ParserConfig& getParserConfig() {
        return _parserContext.getConfig();
    }

    /**
     * Gets symbol table used together with the parser.
     * @return The symbol table used by the parser.
     * @since 1.1.0
     */
    const SymbolTable* getSymbolTable() const {
        return _parserContext.getSymbolTable();
    }

    /**
     * Gets symbol table used together with the parser.
     * @return The symbol table used by the parser.
     * @since 1.1.0
     */
    SymbolTable* getSymbolTable() {
        return _parserContext.getSymbolTable();
    }

    /**
     * Sets a new symbol table for the parser.
     * @param symbolTable The new symbol table.
     * @since 1.1.0
     */
    void setSymbolTable( SymbolTable *symbolTable ) {
        _parserContext.setSymbolTable( symbolTable );
    }

private:

    /** An instruction parser. */
    Parser *_parser;
    /** Parser result used when parsing is supported. */
    ParserResult _parserResult;
    /** Parser context. */
    ParserContext _parserContext;
    /** A disassembled result used by the assembler when needed. */
    AssemblerResult _result;
    /** Currently used instruction builder. */
    Nullable<IB> _instructionBuilder;
    /* Assembler used to assemble code. */
    Assembler &_assembler;
    /** Assembler context. */
    AssemblerContext &_context;
    // Assembled instructions.
    std::vector<AssembledInstruction> _assembledInstructions;
    /** Length of the code assembled so far. */
    fcml_usize _codeLength;

};

}

#endif /* FCML_STATEFUL_ASSEMBLER_HPP_ */