2016-08-17 15:53:36 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
#include <initializer_list>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <memory>
|
2018-10-14 22:17:56 +01:00
|
|
|
#include <optional>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/intrusive_list.h"
|
|
|
|
#include "common/memory_pool.h"
|
2018-01-01 15:23:56 +00:00
|
|
|
#include "frontend/ir/cond.h"
|
2016-09-05 11:54:09 +01:00
|
|
|
#include "frontend/ir/location_descriptor.h"
|
2016-08-17 15:53:36 +01:00
|
|
|
#include "frontend/ir/microinstruction.h"
|
|
|
|
#include "frontend/ir/terminal.h"
|
2016-08-25 15:35:50 +01:00
|
|
|
#include "frontend/ir/value.h"
|
2016-08-17 15:53:36 +01:00
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::IR {
|
2016-08-17 15:53:36 +01:00
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
enum class Opcode;
|
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
/**
|
|
|
|
* A basic block. It consists of zero or more instructions followed by exactly one terminal.
|
|
|
|
* Note that this is a linear IR and not a pure tree-based IR: i.e.: there is an ordering to
|
|
|
|
* the microinstructions. This only matters before chaining is done in order to correctly
|
|
|
|
* order memory accesses.
|
|
|
|
*/
|
|
|
|
class Block final {
|
|
|
|
public:
|
basic_block: Add proxy member functions for the instruction list
Currently basic block kind of acts like a 'dumb struct' which makes things
a little more verbose to write (as opposed to keeping it all in one place,
I guess). It's also a little wonky conceptually, considering a block is
composed of instructions (i.e. 'contains' them).
So providing accessors that make it act more like a container can make working
with algorithms a little nicer. It also makes the API a little more
defined.
Ideally, the list would be only available through a function, but
currently, the pool allocator is exposed, which seems somewhat odd,
considering the block itself should manage its overall allocations
(with placement new, and regular new), rather than putting that
sanitizing directly on the IR emitter (it should just care about emission,
not block state). However, recontaining that can be followed up with,
as it's very trivial to do.
2016-08-21 17:35:30 +01:00
|
|
|
using InstructionList = Common::IntrusiveList<Inst>;
|
2016-08-23 18:05:00 +01:00
|
|
|
using size_type = InstructionList::size_type;
|
basic_block: Add proxy member functions for the instruction list
Currently basic block kind of acts like a 'dumb struct' which makes things
a little more verbose to write (as opposed to keeping it all in one place,
I guess). It's also a little wonky conceptually, considering a block is
composed of instructions (i.e. 'contains' them).
So providing accessors that make it act more like a container can make working
with algorithms a little nicer. It also makes the API a little more
defined.
Ideally, the list would be only available through a function, but
currently, the pool allocator is exposed, which seems somewhat odd,
considering the block itself should manage its overall allocations
(with placement new, and regular new), rather than putting that
sanitizing directly on the IR emitter (it should just care about emission,
not block state). However, recontaining that can be followed up with,
as it's very trivial to do.
2016-08-21 17:35:30 +01:00
|
|
|
using iterator = InstructionList::iterator;
|
|
|
|
using const_iterator = InstructionList::const_iterator;
|
|
|
|
using reverse_iterator = InstructionList::reverse_iterator;
|
|
|
|
using const_reverse_iterator = InstructionList::const_reverse_iterator;
|
|
|
|
|
2017-02-16 18:18:29 +00:00
|
|
|
explicit Block(const LocationDescriptor& location)
|
|
|
|
: location(location), end_location(location) {}
|
2016-08-17 15:53:36 +01:00
|
|
|
|
basic_block: Add proxy member functions for the instruction list
Currently basic block kind of acts like a 'dumb struct' which makes things
a little more verbose to write (as opposed to keeping it all in one place,
I guess). It's also a little wonky conceptually, considering a block is
composed of instructions (i.e. 'contains' them).
So providing accessors that make it act more like a container can make working
with algorithms a little nicer. It also makes the API a little more
defined.
Ideally, the list would be only available through a function, but
currently, the pool allocator is exposed, which seems somewhat odd,
considering the block itself should manage its overall allocations
(with placement new, and regular new), rather than putting that
sanitizing directly on the IR emitter (it should just care about emission,
not block state). However, recontaining that can be followed up with,
as it's very trivial to do.
2016-08-21 17:35:30 +01:00
|
|
|
bool empty() const { return instructions.empty(); }
|
2016-08-23 18:05:00 +01:00
|
|
|
size_type size() const { return instructions.size(); }
|
basic_block: Add proxy member functions for the instruction list
Currently basic block kind of acts like a 'dumb struct' which makes things
a little more verbose to write (as opposed to keeping it all in one place,
I guess). It's also a little wonky conceptually, considering a block is
composed of instructions (i.e. 'contains' them).
So providing accessors that make it act more like a container can make working
with algorithms a little nicer. It also makes the API a little more
defined.
Ideally, the list would be only available through a function, but
currently, the pool allocator is exposed, which seems somewhat odd,
considering the block itself should manage its overall allocations
(with placement new, and regular new), rather than putting that
sanitizing directly on the IR emitter (it should just care about emission,
not block state). However, recontaining that can be followed up with,
as it's very trivial to do.
2016-08-21 17:35:30 +01:00
|
|
|
|
|
|
|
Inst& front() { return instructions.front(); }
|
|
|
|
const Inst& front() const { return instructions.front(); }
|
|
|
|
|
|
|
|
Inst& back() { return instructions.back(); }
|
|
|
|
const Inst& back() const { return instructions.back(); }
|
|
|
|
|
|
|
|
iterator begin() { return instructions.begin(); }
|
|
|
|
const_iterator begin() const { return instructions.begin(); }
|
|
|
|
iterator end() { return instructions.end(); }
|
|
|
|
const_iterator end() const { return instructions.end(); }
|
|
|
|
|
|
|
|
reverse_iterator rbegin() { return instructions.rbegin(); }
|
|
|
|
const_reverse_iterator rbegin() const { return instructions.rbegin(); }
|
|
|
|
reverse_iterator rend() { return instructions.rend(); }
|
|
|
|
const_reverse_iterator rend() const { return instructions.rend(); }
|
|
|
|
|
|
|
|
const_iterator cbegin() const { return instructions.cbegin(); }
|
|
|
|
const_iterator cend() const { return instructions.cend(); }
|
|
|
|
|
|
|
|
const_reverse_iterator crbegin() const { return instructions.crbegin(); }
|
|
|
|
const_reverse_iterator crend() const { return instructions.crend(); }
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
/**
|
2018-01-26 23:30:17 +00:00
|
|
|
* Appends a new instruction to the end of this basic block,
|
2016-08-25 15:35:50 +01:00
|
|
|
* handling any allocations necessary to do so.
|
|
|
|
*
|
|
|
|
* @param op Opcode representing the instruction to add.
|
|
|
|
* @param args A sequence of Value instances used as arguments for the instruction.
|
|
|
|
*/
|
|
|
|
void AppendNewInst(Opcode op, std::initializer_list<Value> args);
|
|
|
|
|
2018-01-26 23:30:17 +00:00
|
|
|
/**
|
|
|
|
* Prepends a new instruction to this basic block before the insertion point,
|
|
|
|
* handling any allocations necessary to do so.
|
|
|
|
*
|
|
|
|
* @param insertion_point Where to insert the new instruction.
|
|
|
|
* @param op Opcode representing the instruction to add.
|
|
|
|
* @param args A sequence of Value instances used as arguments for the instruction.
|
|
|
|
* @returns Iterator to the newly created instruction.
|
|
|
|
*/
|
|
|
|
iterator PrependNewInst(iterator insertion_point, Opcode op, std::initializer_list<Value> args);
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Gets the starting location for this basic block.
|
2016-09-05 11:54:09 +01:00
|
|
|
LocationDescriptor Location() const;
|
2017-02-16 18:18:29 +00:00
|
|
|
/// Gets the end location for this basic block.
|
|
|
|
LocationDescriptor EndLocation() const;
|
|
|
|
/// Sets the end location for this basic block.
|
|
|
|
void SetEndLocation(const LocationDescriptor& descriptor);
|
2016-08-25 15:35:50 +01:00
|
|
|
|
|
|
|
/// Gets the condition required to pass in order to execute this block.
|
2018-01-01 15:23:56 +00:00
|
|
|
Cond GetCondition() const;
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Sets the condition required to pass in order to execute this block.
|
2018-01-01 15:23:56 +00:00
|
|
|
void SetCondition(Cond condition);
|
2016-08-25 15:35:50 +01:00
|
|
|
|
|
|
|
/// Gets the location of the block to execute if the predicated condition fails.
|
2016-09-05 11:54:09 +01:00
|
|
|
LocationDescriptor ConditionFailedLocation() const;
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Sets the location of the block to execute if the predicated condition fails.
|
2016-09-05 11:54:09 +01:00
|
|
|
void SetConditionFailedLocation(LocationDescriptor fail_location);
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Determines whether or not a prediated condition failure block is present.
|
|
|
|
bool HasConditionFailedLocation() const;
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the condition failed cycle count.
|
|
|
|
size_t& ConditionFailedCycleCount();
|
|
|
|
/// Gets an immutable reference to the condition failed cycle count.
|
|
|
|
const size_t& ConditionFailedCycleCount() const;
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the instruction list for this basic block.
|
|
|
|
InstructionList& Instructions();
|
|
|
|
/// Gets an immutable reference to the instruction list for this basic block.
|
|
|
|
const InstructionList& Instructions() const;
|
|
|
|
|
|
|
|
/// Gets the terminal instruction for this basic block.
|
2018-02-03 03:04:44 +00:00
|
|
|
Terminal GetTerminal() const;
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Sets the terminal instruction for this basic block.
|
|
|
|
void SetTerminal(Terminal term);
|
2018-01-13 21:51:13 +00:00
|
|
|
/// Replaces the terminal instruction for this basic block.
|
|
|
|
void ReplaceTerminal(Terminal term);
|
2016-08-25 15:35:50 +01:00
|
|
|
/// Determines whether or not this basic block has a terminal instruction.
|
|
|
|
bool HasTerminal() const;
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the cycle count for this basic block.
|
|
|
|
size_t& CycleCount();
|
|
|
|
/// Gets an immutable reference to the cycle count for this basic block.
|
|
|
|
const size_t& CycleCount() const;
|
|
|
|
|
|
|
|
private:
|
2016-08-17 15:53:36 +01:00
|
|
|
/// Description of the starting location of this block
|
2016-09-05 11:54:09 +01:00
|
|
|
LocationDescriptor location;
|
2017-02-16 18:18:29 +00:00
|
|
|
/// Description of the end location of this block
|
|
|
|
LocationDescriptor end_location;
|
2016-08-17 15:53:36 +01:00
|
|
|
/// Conditional to pass in order to execute this block
|
2018-01-01 15:23:56 +00:00
|
|
|
Cond cond = Cond::AL;
|
2016-08-17 15:53:36 +01:00
|
|
|
/// Block to execute next if `cond` did not pass.
|
2018-10-14 22:17:56 +01:00
|
|
|
std::optional<LocationDescriptor> cond_failed = {};
|
2016-08-25 03:18:27 +01:00
|
|
|
/// Number of cycles this block takes to execute if the conditional fails.
|
|
|
|
size_t cond_failed_cycle_count = 0;
|
2016-08-17 15:53:36 +01:00
|
|
|
|
|
|
|
/// List of instructions in this block.
|
basic_block: Add proxy member functions for the instruction list
Currently basic block kind of acts like a 'dumb struct' which makes things
a little more verbose to write (as opposed to keeping it all in one place,
I guess). It's also a little wonky conceptually, considering a block is
composed of instructions (i.e. 'contains' them).
So providing accessors that make it act more like a container can make working
with algorithms a little nicer. It also makes the API a little more
defined.
Ideally, the list would be only available through a function, but
currently, the pool allocator is exposed, which seems somewhat odd,
considering the block itself should manage its overall allocations
(with placement new, and regular new), rather than putting that
sanitizing directly on the IR emitter (it should just care about emission,
not block state). However, recontaining that can be followed up with,
as it's very trivial to do.
2016-08-21 17:35:30 +01:00
|
|
|
InstructionList instructions;
|
2016-08-17 15:53:36 +01:00
|
|
|
/// Memory pool for instruction list
|
|
|
|
std::unique_ptr<Common::Pool> instruction_alloc_pool = std::make_unique<Common::Pool>(sizeof(Inst), 4096);
|
|
|
|
/// Terminal instruction of this block.
|
|
|
|
Terminal terminal = Term::Invalid{};
|
|
|
|
|
|
|
|
/// Number of cycles this block takes to execute.
|
|
|
|
size_t cycle_count = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns a string representation of the contents of block. Intended for debugging.
|
|
|
|
std::string DumpBlock(const IR::Block& block);
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::IR
|