From eafc7c5d949e61479ff8d03d6543e7da42ddc7a6 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Sat, 7 Mar 2015 14:03:54 +0100 Subject: [PATCH] Prototypes and lookup for standard functions. --- Nodes.cpp | 5 ++- RuntimePrototypes.hpp | 80 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 RuntimePrototypes.hpp diff --git a/Nodes.cpp b/Nodes.cpp index 5a39d37..907de41 100644 --- a/Nodes.cpp +++ b/Nodes.cpp @@ -18,17 +18,16 @@ */ #include "Nodes.hpp" +#include "RuntimePrototypes.hpp" #include using namespace monicelli; -static const std::string MONICELLI_ENTRYPOINT = "__Monicelli_main"; - Function *monicelli::makeMain(PointerList *body) { PointerList *noargs = new PointerList(); return new Function( - new Id(new std::string(MONICELLI_ENTRYPOINT)), + new Id(new std::string(ENTRYPOINT_NAME)), Type::VOID, noargs, body ); } diff --git a/RuntimePrototypes.hpp b/RuntimePrototypes.hpp new file mode 100644 index 0000000..ff3b1ef --- /dev/null +++ b/RuntimePrototypes.hpp @@ -0,0 +1,80 @@ +#ifndef RUNTIME_PROTOTYPES_HPP +#define RUNTIME_PROTOTYPES_HPP + +#include "Nodes.hpp" +#include +#include +#include + +#define PUT(type, funcname) \ + new Function { \ + new Id {#funcname}, Type::VOID, \ + new PointerList { \ + new FunArg {new Id {"value"}, type, false} \ + }, \ + new PointerList{} \ + } + +#define GET(type, funcname) \ + new Function { \ + new Id {#funcname}, type, \ + new PointerList {}, \ + new PointerList{} \ + } + +namespace monicelli { + +static const std::map PUT_NAMES = {{ + {Type::BOOL, "__Monicelli_putBool"}, + {Type::CHAR, "__Monicelli_putChar"}, + {Type::FLOAT, "__Monicelli_putFloat"}, + {Type::DOUBLE, "__Monicelli_putDouble"}, + {Type::INT, "__Monicelli_putInt"} +}}; + +static const std::map GET_NAMES = { + {Type::BOOL, "__Monicelli_getBool"}, + {Type::CHAR, "__Monicelli_getChar"}, + {Type::FLOAT, "__Monicelli_getFloat"}, + {Type::DOUBLE, "__Monicelli_getDouble"}, + {Type::INT, "__Monicelli_getInt"} +}; + +static const std::string ABORT_NAME = "__Monicelli_abort"; +static const std::string ASSERT_NAME = "__Monicelli_assert"; +static const std::string ENTRYPOINT_NAME = "__Monicelli_main"; + + +static const std::map> STANDARD_MODULES = { + {"iostream", { + PUT(Type::BOOL, __Monicelli_putBool), + PUT(Type::CHAR, __Monicelli_putChar), + PUT(Type::FLOAT, __Monicelli_putFloat), + PUT(Type::DOUBLE, __Monicelli_putDouble), + PUT(Type::INT, __Monicelli_putInt), + GET(Type::BOOL, __Monicelli_getBool), + GET(Type::CHAR, __Monicelli_getChar), + GET(Type::FLOAT, __Monicelli_getFloat), + GET(Type::DOUBLE, __Monicelli_getDouble), + GET(Type::INT, __Monicelli_getInt) + }}, + {"cassert", { new Function{ + new Id("__Monicelli_assert"), Type::VOID, + new PointerList { + new FunArg {new Id("condition"), Type::BOOL, false} + }, + new PointerList{} + }}}, + {"cstdlib", { new Function{ + new Id("__Monicelli_abort"), Type::VOID, + new PointerList {}, + new PointerList{} + }}} +}; + +} + +#undef PUT +#undef GET + +#endif