Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl
diff options
context:
space:
mode:
Commit9a7a8a31e75d1b85274ef46ca29714270062809e (patch)
Author
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-14 19:47:37 +0200
Committer
 GitHub <noreply@github.com>
2018-11-14 19:47:37 +0200
Files587a120e78e7c0d50c381d8f38bf0665ed8733a7 /JSImpl
IPL-9a7a8a31e75d1b85274ef46ca29714270062809e.tar
IPL-9a7a8a31e75d1b85274ef46ca29714270062809e.tar.gz
IPL-9a7a8a31e75d1b85274ef46ca29714270062809e.zip
Parent990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675 (diff)
Parente7c2a14f3e424f1ef1109d7634ece820a058e862 (diff)
Merge pull request #45 from SofiaCPP/feature/spasm_improvements
Feature/spasm improvements
7 files changed, 246 insertions, 9 deletions
-rw-r--r--JSImpl/solution/JSImpl.sln1
-rw-r--r--JSImpl/solution/Makefile2
-rw-r--r--JSImpl/test/Test.make14
-rw-r--r--JSImpl/test/Test.vcxproj15
-rw-r--r--JSImpl/test/Test.vcxproj.filters1
-rw-r--r--JSImpl/test/genie.lua2
-rw-r--r--JSImpl/test/sprtTests.cpp220
diff --git a/JSImpl/solution/JSImpl.sln b/JSImpl/solution/JSImpl.sln
index d4acff7..9915cf9 100644
--- a/JSImpl/solution/JSImpl.sln
+++ b/JSImpl/solution/JSImpl.sln
@@ -3,6 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "..\test\Test.vcxproj", "{45DC8C7C-3113-8E0D-DAFF-7310C6150A0F}"
ProjectSection(ProjectDependencies) = postProject
+ {AE0A9E7C-9A41-9F0D-432E-85102F441B0F} = {AE0A9E7C-9A41-9F0D-432E-85102F441B0F}
{19EA680D-85FE-90BE-4E80-341EBA538DEF} = {19EA680D-85FE-90BE-4E80-341EBA538DEF}
{EC34880F-5849-B0C0-21CB-53208D9EACF1} = {EC34880F-5849-B0C0-21CB-53208D9EACF1}
{B686840F-229B-ACC0-EB1C-502057F0A8F1} = {B686840F-229B-ACC0-EB1C-502057F0A8F1}
diff --git a/JSImpl/solution/Makefile b/JSImpl/solution/Makefile
index 37c7a63..b920fc7 100644
--- a/JSImpl/solution/Makefile
+++ b/JSImpl/solution/Makefile
@@ -20,7 +20,7 @@ JSImpl: JSLib
@echo "==== Building JSImpl ($(config)) ===="
@${MAKE} --no-print-directory -C . -f JSImpl.make
-Test: JSLib gtest gmock gtest_main
+Test: sprt JSLib gtest gmock gtest_main
@echo "==== Building Test ($(config)) ===="
@${MAKE} --no-print-directory -C ../test -f Test.make
diff --git a/JSImpl/test/Test.make b/JSImpl/test/Test.make
index 4836496..4540d2d 100644
--- a/JSImpl/test/Test.make
+++ b/JSImpl/test/Test.make
@@ -47,7 +47,7 @@ ifeq ($(config),debug64)
TARGETDIR = ../build/bin/Debug
TARGET = $(TARGETDIR)/Test
DEFINES += -D_SCL_SECURE_NO_WARNINGS
- INCLUDES += -I"googletest/googletest/include" -I"googletest/googlemock/include" -I".."
+ INCLUDES += -I"googletest/googletest/include" -I"googletest/googlemock/include" -I".." -I"../../spasm/src"
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP -MP $(DEFINES) $(INCLUDES)
ALL_ASMFLAGS += $(ASMFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -m64
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -m64
@@ -56,7 +56,7 @@ ifeq ($(config),debug64)
ALL_OBJCPPFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -m64 -std=c++14
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
ALL_LDFLAGS += $(LDFLAGS) -L"../build/bin/Debug" -L"." -m64
- LDDEPS += ../build/bin/Debug/libJSLib.a ../build/bin/Debug/libgtest.a ../build/bin/Debug/libgmock.a ../build/bin/Debug/libgtest_main.a
+ LDDEPS += ../build/bin/Debug/libsprt.a ../build/bin/Debug/libJSLib.a ../build/bin/Debug/libgtest.a ../build/bin/Debug/libgmock.a ../build/bin/Debug/libgtest_main.a
LIBS += $(LDDEPS) -lpthread
EXTERNAL_LIBS +=
LINKOBJS = $(OBJECTS)
@@ -65,6 +65,7 @@ ifeq ($(config),debug64)
$(OBJDIR)/LexerTests.o \
$(OBJDIR)/ParserTests.o \
$(OBJDIR)/empty.o \
+ $(OBJDIR)/sprtTests.o \
define PREBUILDCMDS
endef
@@ -79,7 +80,7 @@ ifeq ($(config),release64)
TARGETDIR = ../build/bin/Release
TARGET = $(TARGETDIR)/Test
DEFINES += -D_SCL_SECURE_NO_WARNINGS
- INCLUDES += -I"googletest/googletest/include" -I"googletest/googlemock/include" -I".."
+ INCLUDES += -I"googletest/googletest/include" -I"googletest/googlemock/include" -I".." -I"../../spasm/src"
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP -MP $(DEFINES) $(INCLUDES)
ALL_ASMFLAGS += $(ASMFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -O3 -m64
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -O3 -m64
@@ -88,7 +89,7 @@ ifeq ($(config),release64)
ALL_OBJCPPFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -Werror -Wall -Wextra -g -O3 -m64 -std=c++14
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
ALL_LDFLAGS += $(LDFLAGS) -L"../build/bin/Release" -L"." -m64
- LDDEPS += ../build/bin/Release/libJSLib.a ../build/bin/Release/libgtest.a ../build/bin/Release/libgmock.a ../build/bin/Release/libgtest_main.a
+ LDDEPS += ../build/bin/Release/libsprt.a ../build/bin/Release/libJSLib.a ../build/bin/Release/libgtest.a ../build/bin/Release/libgmock.a ../build/bin/Release/libgtest_main.a
LIBS += $(LDDEPS) -lpthread
EXTERNAL_LIBS +=
LINKOBJS = $(OBJECTS)
@@ -97,6 +98,7 @@ ifeq ($(config),release64)
$(OBJDIR)/LexerTests.o \
$(OBJDIR)/ParserTests.o \
$(OBJDIR)/empty.o \
+ $(OBJDIR)/sprtTests.o \
define PREBUILDCMDS
endef
@@ -168,6 +170,10 @@ $(OBJDIR)/empty.o: empty.cpp $(GCH) $(MAKEFILE) | $(OBJDIR)/.
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -c "$<"
+$(OBJDIR)/sprtTests.o: sprtTests.cpp $(GCH) $(MAKEFILE) | $(OBJDIR)/.
+ @echo $(notdir $<)
+ $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -c "$<"
+
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(OBJDIR)/$(notdir $(PCH)).d
diff --git a/JSImpl/test/Test.vcxproj b/JSImpl/test/Test.vcxproj
index 6bf7d68..3a7058a 100644
--- a/JSImpl/test/Test.vcxproj
+++ b/JSImpl/test/Test.vcxproj
@@ -21,11 +21,13 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
+ <PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
+ <PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
@@ -55,7 +57,7 @@
<ClCompile>
<AdditionalOptions> %(AdditionalOptions)</AdditionalOptions>
<Optimization>Disabled</Optimization>
- <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;..\..\spasm\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>false</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
@@ -70,7 +72,7 @@
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;..\..\spasm\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -85,7 +87,7 @@
<ClCompile>
<AdditionalOptions> %(AdditionalOptions)</AdditionalOptions>
<Optimization>MaxSpeed</Optimization>
- <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;..\..\spasm\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>false</MinimalRebuild>
<StringPooling>true</StringPooling>
@@ -100,7 +102,7 @@
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>googletest\googletest\include;googletest\googlemock\include;..;..\..\spasm\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -120,8 +122,13 @@
</ClCompile>
<ClCompile Include="ParserTests.cpp">
</ClCompile>
+ <ClCompile Include="sprtTests.cpp">
+ </ClCompile>
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\..\spasm\solution\sprt.vcxproj">
+ <Project>{AE0A9E7C-9A41-9F0D-432E-85102F441B0F}</Project>
+ </ProjectReference>
<ProjectReference Include="..\solution\JSLib.vcxproj">
<Project>{19EA680D-85FE-90BE-4E80-341EBA538DEF}</Project>
</ProjectReference>
diff --git a/JSImpl/test/Test.vcxproj.filters b/JSImpl/test/Test.vcxproj.filters
index e1968c9..b7e0456 100644
--- a/JSImpl/test/Test.vcxproj.filters
+++ b/JSImpl/test/Test.vcxproj.filters
@@ -4,5 +4,6 @@
<ClCompile Include="empty.cpp" />
<ClCompile Include="LexerTests.cpp" />
<ClCompile Include="ParserTests.cpp" />
+ <ClCompile Include="sprtTests.cpp" />
</ItemGroup>
</Project>
diff --git a/JSImpl/test/genie.lua b/JSImpl/test/genie.lua
index 262ed18..ba0f0c2 100644
--- a/JSImpl/test/genie.lua
+++ b/JSImpl/test/genie.lua
@@ -9,8 +9,10 @@ project 'Test'
'googletest/googletest/include',
'googletest/googlemock/include',
'..',
+ '../../spasm/src',
}
links {
+ 'sprt',
'JSLib',
'gtest',
'gmock',
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp
new file mode 100644
index 0000000..7621ca3
--- /dev/null
+++ b/JSImpl/test/sprtTests.cpp
@@ -0,0 +1,220 @@
+#include <gtest/gtest.h>
+
+#include <spasm.hpp>
+#include <sstream>
+
+using Spasm::OpCodes;
+
+struct SPRTTest : public ::testing::Test
+{
+ typedef std::vector<Spasm::byte> ByteCode;
+
+ void Run(ByteCode& code)
+ {
+ Run(code.data(), code.size());
+ }
+
+ void Run(Spasm::byte* bytecode, size_t size)
+ {
+ VM.Initialize(size, bytecode, Input, Output);
+ ASSERT_EQ(Spasm::Spasm::RunResult::Success, VM.run());
+ }
+ Spasm::byte Label(Spasm::byte label) const {
+ return 0x80 + label;
+ }
+ Spasm::Spasm VM;
+ std::istringstream Input;
+ std::ostringstream Output;
+};
+
+TEST_F(SPRTTest, Empty)
+{
+}
+
+TEST_F(SPRTTest, Add)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Add, 3, 1, 2,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "13");
+}
+
+TEST_F(SPRTTest, Sub)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Sub, 3, 1, 2,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "-1");
+}
+
+TEST_F(SPRTTest, Div)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Div, 3, 1, 2,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "0");
+}
+
+TEST_F(SPRTTest, Mod)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Mod, 3, 1, 2,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "6");
+}
+
+TEST_F(SPRTTest, Mutliply)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Mul, 3, 1, 2,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "42");
+}
+
+TEST_F(SPRTTest, Less)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::Less, 3, 1, 2,
+ OpCodes::Less, 4, 2, 1,
+ OpCodes::Less, 5, 2, 2,
+ OpCodes::Print, 3,
+ OpCodes::Print, 4,
+ OpCodes::Print, 5,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "100");
+}
+
+
+TEST_F(SPRTTest, LessEq)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::LessEq, 3, 1, 2,
+ OpCodes::LessEq, 4, 2, 1,
+ OpCodes::LessEq, 5, 2, 2,
+ OpCodes::Print, 3,
+ OpCodes::Print, 4,
+ OpCodes::Print, 5,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "101");
+}
+
+TEST_F(SPRTTest, Jump)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6,
+ OpCodes::Const, 2, 7,
+ OpCodes::LessEq, 3, 1, 2,
+ OpCodes::LessEq, 4, 2, 1,
+ OpCodes::Jump, 18,
+ OpCodes::Print, 5,
+ OpCodes::Print, 3,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "1");
+}
+
+TEST_F(SPRTTest, JumpTrue)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6, // 3
+ OpCodes::Const, 2, 7, // 6
+ OpCodes::Less, 3, 1, 2, // 10
+ OpCodes::JumpT, 3, 17, // 13
+ OpCodes::Print, 2, // 15
+ OpCodes::Jump, 19, // 17
+ OpCodes::Print, 1, // 19
+ OpCodes::Halt,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "6");
+}
+
+TEST_F(SPRTTest, JumpFalse)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Const, 1, 6, // 3
+ OpCodes::Const, 2, 7, // 6
+ OpCodes::Less, 3, 1, 2, // 10
+ OpCodes::JumpF, 3, 17, // 13
+ OpCodes::Print, 2, // 15
+ OpCodes::Jump, 19, // 17
+ OpCodes::Print, 1, // 19
+ OpCodes::Halt,
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "7");
+}
+
+TEST_F(SPRTTest, Call)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Alloc, 5, // 2
+ OpCodes::Const, 1, 0, // 5
+ OpCodes::Const, 2, 6, // 8
+ OpCodes::Const, 3, 7, // 11
+ OpCodes::Const, 4, 2, // 14
+ OpCodes::Push, 3, // 16
+ OpCodes::Push, 2, // 18
+ OpCodes::Push, 4, // 20
+ OpCodes::Call, 25, // 22
+ OpCodes::Print, 4, // 24
+ OpCodes::Halt, // 25
+ OpCodes::Print, 0, // 27
+ OpCodes::Print, -1, // 29
+ OpCodes::Print, -2, // 31
+ OpCodes::Mul, 1, -2, -1,// 33
+ OpCodes::Ret, 1, // 35
+ };
+
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "26742");
+}
+
+TEST_F(SPRTTest, Read)
+{
+ Spasm::byte bytecode[] = {
+ OpCodes::Read, 1,
+ OpCodes::Print, 1,
+ };
+
+ Input.str("42");
+ Run(bytecode, sizeof(bytecode));
+ ASSERT_EQ(Output.str(), "42");
+}
+