From 3ce342b97b6c22b7ce6dded7be7b5c6bf9edcc04 Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Mon, 19 Nov 2018 23:49:15 +0200 Subject: Support for doubles in the assembler --- spasm/src/asm/token.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'spasm/src/asm/token.cpp') diff --git a/spasm/src/asm/token.cpp b/spasm/src/asm/token.cpp index 4f298cf..3f8980d 100644 --- a/spasm/src/asm/token.cpp +++ b/spasm/src/asm/token.cpp @@ -22,20 +22,33 @@ Token::Token(Token::Token_type type, const char* end) : _type(type), _lineno(lineno) { - if (_type == Integer || _type == XInteger) + switch (_type) { - _value_int = strtol(start, NULL, 0); - } - else if (_type == Ident) - { - if (end) + case Integer: + case XInteger: + { + _value_int = strtol(start, NULL, 0); + break; + } + case FloatingPoint: { - _value_str = std::string(start, end); + _value_double = strtod(start, NULL); + break; } - else + case Ident: { - _value_str = std::string(start); + if (end) + { + _value_str = std::string(start, end); + } + else + { + _value_str = std::string(start); + } + break; } + default: + break; } } -- cgit v1.3.1-23-gd82d From 9468ccb2024e850bacfcddcabc5402968c29b956 Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Thu, 22 Nov 2018 14:17:21 +0200 Subject: Fixed linux build --- spasm/src/asm/bytecode.cpp | 21 +++++++++++++++++---- spasm/src/asm/bytecode.hpp | 6 +++--- spasm/src/asm/token.cpp | 2 +- spasm/src/asm/token.hpp | 6 +++--- 4 files changed, 24 insertions(+), 11 deletions(-) (limited to 'spasm/src/asm/token.cpp') diff --git a/spasm/src/asm/bytecode.cpp b/spasm/src/asm/bytecode.cpp index ad436ce..a4a1e6e 100644 --- a/spasm/src/asm/bytecode.cpp +++ b/spasm/src/asm/bytecode.cpp @@ -26,7 +26,20 @@ void Bytecode_File::push_opcode(Bytecode_Stream::Opcode_t opcode) push_byte(opcode); } -void Bytecode_File::push_integer(int number, int size) +union DoubleToBits +{ + double as_double; + int64_t as_bits; +}; + +int64_t to_bits(double d) +{ + DoubleToBits v; + v.as_double = d; + return v.as_bits; +}; + +void Bytecode_File::push_integer(int64_t number, int size) { for (int i = 0; i < size; ++i) push_byte((number >> (i << 3)) & 0xff); @@ -34,7 +47,7 @@ void Bytecode_File::push_integer(int number, int size) void Bytecode_File::push_double(double number) { - auto bits = *reinterpret_cast(&number); + auto bits = to_bits(number); for (size_t i = 0; i < sizeof(size_t); ++i) push_byte((bits >> (i << 3)) & 0xff); } @@ -49,7 +62,7 @@ void Bytecode_Memory::push_opcode(Bytecode_Stream::Opcode_t opcode) push_byte(opcode); } -void Bytecode_Memory::push_integer(int number, int size) +void Bytecode_Memory::push_integer(int64_t number, int size) { for (int i = 0; i < size; ++i) push_byte((number >> (i << 3)) & 0xff); @@ -57,7 +70,7 @@ void Bytecode_Memory::push_integer(int number, int size) void Bytecode_Memory::push_double(double number) { - auto bits = *reinterpret_cast(&number); + auto bits = to_bits(number); for (size_t i = 0; i < sizeof(size_t); ++i) push_byte((bits >> (i << 3)) & 0xff); } diff --git a/spasm/src/asm/bytecode.hpp b/spasm/src/asm/bytecode.hpp index 345b7a8..fb80122 100644 --- a/spasm/src/asm/bytecode.hpp +++ b/spasm/src/asm/bytecode.hpp @@ -18,7 +18,7 @@ class Bytecode_Stream virtual ~Bytecode_Stream(); virtual void push_opcode(Opcode_t) = 0; - virtual void push_integer(int, int size) = 0; + virtual void push_integer(int64_t, int size) = 0; virtual void push_double(double) = 0; virtual void push_location(size_t) = 0; virtual void set_location(size_t, size_t) = 0; @@ -31,7 +31,7 @@ class Bytecode_File : public Bytecode_Stream Bytecode_File(const std::string&); virtual void push_opcode(Opcode_t); - virtual void push_integer(int, int size); + virtual void push_integer(int64_t, int size) override; virtual void push_double(double) override; private: @@ -47,7 +47,7 @@ class Bytecode_Memory : public Bytecode_Stream { public: virtual void push_opcode(Opcode_t); - virtual void push_integer(int, int) override; + virtual void push_integer(int64_t, int) override; virtual void push_double(double) override; virtual void push_location(size_t); virtual void set_location(size_t, size_t); diff --git a/spasm/src/asm/token.cpp b/spasm/src/asm/token.cpp index 3f8980d..fa612e3 100644 --- a/spasm/src/asm/token.cpp +++ b/spasm/src/asm/token.cpp @@ -57,7 +57,7 @@ Token::Token_type Token::type() const return _type; } -int Token::value_int() const +int64_t Token::value_int() const { return _value_int; } diff --git a/spasm/src/asm/token.hpp b/spasm/src/asm/token.hpp index 6c64e89..8f18768 100644 --- a/spasm/src/asm/token.hpp +++ b/spasm/src/asm/token.hpp @@ -58,8 +58,8 @@ class Token size_t lineno() const; - int value_int() const; - void set_int(int v) { _value_int = v; } + int64_t value_int() const; + void set_int(int64_t v) { _value_int = v; } double value_double() const { return _value_double; } @@ -70,7 +70,7 @@ class Token size_t _lineno; - int _value_int; + int64_t _value_int; double _value_double; std::string _value_str; -- cgit v1.3.1-23-gd82d