diff --git a/src/MapleFE/Makefile b/src/MapleFE/Makefile index f1dbcac61dd5bed54e947fda4a6c6be58a2144c1..7ae81206d08961f3ecc2c399098634ea3309f1be 100644 --- a/src/MapleFE/Makefile +++ b/src/MapleFE/Makefile @@ -24,7 +24,7 @@ ifeq ($(SRCLANG),java) else ifeq ($(SRCLANG),typescript) TARGET := ts2ast ast2cpp obfuscate else ifeq ($(SRCLANG),c) - TARGET := c2ast + TARGET := c2ast ast2mpl endif all: $(TARGET) @@ -44,7 +44,7 @@ recdetect: autogen shared ladetect ladetect: autogen shared $(MAKE) LANG=$(SRCLANG) -C ladetect -ast2mpl: astopt java2ast +ast2mpl: astopt $(MAKE) -C ast2mpl astopt: shared recdetect ladetect diff --git a/src/MapleFE/Makefile.in b/src/MapleFE/Makefile.in index 9ec2c1ca5240a2d01be9d271ec8a04b1c6abbcb3..12452eea5882c680c9b71566168a53aee1b6bf19 100644 --- a/src/MapleFE/Makefile.in +++ b/src/MapleFE/Makefile.in @@ -10,11 +10,11 @@ LANG=java MKDIR_P = mkdir -p -MAPLELIBPATH:=$(MAPLE_ROOT)/OpenArkCompiler/output/aarch64-clang-debug/lib/64 +MAPLELIBPATH:=$(MAPLE_ROOT)/output/aarch64-clang-debug/lib/64 MAPLELIBS := -L $(MAPLELIBPATH) -lmplir -loption_parser -lmplphase -lmplutil -lmempool -lmpl2mpl -lHWSecureC -ldriver_option -lmplir -loption_parser -ldriver_option MAPLEALL_INC = -I $(MAPLEALL_SRC)/maple_ir/include \ -I $(MAPLEALL_SRC)/mempool/include \ -I $(MAPLEALL_SRC)/maple_util/include \ -I $(MAPLEALL_SRC)/maple_driver/include \ - -I $(MAPLEALL_ROOT)/third_party/bounds_checking_function/include + -I $(MAPLE_ROOT)/third_party/bounds_checking_function/include diff --git a/src/MapleFE/README b/src/MapleFE/README index 9446b57ff2979767e8ab7b069dae94f732af269f..0d57667771c3609c0a64c8d403304bcb88e8405f 100644 --- a/src/MapleFE/README +++ b/src/MapleFE/README @@ -40,23 +40,30 @@ sudo npm install -g typescript@latest [How to build] -1. source envsetup.sh [java|typescript] +typescript +1. source envsetup.sh typescript +2. make +3. outputs: + output/typescript/bin/ts2ast - executable frontend + output/typescript/bin/ast2cpp - translate AST to cpp source code. + +java +1. source envsetup.sh java 2. make mapleall This step is to build mapleall (Maple IR related libraries). If you have done it once, you dont need do it again unless you changed the code in mapleall. 3. make -4. If you are working at Java frontend, you will see output/java/java/java2mpl. This is the executable - frontend. -4. If you are working at Typescript frontend, you will see output/typescript/bin/ts2ast. This is the executable - frontend. You can also see output/typescript/bin/ast2cpp, which translate AST to cpp source code. +4. outputs: + output/java/bin/java2ast - executable frontend + output/java/bin/ast2mpl - translate AST to mpl [How to test] 1. make test -run a single test, say t1.java: +run a single test, say add.ts: 1. cd test -2. make t1 +2. make add You can find all Java test cases in test/java. You can find all Typescript test cases in test/typescript. diff --git a/src/MapleFE/ast2mpl/include/cvt_block.h b/src/MapleFE/ast2mpl/include/cvt_block.h deleted file mode 100644 index 99ac1da878fed8b78a11a0465c6d7aec4a56bd33..0000000000000000000000000000000000000000 --- a/src/MapleFE/ast2mpl/include/cvt_block.h +++ /dev/null @@ -1,41 +0,0 @@ -/* -* Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. -* -* OpenArkFE is licensed under the Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* -* http://license.coscl.org.cn/MulanPSL2 -* -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR -* FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -#ifndef __AST_CVT_BLOCK_H__ -#define __AST_CVT_BLOCK_H__ - -#include "ast_module.h" -#include "ast.h" -#include "gen_astvisitor.h" - -namespace maplefe { - -// CvtBlockVisitor is to fix up some tree nodes after the AST is created -class CvtToBlockVisitor : public AstVisitor { - private: - ModuleNode *mASTModule; - bool mUpdated; - - public: - CvtToBlockVisitor(ModuleNode *m) : mASTModule(m), mUpdated(false) {} - - bool CvtToBlock(); - - CondBranchNode *VisitCondBranchNode(CondBranchNode *node); - ForLoopNode *VisitForLoopNode(ForLoopNode *node); -}; - -} -#endif diff --git a/src/MapleFE/ast2mpl/src/Makefile b/src/MapleFE/ast2mpl/src/Makefile index 51ab1dcd3e1b7c95dcda06ba6a8a02e8df10d433..22ca5983e7b91ea3108cd0d8da7936306c5635c7 100644 --- a/src/MapleFE/ast2mpl/src/Makefile +++ b/src/MapleFE/ast2mpl/src/Makefile @@ -48,7 +48,7 @@ INCLUDEGEN := -I $(MAPLEFE_ROOT)/shared/include -I $(BUILDDIR)/gen -I $(BUILDAST TARGET=ast2mpl SHAREDLIB = $(BUILDDIR)/astopt/astopt.a $(BUILDDIR)/shared/shared.a $(BUILDASTGEN)/genast.a -LANGSPEC=$(BUILDDIR)/java/lang_spec.o +LANGSPEC=$(BUILDDIR)/$(SRCLANG)/lang_spec.o .PHONY: all all: $(BUILDBIN)/$(TARGET) diff --git a/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp b/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp index 2c8c3546f32498b5d14b0b4639585a97e747164d..262a13bb48e78f2a9c91ad0040326a3146b82514 100644 --- a/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp +++ b/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp @@ -18,7 +18,6 @@ #include "mir_module.h" #include "mir_function.h" #include "maplefe_mir_builder.h" -#include "cvt_block.h" namespace maplefe { @@ -77,10 +76,6 @@ void Ast2MplBuilder::Build() { std::cout << "============= in ProcessAST ===========" << std::endl; std::cout << "srcLang : " << module->GetSrcLangString() << std::endl; } - // pass 0: convert to use BlockNode for if-then-else and loop bodies - CvtToBlockVisitor visitor(module); - visitor.CvtToBlock(); - // pass 1: collect class/interface/function decl for (unsigned i = 0; i < module->GetTreesNum(); i++) { TreeNode *tnode = module->GetTree(i); diff --git a/src/MapleFE/ast2mpl/src/cvt_block.cpp b/src/MapleFE/ast2mpl/src/cvt_block.cpp deleted file mode 100644 index 419d1c54bd72c4aa5d0db2ac80bb82b0b40f713a..0000000000000000000000000000000000000000 --- a/src/MapleFE/ast2mpl/src/cvt_block.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* -* Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. -* -* OpenArkFE is licensed under the Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* -* http://license.coscl.org.cn/MulanPSL2 -* -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR -* FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -#include "cvt_block.h" - -namespace maplefe { - -bool CvtToBlockVisitor::CvtToBlock() { - for (unsigned i = 0; i < mASTModule->GetTreesNum(); i++ ) { - TreeNode *it = mASTModule->GetTree(i); - Visit(it); - } - return mUpdated; -} - -// if-then-else -CondBranchNode *CvtToBlockVisitor::VisitCondBranchNode(CondBranchNode *node) { - TreeNode *tn = VisitTreeNode(node->GetTrueBranch()); - if (tn && !tn->IsBlock()) { - BlockNode *blk = (BlockNode*)gTreePool.NewTreeNode(sizeof(BlockNode)); - new (blk) BlockNode(); - blk->AddChild(tn); - node->SetTrueBranch(blk); - mUpdated = true; - } - tn = VisitTreeNode(node->GetFalseBranch()); - if (tn && !tn->IsBlock()) { - BlockNode *blk = (BlockNode*)gTreePool.NewTreeNode(sizeof(BlockNode)); - new (blk) BlockNode(); - blk->AddChild(tn); - node->SetFalseBranch(blk); - mUpdated = true; - } - return node; -} - -// for -ForLoopNode *CvtToBlockVisitor::VisitForLoopNode(ForLoopNode *node) { - TreeNode *tn = VisitTreeNode(node->GetBody()); - if (tn && !tn->IsBlock()) { - BlockNode *blk = (BlockNode*)gTreePool.NewTreeNode(sizeof(BlockNode)); - new (blk) BlockNode(); - blk->AddChild(tn); - node->SetBody(blk); - mUpdated = true; - } - return node; -} - -} diff --git a/src/MapleFE/astopt/include/ast_info.h b/src/MapleFE/astopt/include/ast_info.h index 83a0fc37122e10231fdde2f6d7050bd0eaf908ff..e040caf7a04ead1739f6afe6e58035a6fdacabec 100644 --- a/src/MapleFE/astopt/include/ast_info.h +++ b/src/MapleFE/astopt/include/ast_info.h @@ -46,7 +46,6 @@ class AST_INFO { std::unordered_set mWithTypeParamNodeSet; std::unordered_set mWithThisFuncSet;; std::unordered_set mFromLambda; - std::unordered_map mStrIdx2TypeIdxMap; void AddField(unsigned nid, TreeNode *node); @@ -108,11 +107,6 @@ class AST_INFO { void AddFromLambda(unsigned nid) { mFromLambda.insert(nid); } bool IsFromLambda(unsigned nid) { return mFromLambda.find(nid) != mFromLambda.end(); } - - void AddBuiltInTypes(); - bool IsBuiltInType(TreeNode *node); - unsigned GetBuiltInTypeIdx(unsigned stridx); - unsigned GetBuiltInTypeIdx(TreeNode *node); }; class FillNodeInfoVisitor : public AstVisitor { diff --git a/src/MapleFE/astopt/src/ast_info.cpp b/src/MapleFE/astopt/src/ast_info.cpp index 5c8ef881ec5d39d5ac2f54f6a6cae737fbb8cb82..3bd89023e05fa461c1840a885aa14b3f03cf9065 100644 --- a/src/MapleFE/astopt/src/ast_info.cpp +++ b/src/MapleFE/astopt/src/ast_info.cpp @@ -32,8 +32,6 @@ void AST_INFO::CollectInfo() { it->SetParent(module); } - AddBuiltInTypes(); - // collect import/export info MSGNOLOC0("============== XXport info =============="); mHandler->GetASTXXport()->CollectXXportInfo(mHandler->GetHidx()); @@ -73,44 +71,6 @@ void AST_INFO::CollectInfo() { func_visitor.Visit(module); } -void AST_INFO::AddBuiltInTypes() { - unsigned size = gTypeTable.size(); - for (unsigned idx = 1; idx < size; idx++) { - TreeNode *node = gTypeTable.GetTypeFromTypeIdx(idx); - if (node->IsUserType()) { - mStrIdx2TypeIdxMap[node->GetStrIdx()] = node->GetTypeIdx(); - } - } - - // add language builtin types - TreeNode *node = NULL; - unsigned stridx = 0; -#define BUILTIN(T) \ - stridx = gStringPool.GetStrIdx(#T);\ - if (mStrIdx2TypeIdxMap.find(stridx) == mStrIdx2TypeIdxMap.end()) {\ - node = gTypeTable.CreateBuiltinType(#T, TY_Class);\ - gTypeTable.AddType(node);\ - mStrIdx2TypeIdxMap[stridx] = node->GetTypeIdx();\ - } -#include "lang_builtin.def" -} - -bool AST_INFO::IsBuiltInType(TreeNode *node) { - return mStrIdx2TypeIdxMap.find(node->GetStrIdx()) != mStrIdx2TypeIdxMap.end(); -} - -unsigned AST_INFO::GetBuiltInTypeIdx(unsigned stridx) { - if (mStrIdx2TypeIdxMap.find(stridx) != mStrIdx2TypeIdxMap.end()) { - return mStrIdx2TypeIdxMap[stridx]; - } - return 0; -} - -unsigned AST_INFO::GetBuiltInTypeIdx(TreeNode *node) { - unsigned stridx = node->GetStrIdx(); - return GetBuiltInTypeIdx(stridx); -} - TypeId AST_INFO::GetTypeId(TreeNode *node) { return node->GetTypeId(); } @@ -438,7 +398,7 @@ IdentifierNode *AST_INFO::CreateIdentifierNode(unsigned stridx) { } UserTypeNode *AST_INFO::CreateUserTypeNode(unsigned stridx, ASTScope *scope) { - unsigned tidx = GetBuiltInTypeIdx(stridx); + unsigned tidx = gTypeTable.GetBuiltInTypeIdx(stridx); IdentifierNode *node = CreateIdentifierNode(stridx); SetTypeId(node, TY_Class); SetTypeIdx(node, tidx); @@ -692,7 +652,7 @@ FunctionNode *FillNodeInfoVisitor::VisitFunctionNode(FunctionNode *node) { mInfo->SetTypeIdx(node, type->GetTypeIdx()); } else if (node->IsGenerator()) { unsigned stridx = gStringPool.GetStrIdx("Generator"); - unsigned tidx = mInfo->GetBuiltInTypeIdx(stridx); + unsigned tidx = gTypeTable.GetBuiltInTypeIdx(stridx); UserTypeNode *ut = mInfo->CreateUserTypeNode(stridx); node->SetRetType(ut); } @@ -766,7 +726,7 @@ UserTypeNode *FillNodeInfoVisitor::VisitUserTypeNode(UserTypeNode *node) { (void) AstVisitor::VisitUserTypeNode(node); TreeNode *id = node->GetId(); if (id) { - unsigned tidx = mInfo->GetBuiltInTypeIdx(id); + unsigned tidx = gTypeTable.GetBuiltInTypeIdx(id); if (tidx) { mInfo->SetTypeIdx(id, tidx); } diff --git a/src/MapleFE/astopt/src/ast_ti.cpp b/src/MapleFE/astopt/src/ast_ti.cpp index 99439b59de694b757e30393cd8db4d3a3456d115..930230e293f133130dc58b52a3a951c2cced6439 100644 --- a/src/MapleFE/astopt/src/ast_ti.cpp +++ b/src/MapleFE/astopt/src/ast_ti.cpp @@ -136,7 +136,7 @@ FieldLiteralNode *BuildIdDirectFieldVisitor::VisitFieldLiteralNode(FieldLiteralN IdentifierNode *field = static_cast(name); TreeNode *decl = mHandler->FindDecl(field); TreeNode *vtype = GetParentVarClass(decl); - if (vtype && !mHandler->GetINFO()->IsBuiltInType(vtype)) { + if (vtype && !gTypeTable.IsBuiltInType(vtype)) { // check if decl is a field of vtype // note: vtype could be in different module Module_Handler *h = mHandler->GetModuleHandler(vtype); diff --git a/src/MapleFE/envsetup.sh b/src/MapleFE/envsetup.sh index bc75a54707313c7edd50cad8b64557d54b67b914..4f106000bc74d34cdd98aeba3f0b16e04b97fb8f 100755 --- a/src/MapleFE/envsetup.sh +++ b/src/MapleFE/envsetup.sh @@ -40,13 +40,10 @@ fi export SRCLANG=$LANGSRC export MAPLEFE_ROOT=$(cd $(dirname ${BASH_SOURCE[0]}); pwd) -export MAPLE_ROOT=$(cd ${MAPLEFE_ROOT}/../../..; pwd) - -unset MAPLEALL_ROOT -export MAPLEALL_ROOT=${MAPLE_ROOT}/OpenArkCompiler +export MAPLE_ROOT=$(cd ${MAPLEFE_ROOT}/../..; pwd) unset MAPLEALL_SRC -export MAPLEALL_SRC=${MAPLEALL_ROOT}/src/mapleall +export MAPLEALL_SRC=${MAPLE_ROOT}/src/mapleall unset BUILDDIR export BUILDDIR=${MAPLEFE_ROOT}/output/${SRCLANG} diff --git a/src/MapleFE/scripts/build_mapleall.sh b/src/MapleFE/scripts/build_mapleall.sh index c1c5d7375cdb59f496a9c361166f40eab0b2a456..63c03a79eef871eaad7d9888eefe84a86fab69e3 100755 --- a/src/MapleFE/scripts/build_mapleall.sh +++ b/src/MapleFE/scripts/build_mapleall.sh @@ -15,14 +15,7 @@ set -e -if [ ! -d $MAPLEALL_ROOT ]; then - cd $MAPLE_ROOT - git clone https://gitee.com/openarkcompiler/OpenArkCompiler.git -b dev_MapleFE -fi - -cd $MAPLEALL_ROOT -git checkout dev_MapleFE -git pull +cd $MAPLE_ROOT source build/envsetup.sh arm debug make setup make clobber diff --git a/src/MapleFE/shared/include/typetable.h b/src/MapleFE/shared/include/typetable.h index d53b17719156f086fefca6fbc7d2f9ec02844e92..8e6b37b40e72d299f24fc7fd9a73a1677dec84e4 100644 --- a/src/MapleFE/shared/include/typetable.h +++ b/src/MapleFE/shared/include/typetable.h @@ -53,6 +53,7 @@ private: std::vector mTypeTable; std::unordered_map mNodeId2TypeIdxMap; std::unordered_map mTypeId2TypeMap; + std::unordered_map mStrIdx2BuiltInTypeIdxMap; std::unordered_set mPrimTypeId; std::unordered_set mFuncTypeIdx; unsigned mPrimSize; @@ -74,6 +75,10 @@ public: void AddPrimAndBuiltinTypes(); bool AddType(TreeNode *node); + bool IsBuiltInType(TreeNode *node); + unsigned GetBuiltInTypeIdx(unsigned stridx); + unsigned GetBuiltInTypeIdx(TreeNode *node); + TypeEntry *GetTypeEntryFromTypeIdx(unsigned tidx); TreeNode *GetTypeFromTypeIdx(unsigned tidx); TreeNode *GetTypeFromTypeId(TypeId tid) { return mTypeId2TypeMap[tid]; } diff --git a/src/MapleFE/shared/src/Makefile b/src/MapleFE/shared/src/Makefile index e09a8359da282bcbcfa5eb587cfb226eff3a9939..b784761fdd9b2ee44ff33cbc81956cf9ef3430f5 100644 --- a/src/MapleFE/shared/src/Makefile +++ b/src/MapleFE/shared/src/Makefile @@ -36,7 +36,7 @@ ASTSRCG:=$(wildcard ${GENASTDIR}/*.cpp) ASTOBJG:=$(patsubst %.cpp,%.o,$(ASTSRCG)) ASTDEPG:=$(patsubst %.cpp,%.d,$(ASTSRCG)) -INCLUDES := -I $(MAPLEFE_ROOT)/shared/include -I ${GENASTDIR} -I ${GENDIR} +INCLUDES := -I $(MAPLEFE_ROOT)/shared/include -I ${GENASTDIR} -I ${GENDIR} -I $(MAPLEFE_ROOT)/$(SRCLANG)/include SHAREDLIB := shared.a GENASTLIB := genast.a diff --git a/src/MapleFE/shared/src/typetable.cpp b/src/MapleFE/shared/src/typetable.cpp index 8a1d8a93e96c53f2c8636eafbdebf5d22e352a57..ae6771c292a0035a5e667be52e27386520588dba 100644 --- a/src/MapleFE/shared/src/typetable.cpp +++ b/src/MapleFE/shared/src/typetable.cpp @@ -79,6 +79,7 @@ TreeNode *TypeTable::CreateBuiltinType(std::string name, TypeId tid) { id->SetParent(utype); mTypeId2TypeMap[tid] = utype; + mStrIdx2BuiltInTypeIdxMap[stridx] = tid; return utype; } @@ -132,9 +133,39 @@ void TypeTable::AddPrimAndBuiltinTypes() { #include "supported_types.def" mPreBuildSize = size(); + + // add language builtin types + unsigned stridx = 0; +#define BUILTIN(T) \ + stridx = gStringPool.GetStrIdx(#T);\ + if (mStrIdx2BuiltInTypeIdxMap.find(stridx) == mStrIdx2BuiltInTypeIdxMap.end()) {\ + node = CreateBuiltinType(#T, TY_Class);\ + AddType(node);\ + mStrIdx2BuiltInTypeIdxMap[stridx] = node->GetTypeIdx();\ + } +#include "lang_builtin.def" return; } +bool TypeTable::IsBuiltInType(TreeNode *node) { + return mStrIdx2BuiltInTypeIdxMap.find(node->GetStrIdx()) != mStrIdx2BuiltInTypeIdxMap.end(); +} + +unsigned TypeTable::GetBuiltInTypeIdx(unsigned stridx) { + if (mStrIdx2BuiltInTypeIdxMap.find(stridx) != mStrIdx2BuiltInTypeIdxMap.end()) { + return mStrIdx2BuiltInTypeIdxMap[stridx]; + } + return 0; +} + +unsigned TypeTable::GetBuiltInTypeIdx(TreeNode *node) { + unsigned stridx = node->GetStrIdx(); + if (mStrIdx2BuiltInTypeIdxMap.find(stridx) != mStrIdx2BuiltInTypeIdxMap.end()) { + return GetBuiltInTypeIdx(stridx); + } + return 0; +} + TypeEntry *TypeTable::GetTypeEntryFromTypeIdx(unsigned tidx) { MASSERT(tidx < mTypeTable.size() && "type index out of range"); return mTypeTable[tidx]; diff --git a/src/MapleFE/test/Makefile b/src/MapleFE/test/Makefile index aed83f9c0ad98a99312e7e2785030cbc13cd311a..649363ecdfd4ee788ec0e05fdac909f6c522bde1 100644 --- a/src/MapleFE/test/Makefile +++ b/src/MapleFE/test/Makefile @@ -76,8 +76,8 @@ mssetup: @cat -n $(build)/$@.mpl @echo "========================================================\n" @echo gdb --args ../output/java/java/java2mpl $(build)/$@.java $(FLAGS) - $(MAPLEALL_ROOT)/output/aarch64-clang-debug/bin/irbuild $(build)/$@.mpl - $(MAPLEALL_ROOT)/output/aarch64-clang-debug/bin/irbuild $(build)/$@.irb.mpl + $(MAPLE_ROOT)/output/aarch64-clang-debug/bin/irbuild $(build)/$@.mpl + $(MAPLE_ROOT)/output/aarch64-clang-debug/bin/irbuild $(build)/$@.irb.mpl diff -uwb $(build)/$@.irb.mpl $(build)/$@.irb.irb.mpl else ifeq ($(SRCLANG),typescript)