Split literal files

This commit is contained in:
ReinUsesLisp 2018-08-28 04:16:52 -03:00
parent edfc77bbbe
commit 014c6ab586
9 changed files with 215 additions and 156 deletions

View File

@ -8,6 +8,10 @@ add_library(sirit
operand.cpp operand.cpp
operand.h operand.h
literal.cpp literal.cpp
lnumber.cpp
lnumber.h
lstring.cpp
lstring.h
common_types.h common_types.h
insts.h insts.h
insts/type.cpp insts/type.cpp

View File

@ -7,6 +7,7 @@
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include "common_types.h" #include "common_types.h"
#include "operand.h" #include "operand.h"
#include "lnumber.h"
namespace Sirit { namespace Sirit {

89
src/lnumber.cpp Normal file
View File

@ -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 <cassert>
#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<const LiteralNumber&>(other)};
return o.type == type && o.raw == raw;
}
return false;
}
} // namespace Sirit

52
src/lnumber.h Normal file
View File

@ -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

38
src/lstring.cpp Normal file
View File

@ -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<u8>(string[i]));
}
for (std::size_t i{}; i < 4 - (string.size() % 4); i++) {
stream.Write(static_cast<u8>(0));
}
}
u16 LiteralString::GetWordCount() const {
return static_cast<u16>(string.size() / 4 + 1);
}
bool LiteralString::operator==(const Operand& other) const {
if (operand_type == other.GetType()) {
return dynamic_cast<const LiteralString&>(other).string == string;
}
return false;
}
} // namespace Sirit

29
src/lstring.h Normal file
View File

@ -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 <string>
#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

View File

@ -8,6 +8,8 @@
#include "common_types.h" #include "common_types.h"
#include "operand.h" #include "operand.h"
#include "op.h" #include "op.h"
#include "lnumber.h"
#include "lstring.h"
namespace Sirit { namespace Sirit {

View File

@ -34,108 +34,4 @@ OperandType Operand::GetType() const {
return operand_type; 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<const LiteralNumber&>(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<u8>(string[i]));
}
for (std::size_t i{}; i < 4 - (string.size() % 4); i++) {
stream.Write(static_cast<u8>(0));
}
}
u16 LiteralString::GetWordCount() const {
return static_cast<u16>(string.size() / 4 + 1);
}
bool LiteralString::operator==(const Operand& other) const {
if (operand_type == other.GetType()) {
return dynamic_cast<const LiteralString&>(other).string == string;
}
return false;
}
} // namespace Sirit } // namespace Sirit

View File

@ -34,56 +34,4 @@ protected:
OperandType operand_type{}; 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 } // namespace Sirit