diff --git a/tests/CPP/rtti2/Makefile b/tests/CPP/rtti2/Makefile new file mode 100644 index 0000000..63e8b66 --- /dev/null +++ b/tests/CPP/rtti2/Makefile @@ -0,0 +1,50 @@ +TARGET = rtti2 + +TARGET_ARCH = riscv32 + +CC = riscv32-unknown-elf-g++ + +# compiling flags here +CFLAGS = -Wall -I. -O0 -static -march=rv32imac -mabi=ilp32 --specs=nosys.specs + +LINKER = riscv32-unknown-elf-g++ +# linking flags here +LDFLAGS = -I. -static +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.cpp) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) + +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) + $(LINKER) $(CFLAGS) $(LDFLAGS) $(LIBS) $(LIBDIR) $(OBJECTS) -o $@ + riscv32-unknown-elf-objdump -d $@ > dump + riscv32-unknown-elf-objcopy -Oihex $@ $(TARGET).hex + @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp + @echo "Compiling "$<" ..." + $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) *.hex dump + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/tests/CPP/rtti2/helper_functions.cpp b/tests/CPP/rtti2/helper_functions.cpp new file mode 100644 index 0000000..a137bfc --- /dev/null +++ b/tests/CPP/rtti2/helper_functions.cpp @@ -0,0 +1,51 @@ +#include +#include + + +#define TRACE (*(unsigned char *)0x40000000) +extern "C" { + +int _read(int file, char* ptr, int len) { + return 0; +} + +int _open(int fd) { + return 0; +} + +int _close(int fd){ + return 0; +} + +int _fstat_r(int fd) { + return 0; +} + +int _lseek_r(struct _reent *ptr, FILE *fp, long offset, int whence){ + return 0; +} + +int _isatty_r(struct _reent *ptr, int fd) { + return 0; +} + + +int _write(int file, const char *ptr, int len) { + int x; + + for (x = 0; x < len; x++) { + TRACE = *ptr++; + } + + return (len); +} + +int _getpid(void) { + return 0; +} + +void _kill(int pid) { + return; +} + +} diff --git a/tests/CPP/rtti2/rtti2 b/tests/CPP/rtti2/rtti2 new file mode 100755 index 0000000..dca794b Binary files /dev/null and b/tests/CPP/rtti2/rtti2 differ diff --git a/tests/CPP/rtti2/rtti2.cpp b/tests/CPP/rtti2/rtti2.cpp new file mode 100644 index 0000000..c03b7fc --- /dev/null +++ b/tests/CPP/rtti2/rtti2.cpp @@ -0,0 +1,42 @@ +/* + * RTTI example from https://www.tenouk.com/cpluscodesnippet/moreonruntimetypeinfo.html + */ + +// run time type information + +#include +#include + +using namespace std; + +class Base +{ +public: + virtual void funct(){} +}; + +class Derived:public Base{}; + +int main(void) +{ + Derived* Test1 = new Derived; + Base* Test2 = Test1; + + cout<<"The type name of Test1 is: "; + cout< dump + riscv32-unknown-elf-objcopy -Oihex $@ $(TARGET).hex + @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp + @echo "Compiling "$<" ..." + $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) *.hex dump + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/tests/CPP/rtti3/helper_functions.cpp b/tests/CPP/rtti3/helper_functions.cpp new file mode 100644 index 0000000..a137bfc --- /dev/null +++ b/tests/CPP/rtti3/helper_functions.cpp @@ -0,0 +1,51 @@ +#include +#include + + +#define TRACE (*(unsigned char *)0x40000000) +extern "C" { + +int _read(int file, char* ptr, int len) { + return 0; +} + +int _open(int fd) { + return 0; +} + +int _close(int fd){ + return 0; +} + +int _fstat_r(int fd) { + return 0; +} + +int _lseek_r(struct _reent *ptr, FILE *fp, long offset, int whence){ + return 0; +} + +int _isatty_r(struct _reent *ptr, int fd) { + return 0; +} + + +int _write(int file, const char *ptr, int len) { + int x; + + for (x = 0; x < len; x++) { + TRACE = *ptr++; + } + + return (len); +} + +int _getpid(void) { + return 0; +} + +void _kill(int pid) { + return; +} + +} diff --git a/tests/CPP/rtti3/rtti3 b/tests/CPP/rtti3/rtti3 new file mode 100755 index 0000000..9874b24 Binary files /dev/null and b/tests/CPP/rtti3/rtti3 differ diff --git a/tests/CPP/rtti3/rtti3.cpp b/tests/CPP/rtti3/rtti3.cpp new file mode 100644 index 0000000..64bd370 --- /dev/null +++ b/tests/CPP/rtti3/rtti3.cpp @@ -0,0 +1,26 @@ + +// dynamic_cast +#include +#include +using namespace std; + +class Base_Class { virtual void dummy() {} }; +class Derived_Class: public Base_Class { int a; }; + +int main () { + try { + Base_Class * ptr_a = new Derived_Class; + Base_Class * ptr_b = new Base_Class; + Derived_Class * ptr_c; + + ptr_c = dynamic_cast< Derived_Class *>(ptr_a); + if (ptr_c ==0) cout << "Null pointer on first type-cast" << endl; + + ptr_c = dynamic_cast< Derived_Class *>(ptr_b); + if (ptr_c ==0) cout << "Null pointer on second type-cast" << endl; + + } catch (exception& my_ex) {cout << "Exception: " << my_ex.what();} + + asm volatile ("ecall"); + return 0; +}