From 014c6ab5869ce30bb3b0d116b9c204d972fbd57a Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 28 Aug 2018 04:16:52 -0300 Subject: [PATCH] Split literal files --- src/CMakeLists.txt | 4 ++ src/literal.cpp | 1 + src/lnumber.cpp | 89 ++++++++++++++++++++++++++++++++++++++ src/lnumber.h | 52 +++++++++++++++++++++++ src/lstring.cpp | 38 +++++++++++++++++ src/lstring.h | 29 +++++++++++++ src/op.cpp | 2 + src/operand.cpp | 104 --------------------------------------------- src/operand.h | 52 ----------------------- 9 files changed, 215 insertions(+), 156 deletions(-) create mode 100644 src/lnumber.cpp create mode 100644 src/lnumber.h create mode 100644 src/lstring.cpp create mode 100644 src/lstring.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 92540a3..dca3b91 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,10 @@ add_library(sirit operand.cpp operand.h literal.cpp + lnumber.cpp + lnumber.h + lstring.cpp + lstring.h common_types.h insts.h insts/type.cpp diff --git a/src/literal.cpp b/src/literal.cpp index 79da059..a36824c 100644 --- a/src/literal.cpp +++ b/src/literal.cpp @@ -7,6 +7,7 @@ #include "sirit/sirit.h" #include "common_types.h" #include "operand.h" +#include "lnumber.h" namespace Sirit { diff --git a/src/lnumber.cpp b/src/lnumber.cpp new file mode 100644 index 0000000..9c347b0 --- /dev/null +++ b/src/lnumber.cpp @@ -0,0 +1,89 @@ +/* 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 +#include "lnumber.h" + +namespace Sirit { + +LiteralNumber::LiteralNumber() { + operand_type = OperandType::Number; +} + +LiteralNumber::LiteralNumber(u32 number) + : uint32(number), type(NumberType::U32) { + LiteralNumber(); +} + +LiteralNumber::LiteralNumber(s32 number) + : int32(number), type(NumberType::S32) { + LiteralNumber(); +} + +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()) { + const auto& o{dynamic_cast(other)}; + return o.type == type && o.raw == raw; + } + return false; +} + +} // namespace Sirit diff --git a/src/lnumber.h b/src/lnumber.h new file mode 100644 index 0000000..6b86504 --- /dev/null +++ b/src/lnumber.h @@ -0,0 +1,52 @@ +/* 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. + */ + +#pragma once + +#include "stream.h" +#include "operand.h" + +namespace Sirit { + +class LiteralNumber : public Operand { +public: + LiteralNumber(u32 number); + LiteralNumber(s32 number); + LiteralNumber(f32 number); + LiteralNumber(u64 number); + LiteralNumber(s64 number); + LiteralNumber(f64 number); + ~LiteralNumber(); + + virtual void Fetch(Stream& stream) const; + virtual u16 GetWordCount() const; + + virtual bool operator==(const Operand& other) const; + +private: + 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; + }; +}; + +} // namespace Sirit diff --git a/src/lstring.cpp b/src/lstring.cpp new file mode 100644 index 0000000..d15d933 --- /dev/null +++ b/src/lstring.cpp @@ -0,0 +1,38 @@ +/* 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 "lstring.h" + +namespace Sirit { + +LiteralString::LiteralString(const std::string& string_) + : string(string_) { + operand_type = OperandType::String; +} + +LiteralString::~LiteralString() = default; + +void LiteralString::Fetch(Stream& stream) const { + for (std::size_t i{}; i < string.size(); i++) { + stream.Write(static_cast(string[i])); + } + for (std::size_t i{}; i < 4 - (string.size() % 4); i++) { + stream.Write(static_cast(0)); + } +} + +u16 LiteralString::GetWordCount() const { + return static_cast(string.size() / 4 + 1); +} + +bool LiteralString::operator==(const Operand& other) const { + if (operand_type == other.GetType()) { + return dynamic_cast(other).string == string; + } + return false; +} + +} // namespace Sirit diff --git a/src/lstring.h b/src/lstring.h new file mode 100644 index 0000000..ea54e23 --- /dev/null +++ b/src/lstring.h @@ -0,0 +1,29 @@ +/* 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. + */ + +#pragma once + +#include +#include "stream.h" +#include "operand.h" + +namespace Sirit { + +class LiteralString : public Operand { +public: + LiteralString(const std::string& string); + ~LiteralString(); + + virtual void Fetch(Stream& stream) const; + virtual u16 GetWordCount() const; + + virtual bool operator==(const Operand& other) const; + +private: + std::string string; +}; + +} // namespace Sirit diff --git a/src/op.cpp b/src/op.cpp index a8d5d2a..40ce310 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -8,6 +8,8 @@ #include "common_types.h" #include "operand.h" #include "op.h" +#include "lnumber.h" +#include "lstring.h" namespace Sirit { diff --git a/src/operand.cpp b/src/operand.cpp index 6123e9d..b2ed322 100644 --- a/src/operand.cpp +++ b/src/operand.cpp @@ -34,108 +34,4 @@ OperandType Operand::GetType() const { return operand_type; } -LiteralNumber::LiteralNumber() { - operand_type = OperandType::Number; -} - -LiteralNumber::LiteralNumber(u32 number) - : uint32(number), type(NumberType::U32) { - LiteralNumber(); -} - -LiteralNumber::LiteralNumber(s32 number) - : int32(number), type(NumberType::S32) { - LiteralNumber(); -} - -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()) { - const auto& o{dynamic_cast(other)}; - return o.type == type && o.raw == raw; - } - return false; -} - -LiteralString::LiteralString(const std::string& string_) - : string(string_) { - operand_type = OperandType::String; -} - -LiteralString::~LiteralString() = default; - -void LiteralString::Fetch(Stream& stream) const { - for (std::size_t i{}; i < string.size(); i++) { - stream.Write(static_cast(string[i])); - } - for (std::size_t i{}; i < 4 - (string.size() % 4); i++) { - stream.Write(static_cast(0)); - } -} - -u16 LiteralString::GetWordCount() const { - return static_cast(string.size() / 4 + 1); -} - -bool LiteralString::operator==(const Operand& other) const { - if (operand_type == other.GetType()) { - return dynamic_cast(other).string == string; - } - return false; -} - } // namespace Sirit diff --git a/src/operand.h b/src/operand.h index 6f2765e..2583f76 100644 --- a/src/operand.h +++ b/src/operand.h @@ -34,56 +34,4 @@ protected: OperandType operand_type{}; }; -class LiteralNumber : public Operand { -public: - LiteralNumber(u32 number); - LiteralNumber(s32 number); - LiteralNumber(f32 number); - LiteralNumber(u64 number); - LiteralNumber(s64 number); - LiteralNumber(f64 number); - ~LiteralNumber(); - - virtual void Fetch(Stream& stream) const; - virtual u16 GetWordCount() const; - - virtual bool operator==(const Operand& other) const; - -private: - 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 { -public: - LiteralString(const std::string& string); - ~LiteralString(); - - virtual void Fetch(Stream& stream) const; - virtual u16 GetWordCount() const; - - virtual bool operator==(const Operand& other) const; - -private: - std::string string; -}; - } // namespace Sirit