cpp rtti examples

This commit is contained in:
Màrius Montón 2020-06-11 10:23:20 +02:00
parent 7867d7c592
commit 03a228b020
8 changed files with 270 additions and 0 deletions

50
tests/CPP/rtti2/Makefile Normal file
View File

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

View File

@ -0,0 +1,51 @@
#include <string.h>
#include <stdio.h>
#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;
}
}

BIN
tests/CPP/rtti2/rtti2 Executable file

Binary file not shown.

42
tests/CPP/rtti2/rtti2.cpp Normal file
View File

@ -0,0 +1,42 @@
/*
* RTTI example from https://www.tenouk.com/cpluscodesnippet/moreonruntimetypeinfo.html
*/
// run time type information
#include <iostream>
#include <typeinfo>
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<<typeid(Test1).name()<<endl;
cout<<"The type name of *Test1 is: ";
cout<<typeid(*Test1).name()<<endl;
cout<<"The type name of Test2 is: ";
cout<<typeid(Test2).name()<<endl;
cout<<"The type name of *Test2 is: ";
cout<<typeid(*Test2).name()<<endl;
delete Test1;
asm volatile ("ecall");
return 0;
}

50
tests/CPP/rtti3/Makefile Normal file
View File

@ -0,0 +1,50 @@
TARGET = rtti3
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!"

View File

@ -0,0 +1,51 @@
#include <string.h>
#include <stdio.h>
#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;
}
}

BIN
tests/CPP/rtti3/rtti3 Executable file

Binary file not shown.

26
tests/CPP/rtti3/rtti3.cpp Normal file
View File

@ -0,0 +1,26 @@
// dynamic_cast
#include <iostream>
#include <exception>
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;
}