Add OpConstant

This commit is contained in:
ReinUsesLisp 2018-08-27 00:29:40 -03:00
parent f68dbb20fc
commit 48cbe695f0
8 changed files with 162 additions and 20 deletions

View File

@ -19,6 +19,7 @@ static const std::uint32_t GeneratorMagicNumber = 0;
static const std::uint32_t Undefined = UINT32_MAX; static const std::uint32_t Undefined = UINT32_MAX;
class Op; class Op;
class Operand;
class Module { class Module {
public: public:
@ -128,6 +129,9 @@ public:
/// Returns a false scalar constant. /// Returns a false scalar constant.
const Op* ConstantFalse(const Op* result_type); const Op* ConstantFalse(const Op* result_type);
/// Returns a numeric scalar constant.
const Op* Constant(const Op* result_type, Operand* literal);
// Function // Function
/// Emits a function. /// Emits a function.
@ -145,6 +149,14 @@ public:
/// Emits a return. It ends a block. /// Emits a return. It ends a block.
const Op* Return(); const Op* Return();
// Literals
static Operand* Literal(std::uint32_t value);
static Operand* Literal(std::uint64_t value);
static Operand* Literal(std::int32_t value);
static Operand* Literal(std::int64_t value);
static Operand* Literal(float value);
static Operand* Literal(double value);
private: private:
const Op* AddCode(Op* op); const Op* AddCode(Op* op);

View File

@ -7,6 +7,7 @@ add_library(sirit
stream.h stream.h
operand.cpp operand.cpp
operand.h operand.h
literal.cpp
common_types.h common_types.h
opcodes.h opcodes.h
opcodes/type.cpp opcodes/type.cpp

37
src/literal.cpp Normal file
View File

@ -0,0 +1,37 @@
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
* Lesser General Public License version 2.1 or any later version.
*/
#include "sirit/sirit.h"
#include "common_types.h"
#include "operand.h"
namespace Sirit {
Operand* Module::Literal(u32 value) {
return new LiteralNumber(value);
}
Operand* Module::Literal(u64 value) {
return new LiteralNumber(value);
}
Operand* Module::Literal(s32 value) {
return new LiteralNumber(value);
}
Operand* Module::Literal(s64 value) {
return new LiteralNumber(value);
}
Operand* Module::Literal(f32 value) {
return new LiteralNumber(value);
}
Operand* Module::Literal(f64 value) {
return new LiteralNumber(value);
}
} // namespace Sirit

View File

@ -13,7 +13,7 @@ namespace Sirit {
Op::Op(spv::Op opcode_, u32 id_, const Op* result_type_) Op::Op(spv::Op opcode_, u32 id_, const Op* result_type_)
: opcode(opcode_), id(id_), result_type(result_type_) { : opcode(opcode_), id(id_), result_type(result_type_) {
operand_type = OperandType::Ref; operand_type = OperandType::Op;
} }
Op::~Op() = default; Op::~Op() = default;
@ -69,7 +69,7 @@ void Op::Add(const Operand* operand) {
} }
void Op::Add(u32 integer) { void Op::Add(u32 integer) {
Add(new LiteralInteger(integer)); Add(new LiteralNumber(integer));
} }
void Op::Add(const std::string& string) { void Op::Add(const std::string& string) {

View File

@ -18,4 +18,10 @@ const Op* Module::ConstantFalse(const Op* result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type)); return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type));
} }
const Op* Module::Constant(const Op* result_type, Operand* literal) {
Op* op{new Op(spv::Op::OpConstant, bound, result_type)};
op->Add(literal);
return AddDeclaration(op);
}
} // namespace Sirit } // namespace Sirit

View File

@ -34,24 +34,79 @@ OperandType Operand::GetType() const {
return operand_type; return operand_type;
} }
LiteralInteger::LiteralInteger(u32 integer_) LiteralNumber::LiteralNumber() {
: integer(integer_) { operand_type = OperandType::Number;
operand_type = OperandType::Integer;
} }
LiteralInteger::~LiteralInteger() = default; LiteralNumber::LiteralNumber(u32 number)
: uint32(number), type(NumberType::U32) {
void LiteralInteger::Fetch(Stream& stream) const { LiteralNumber();
stream.Write(integer);
} }
u16 LiteralInteger::GetWordCount() const { LiteralNumber::LiteralNumber(s32 number)
return 1; : int32(number), type(NumberType::S32) {
LiteralNumber();
} }
bool LiteralInteger::operator==(const Operand& other) const { LiteralNumber::LiteralNumber(f32 number)
: float32(number), type(NumberType::F32) {
LiteralNumber();
}
LiteralNumber::LiteralNumber(u64 number)
: uint64(number), type(NumberType::U64) {
LiteralNumber();
}
LiteralNumber::LiteralNumber(s64 number)
: int64(number), type(NumberType::S64) {
LiteralNumber();
}
LiteralNumber::LiteralNumber(f64 number)
: float64(number), type(NumberType::F64) {
LiteralNumber();
}
LiteralNumber::~LiteralNumber() = default;
void LiteralNumber::Fetch(Stream& stream) const {
switch (type) {
case NumberType::S32:
case NumberType::U32:
case NumberType::F32:
stream.Write(uint32);
break;
case NumberType::S64:
case NumberType::U64:
case NumberType::F64:
stream.Write(uint64);
break;
default:
assert(0);
}
}
u16 LiteralNumber::GetWordCount() const {
switch (type) {
case NumberType::S32:
case NumberType::U32:
case NumberType::F32:
return 1;
case NumberType::S64:
case NumberType::U64:
case NumberType::F64:
return 2;
default:
assert(0);
return 0;
}
}
bool LiteralNumber::operator==(const Operand& other) const {
if (operand_type == other.GetType()) { if (operand_type == other.GetType()) {
return dynamic_cast<const LiteralInteger&>(other).integer == integer; const auto& o{dynamic_cast<const LiteralNumber&>(other)};
return o.type == type && o.raw == raw;
} }
return false; return false;
} }

View File

@ -12,8 +12,8 @@ namespace Sirit {
enum class OperandType { enum class OperandType {
Invalid, Invalid,
Ref, Op,
Integer, Number,
String String
}; };
@ -34,10 +34,15 @@ protected:
OperandType operand_type{}; OperandType operand_type{};
}; };
class LiteralInteger : public Operand { class LiteralNumber : public Operand {
public: public:
LiteralInteger(u32 integer); LiteralNumber(u32 number);
~LiteralInteger(); LiteralNumber(s32 number);
LiteralNumber(f32 number);
LiteralNumber(u64 number);
LiteralNumber(s64 number);
LiteralNumber(f64 number);
~LiteralNumber();
virtual void Fetch(Stream& stream) const; virtual void Fetch(Stream& stream) const;
virtual u16 GetWordCount() const; virtual u16 GetWordCount() const;
@ -45,7 +50,26 @@ public:
virtual bool operator==(const Operand& other) const; virtual bool operator==(const Operand& other) const;
private: private:
u32 integer; LiteralNumber();
enum class NumberType {
U32,
S32,
F32,
U64,
S64,
F64
} type;
union {
u64 raw{};
u32 uint32;
s32 int32;
u64 uint64;
s64 int64;
f32 float32;
f64 float64;
};
}; };
class LiteralString : public Operand { class LiteralString : public Operand {

View File

@ -41,6 +41,13 @@ public:
ConstantTrue(TypeBool()); ConstantTrue(TypeBool());
ConstantTrue(TypeBool()); ConstantTrue(TypeBool());
ConstantFalse(TypeBool()); ConstantFalse(TypeBool());
Constant(TypeFloat(64), Literal(6342.2));
Constant(TypeFloat(64), Literal(6342.21));
Constant(TypeFloat(32), Literal(6342.21f));
Constant(TypeFloat(16), Literal(30u));
Constant(TypeInt(32, false), Literal(30u));
Constant(TypeInt(16, false), Literal(30u));
Constant(TypeInt(8, false), Literal(30u));
auto main_type{TypeFunction(TypeVoid())}; auto main_type{TypeFunction(TypeVoid())};
auto main_func{Emit(Function(TypeVoid(), spv::FunctionControlMask::MaskNone, main_type))}; auto main_func{Emit(Function(TypeVoid(), spv::FunctionControlMask::MaskNone, main_type))};