diff --git a/src/MapleFE/Makefile b/src/MapleFE/Makefile index 241d5d663a3a6ba82c40ccac344400c057ba8f37..626118d0fd329e1fd3a3fc8e02c250790f70a4fc 100644 --- a/src/MapleFE/Makefile +++ b/src/MapleFE/Makefile @@ -20,14 +20,14 @@ TARGS = autogen shared recdetect ladetect astopt java2mpl ast2mpl ts2ast ast2cpp $(shell $(MKDIR_P) $(BUILDDIR)) ifeq ($(SRCLANG),java) - TARGET := java2mpl + TARGET := java2ast ast2mpl else ifeq ($(SRCLANG),typescript) TARGET := ts2ast ast2cpp endif all: $(TARGET) -java2mpl: autogen recdetect ladetect shared ast2mpl +java2ast: autogen recdetect ladetect shared $(MAKE) LANG=$(SRCLANG) -C $(SRCLANG) ts2ast: autogen recdetect ladetect shared @@ -39,7 +39,7 @@ recdetect: autogen shared ladetect ladetect: autogen shared $(MAKE) LANG=$(SRCLANG) -C ladetect -ast2mpl: shared +ast2mpl: astopt java2ast $(MAKE) -C ast2mpl astopt: shared recdetect ladetect diff --git a/src/MapleFE/Makefile.in b/src/MapleFE/Makefile.in index 7e05c5a3ab9e48fbfb39e19aecd85d0517c12ea7..f08da2da66e3ae14bac7979d612dd2ca6cb39501 100644 --- a/src/MapleFE/Makefile.in +++ b/src/MapleFE/Makefile.in @@ -11,7 +11,7 @@ LANG=java MKDIR_P = mkdir -p MAPLELIBPATH:=$(MAPLE_ROOT)/OpenArkCompiler/output/aarch64-clang-debug/lib/64 -MAPLELIBS := -L $(MAPLELIBPATH) -lmplir -loption_parser -lmpl2mpl -lmplutil -lmempool -lHWSecureC -ldriver_option +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 \ diff --git a/src/MapleFE/ast2mpl/include/ast2mpl.h b/src/MapleFE/ast2mpl/include/ast2mpl.h index eb94ed78602227173ea3a9d31973acaf8de570fd..a9283482498005a40f1666a3bbaace49a85e4557 100644 --- a/src/MapleFE/ast2mpl/include/ast2mpl.h +++ b/src/MapleFE/ast2mpl/include/ast2mpl.h @@ -20,126 +20,31 @@ #ifndef __AST2MPL_HEADER__ #define __AST2MPL_HEADER__ -#include "ast_module.h" -#include "ast.h" -#include "ast_type.h" +#include "astopt.h" +#include "ast_handler.h" #include "mir_module.h" #include "maplefe_mir_builder.h" namespace maplefe { -#define NOTYETIMPL(K) { if (mTraceA2m) { MNYI(K); }} -#define AST2MPLMSG0(K) { if (mTraceA2m) { MMSG0(K); }} -#define AST2MPLMSG(K,v) { if (mTraceA2m) { MMSG(K,v); }} -#define AST2MPLMSG2(K,v,w) { if (mTraceA2m) { MMSG2(K,v,w); }} - -enum StmtExprKind { - SK_Null, - SK_Stmt, - SK_Expr -}; - -enum bool3 { - false3, - true3, - maybe3 -}; - -class A2M { +class A2M : public AstOpt { private: - const char *mFilename; - ModuleNode *mASTModule; - bool mTraceA2m; - FEMIRBuilder *mMirBuilder; - maple::MIRType *mDefaultType; - FieldData *mFieldData; - unsigned mUniqNum; + AST_Handler *mASTHandler; + unsigned mFlags; + unsigned mIndexImported; public: - maple::MIRModule *mMirModule; - // use type's uniq name as key - std::map mNodeTypeMap; - std::map> mNameFuncMap; - std::map mBlockNodeMap; - std::map mBlockFuncMap; - std::map mFuncMap; - std::map, maple::MIRSymbol*> mNameBlockVarMap; - - A2M(ModuleNode *m); - ~A2M(); - - void Init(); - bool IsStmt(TreeNode *tnode); - bool3 IsCompatibleTo(maple::PrimType expected, maple::PrimType prim); - - void UpdateFuncName(maple::MIRFunction *func); - - virtual const char *Type2Label(const maple::MIRType *type); - void Type2Name(std::string &str, const maple::MIRType *type); - - ClassNode *GetSuperClass(ClassNode *klass); - BlockNode *GetSuperBlock(BlockNode *block); - maple::MIRSymbol *GetSymbol(TreeNode *tnode, BlockNode *block); - maple::MIRSymbol *CreateSymbol(TreeNode *tnode, BlockNode *block); - maple::MIRSymbol *CreateTempVar(const char *prefix, maple::MIRType *type); - maple::MIRFunction *GetCurrFunc(BlockNode *block); - maple::MIRFunction *SearchFunc(unsigned idx, maple::MapleVector &args); - maple::MIRFunction *SearchFunc(TreeNode *method, maple::MapleVector &args, BlockNode *block); - maple::MIRClassType *GetClass(BlockNode *block); - void UpdateUniqName(std::string &str); - - virtual maple::PrimType MapPrim(TypeId id)=0; - virtual maple::MIRType *MapPrimType(TypeId id)=0; - virtual maple::MIRType *MapPrimType(PrimTypeNode *tnode)=0; - - maple::MIRType *MapType(TreeNode *tnode); - void MapAttr(maple::GenericAttrs &attr, AttrId id); - void MapAttr(maple::GenericAttrs &attr, IdentifierNode *inode); - void MapAttr(maple::GenericAttrs &attr, FunctionNode *fnode); - - maple::Opcode MapUnaOpcode(OprId); - maple::Opcode MapBinOpcode(OprId); - maple::Opcode MapBinCmpOpcode(OprId); - maple::Opcode MapBinComboOpcode(OprId); - - void ProcessAST(bool trace_a2m); - maple::BaseNode *ProcessNodeDecl(StmtExprKind, TreeNode *tnode, BlockNode *); - maple::BaseNode *ProcessNode(StmtExprKind, TreeNode *tnode, BlockNode *); - - // Process different TreeNode kind while expecting StmtExprKind as stmt or expr -#undef NODEKIND -#define NODEKIND(K) maple::BaseNode *Process##K(StmtExprKind, TreeNode*, BlockNode*); -#include "ast_nk.def" - - maple::BaseNode *ProcessClassDecl(StmtExprKind, TreeNode*, BlockNode*); - maple::BaseNode *ProcessInterfaceDecl(StmtExprKind, TreeNode*, BlockNode*); - maple::BaseNode *ProcessFieldDecl(StmtExprKind, TreeNode*, BlockNode*); - maple::BaseNode *ProcessFuncDecl(StmtExprKind, TreeNode*, BlockNode*); - maple::BaseNode *ProcessBlockDecl(StmtExprKind, TreeNode*, BlockNode*); - - maple::BaseNode *ProcessFuncSetup(StmtExprKind, TreeNode*, BlockNode*); - - maple::BaseNode *ProcessUnaOperatorMpl(StmtExprKind skind, - maple::Opcode op, - maple::BaseNode *bn, - BlockNode *block); - maple::BaseNode *ProcessBinOperatorMplAssign(StmtExprKind skind, - maple::BaseNode *lhs, - maple::BaseNode *rhs, - BlockNode *block); - maple::BaseNode *ProcessBinOperatorMplComboAssign(StmtExprKind skind, - maple::Opcode op, - maple::BaseNode *lhs, - maple::BaseNode *rhs, - BlockNode *block); - maple::BaseNode *ProcessBinOperatorMplArror(StmtExprKind skind, - maple::BaseNode *lhs, - maple::BaseNode *rhs, - BlockNode *block); - - maple::BaseNode *ProcessLoopCondBody(StmtExprKind skind, TreeNode *cond, TreeNode *body, BlockNode *block); - maple::BaseNode *GetNewNodeLhs(NewNode *node, BlockNode *block); + explicit A2M(AST_Handler *h, unsigned flags) : + AstOpt(h, flags), + mASTHandler(h), + mFlags(flags), + mIndexImported(0) {} + ~A2M() = default; + + // return 0 if successful + // return non-zero if failed + int ProcessAST(); }; } diff --git a/src/MapleFE/ast2mpl/include/ast2mpl_builder.h b/src/MapleFE/ast2mpl/include/ast2mpl_builder.h new file mode 100644 index 0000000000000000000000000000000000000000..1270063fde7b185f7c04293502898921f6174215 --- /dev/null +++ b/src/MapleFE/ast2mpl/include/ast2mpl_builder.h @@ -0,0 +1,148 @@ +/* +* Copyright (C) [2020] 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. +*/ + +////////////////////////////////////////////////////////////////////////////////////////////// +// This is the interface to translate AST to MapleIR. +////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __AST2MPL_BUILDER_HEADER__ +#define __AST2MPL_BUILDER_HEADER__ + +#include "ast.h" +#include "ast_handler.h" + +#include "mir_module.h" +#include "generic_attrs.h" +#include "maplefe_mir_builder.h" + +namespace maplefe { + +#define NOTYETIMPL(K) { if (mTraceA2m) { MNYI(K); }} +#define AST2MPLMSG0(K) { if (mTraceA2m) { MMSG0(K); }} +#define AST2MPLMSG(K,v) { if (mTraceA2m) { MMSG(K,v); }} +#define AST2MPLMSG2(K,v,w) { if (mTraceA2m) { MMSG2(K,v,w); }} + +enum StmtExprKind { + SK_Null, + SK_Stmt, + SK_Expr +}; + +enum bool3 { + false3, + true3, + maybe3 +}; + +class Ast2MplBuilder { +private: + AST_Handler *mASTHandler; + const char *mFilename; + bool mTraceA2m; + FEMIRBuilder *mMirBuilder; + maple::MIRType *mDefaultType; + FieldData *mFieldData; + unsigned mUniqNum; + unsigned mFlags; + +public: + maple::MIRModule *mMirModule; + // use type's uniq name as key + std::map mNodeTypeMap; + std::map> mNameFuncMap; + std::map mBlockNodeMap; + std::map mBlockFuncMap; + std::map mFuncMap; + std::map, maple::MIRSymbol*> mNameBlockVarMap; + + Ast2MplBuilder(AST_Handler *h, unsigned f); + ~Ast2MplBuilder(); + + void Build(); + + void Init(); + bool IsStmt(TreeNode *tnode); + bool3 IsCompatibleTo(maple::PrimType expected, maple::PrimType prim); + + void UpdateFuncName(maple::MIRFunction *func); + + virtual const char *Type2Label(const maple::MIRType *type); + void Type2Name(std::string &str, const maple::MIRType *type); + + ClassNode *GetSuperClass(ClassNode *klass); + BlockNode *GetSuperBlock(BlockNode *block); + maple::MIRSymbol *GetSymbol(TreeNode *tnode, BlockNode *block); + maple::MIRSymbol *CreateSymbol(TreeNode *tnode, BlockNode *block); + maple::MIRSymbol *CreateTempVar(const char *prefix, maple::MIRType *type); + maple::MIRFunction *GetCurrFunc(BlockNode *block); + maple::MIRFunction *SearchFunc(unsigned idx, maple::MapleVector &args); + maple::MIRFunction *SearchFunc(TreeNode *method, maple::MapleVector &args, BlockNode *block); + maple::MIRClassType *GetClass(BlockNode *block); + void UpdateUniqName(std::string &str); + + maple::PrimType MapPrim(TypeId id); + maple::MIRType *MapPrimType(TypeId id); + maple::MIRType *MapPrimType(PrimTypeNode *tnode); + + maple::MIRType *MapType(TreeNode *tnode); + void MapAttr(maple::GenericAttrs &attr, AttrId id); + void MapAttr(maple::GenericAttrs &attr, IdentifierNode *inode); + void MapAttr(maple::GenericAttrs &attr, FunctionNode *fnode); + + maple::Opcode MapUnaOpcode(OprId); + maple::Opcode MapBinOpcode(OprId); + maple::Opcode MapBinCmpOpcode(OprId); + maple::Opcode MapBinComboOpcode(OprId); + + maple::BaseNode *ProcessNodeDecl(StmtExprKind, TreeNode *tnode, BlockNode *); + maple::BaseNode *ProcessNode(StmtExprKind, TreeNode *tnode, BlockNode *); + + // Process different TreeNode kind while expecting StmtExprKind as stmt or expr +#undef NODEKIND +#define NODEKIND(K) maple::BaseNode *Process##K(StmtExprKind, TreeNode*, BlockNode*); +#include "ast_nk.def" + + maple::BaseNode *ProcessClassDecl(StmtExprKind, TreeNode*, BlockNode*); + maple::BaseNode *ProcessInterfaceDecl(StmtExprKind, TreeNode*, BlockNode*); + maple::BaseNode *ProcessFieldDecl(StmtExprKind, TreeNode*, BlockNode*); + maple::BaseNode *ProcessFuncDecl(StmtExprKind, TreeNode*, BlockNode*); + maple::BaseNode *ProcessBlockDecl(StmtExprKind, TreeNode*, BlockNode*); + + maple::BaseNode *ProcessFuncSetup(StmtExprKind, TreeNode*, BlockNode*); + + maple::BaseNode *ProcessUnaOperatorMpl(StmtExprKind skind, + maple::Opcode op, + maple::BaseNode *bn, + BlockNode *block); + maple::BaseNode *ProcessBinOperatorMplAssign(StmtExprKind skind, + maple::BaseNode *lhs, + maple::BaseNode *rhs, + BlockNode *block); + maple::BaseNode *ProcessBinOperatorMplComboAssign(StmtExprKind skind, + maple::Opcode op, + maple::BaseNode *lhs, + maple::BaseNode *rhs, + BlockNode *block); + maple::BaseNode *ProcessBinOperatorMplArror(StmtExprKind skind, + maple::BaseNode *lhs, + maple::BaseNode *rhs, + BlockNode *block); + + maple::BaseNode *ProcessLoopCondBody(StmtExprKind skind, TreeNode *cond, TreeNode *body, BlockNode *block); + maple::BaseNode *GetNewNodeLhs(NewNode *node, BlockNode *block); +}; + +} +#endif diff --git a/src/MapleFE/ast2mpl/include/generic_attrs.h b/src/MapleFE/ast2mpl/include/generic_attrs.h new file mode 100644 index 0000000000000000000000000000000000000000..ecab7c62d180bec7218941b55af3167d9d8eefcf --- /dev/null +++ b/src/MapleFE/ast2mpl/include/generic_attrs.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. + * + * OpenArkCompiler is licensed under 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 GENERIC_ATTRS_H +#define GENERIC_ATTRS_H +#include +#include "mir_type.h" + +namespace maple { +// only for internal use, not emitted +enum GenericAttrKind { +#define FUNC_ATTR +#define TYPE_ATTR +#define FIELD_ATTR +#define ATTR(STR) GENATTR_##STR, +#include "all_attributes.def" +#undef ATTR +#undef FUNC_ATTR +#undef TYPE_ATTR +#undef FIELD_ATTR +}; + +class GenericAttrs { + public: + GenericAttrs() = default; + GenericAttrs(const GenericAttrs &ta) = default; + GenericAttrs &operator=(const GenericAttrs &p) = default; + ~GenericAttrs() = default; + + void SetAttr(GenericAttrKind x) { + attrFlag.set(x); + } + + bool GetAttr(GenericAttrKind x) const { + return attrFlag[x]; + } + + bool operator==(const GenericAttrs &tA) const { + return attrFlag == tA.attrFlag; + } + + bool operator!=(const GenericAttrs &tA) const { + return !(*this == tA); + } + + FieldAttrs ConvertToFieldAttrs(); + TypeAttrs ConvertToTypeAttrs(); + FuncAttrs ConvertToFuncAttrs(); + + private: + std::bitset<128> attrFlag = 0; +}; +} +#endif // GENERIC_ATTRS_H \ No newline at end of file diff --git a/src/MapleFE/ast2mpl/src/Makefile b/src/MapleFE/ast2mpl/src/Makefile index 0f3fb0dfbaa04b6b8986ff16c2dfd17a7f3d73d7..51ab1dcd3e1b7c95dcda06ba6a8a02e8df10d433 100644 --- a/src/MapleFE/ast2mpl/src/Makefile +++ b/src/MapleFE/ast2mpl/src/Makefile @@ -13,27 +13,45 @@ # include ../../Makefile.in +BUILDBIN=$(BUILDDIR)/bin BUILD=$(BUILDDIR)/ast2mpl +BUILDGEN=$(BUILDDIR)/gen +BUILDASTGEN=$(BUILDDIR)/ast_gen/shared $(shell $(MKDIR_P) $(BUILD)) SRC=$(wildcard *.cpp) OBJ :=$(patsubst %.cpp,%.o,$(SRC)) DEP :=$(patsubst %.cpp,%.d,$(SRC)) -OBJS :=$(foreach obj,$(OBJ), $(BUILD)/$(obj)) +SRCG := $(wildcard $(BUILDGEN)/gen*.cpp) +OBJG := $(patsubst %.cpp, %.o, $(SRCG)) +DEPG := $(patsubst %.cpp, %.d, $(SRCG)) + +OBJS :=$(foreach obj,$(OBJ), $(BUILD)/$(obj)) $(OBJG) +DEPS :=$(foreach dep,$(DEP), $(BUILD)/$(dep)) $(DEPG) + LIBOBJS :=$(patsubst $(BUILD)/main.o,,$(OBJS)) -DEPS :=$(foreach dep,$(DEP), $(BUILD)/$(dep)) GENDIR:=${BUILDDIR}/ast_gen/shared INCLUDES := -I $(MAPLEFE_ROOT)/shared/include \ + -I $(MAPLEFE_ROOT)/astopt/include \ -I $(MAPLEFE_ROOT)/ast2mpl/include \ + -I $(MAPLEFE_ROOT)/autogen/include \ + -I $(MAPLEFE_ROOT)/shared/include \ + -I $(MAPLEFE_ROOT)/java/include \ $(MAPLEALL_INC) -I ${GENDIR} AST2MPLLIB = ast2mpl.a +INCLUDEGEN := -I $(MAPLEFE_ROOT)/shared/include -I $(BUILDDIR)/gen -I $(BUILDASTGEN) + +TARGET=ast2mpl +SHAREDLIB = $(BUILDDIR)/astopt/astopt.a $(BUILDDIR)/shared/shared.a $(BUILDASTGEN)/genast.a +LANGSPEC=$(BUILDDIR)/java/lang_spec.o + .PHONY: all -all: $(BUILD)/$(AST2MPLLIB) +all: $(BUILDBIN)/$(TARGET) -include $(DEPS) .PHONY: clean @@ -46,13 +64,25 @@ $(BUILD)/%.o : %.cpp $(CXX) $(CXXFLAGS) -fpermissive $(INCLUDES) -w -c $< -o $@ $(BUILD)/%.d : %.cpp - @$(CXX) $(CXXFLAGS) -std=c++11 -MM $(INCLUDES) $< > $@ + @$(CXX) $(CXXFLAGS) -MM $(INCLUDES) $< > $@ @mv -f $(BUILD)/$*.d $(BUILD)/$*.d.tmp @sed -e 's|.*:|$(BUILD)/$*.o:|' < $(BUILD)/$*.d.tmp > $(BUILD)/$*.d @rm -f $(BUILD)/$*.d.tmp -$(BUILD)/$(AST2MPLLIB) : $(OBJS) - /usr/bin/ar rcs $(BUILD)/$(AST2MPLLIB) $(LIBOBJS) +$(BUILDGEN)/%.o : $(BUILDGEN)/%.cpp $(BUILDGEN)/%.d + $(CXX) $(CXXFLAGS) -fpermissive $(INCLUDEGEN) -w -c $< -o $@ + +$(BUILDGEN)/%.d : $(BUILDGEN)/%.cpp + @$(CXX) $(CXXFLAGS) -std=c++11 -MM $(INCLUDEGEN) $< > $@ + @mv -f $(BUILDGEN)/$*.d $(BUILDGEN)/$*.d.tmp + @sed -e 's|.*:|$(BUILDGEN)/$*.o:|' < $(BUILDGEN)/$*.d.tmp > $(BUILDGEN)/$*.d + @rm -f $(BUILDGEN)/$*.d.tmp + +# TARGET depends on OBJS and shared OBJS from shared directory +# as well as mapleall libraries +$(BUILDBIN)/$(TARGET): $(OBJS) $(SHAREDLIB) + @mkdir -p $(BUILDBIN) + $(LD) -o $(BUILDBIN)/$(TARGET) $(OBJS) $(LANGSPEC) $(SHAREDLIB) $(MAPLELIBS) clean: rm -rf $(BUILD) diff --git a/src/MapleFE/ast2mpl/src/ast2mpl.cpp b/src/MapleFE/ast2mpl/src/ast2mpl.cpp index ff0a0917d2a759592cd44b5652c26d1092d16d1d..e6ac6c2b44641dfdd3b96313037b5dadfe55ecbb 100644 --- a/src/MapleFE/ast2mpl/src/ast2mpl.cpp +++ b/src/MapleFE/ast2mpl/src/ast2mpl.cpp @@ -13,649 +13,117 @@ * See the Mulan PSL v2 for more details. */ +#include +#include +#include +#include + #include "ast2mpl.h" +#include "ast_handler.h" #include "gen_astdump.h" +#include "gen_astgraph.h" +#include "gen_aststore.h" +#include "gen_astload.h" + #include "mir_function.h" -#include "maplefe_mir_builder.h" -#include "cvt_block.h" +#include "ast2mpl_builder.h" namespace maplefe { -A2M::A2M(ModuleNode *m) : mASTModule(m) { - mFilename = mASTModule->GetFilename(); - mMirModule = new maple::MIRModule(mFilename); - maple::theMIRModule = mMirModule; - mMirBuilder = new FEMIRBuilder(mMirModule); - mFieldData = new FieldData(); - Init(); -} - -A2M::~A2M() { - delete mMirModule; - delete mMirBuilder; - delete mFieldData; - mNodeTypeMap.clear(); -} - -void A2M::Init() { - // create mDefaultType - maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetOrCreateClassType("DEFAULT_TYPE", *mMirModule); - type->SetMIRTypeKind(maple::kTypeClass); - mDefaultType = mMirBuilder->GetOrCreatePointerType(type); +// starting point of AST +int A2M::ProcessAST() { + mIndexImported = GetModuleNum(); - // setup flavor and srclang - mMirModule->SetFlavor(maple::kFeProduced); - mMirModule->SetSrcLang(maple::kSrcLangJava); + // loop through module handlers + for (HandlerIndex i = 0; i < GetModuleNum(); i++) { + Module_Handler *handler = mASTHandler->GetModuleHandler(i); + ModuleNode *module = handler->GetASTModule(); - // setup INFO_filename - maple::GStrIdx idx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(mFilename); - SET_INFO_PAIR(mMirModule, "INFO_filename", idx.GetIdx(), true); - - // add to src file list - std::string str(mFilename); - size_t pos = str.rfind('/'); - if (pos != std::string::npos) { - idx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(str.substr(pos+1)); - } - mMirModule->PushbackFileInfo(maple::MIRInfoPair(idx, 2)); + if (mFlags & FLG_trace_1) { + std::cout << "============= in ProcessAST ===========" << std::endl; + std::cout << "srcLang : " << module->GetSrcLangString() << std::endl; - // initialize unique serial number for temporary variables and inner classes - mUniqNum = 1; -} - -// starting point of AST to MPL process -void A2M::ProcessAST(bool trace_a2m) { - mTraceA2m = trace_a2m; - AstDump astdump(mASTModule); - - if (mTraceA2m) { - std::cout << "============= in ProcessAST ===========" << std::endl; - std::cout << "srcLang : " << mASTModule->GetSrcLangString() << std::endl; - } - // pass 0: convert to use BlockNode for if-then-else and loop bodies - CvtToBlockVisitor visitor(mASTModule); - visitor.CvtToBlock(); - - // pass 1: collect class/interface/function decl - for (unsigned i = 0; i < mASTModule->GetTreesNum(); i++) { - TreeNode *tnode = mASTModule->GetTree(i); - ProcessNodeDecl(SK_Stmt, tnode, nullptr); - } - // pass 2: handle function def - for (unsigned i = 0; i < mASTModule->GetTreesNum(); i++) { - TreeNode *tnode = mASTModule->GetTree(i); - ProcessNode(SK_Stmt, tnode, nullptr); - } - if (mTraceA2m) { astdump.Dump("ProcessAST", &std::cout); } -} - -maple::MIRType *A2M::MapType(TreeNode *type) { - if (!type) { - return maple::GlobalTables::GetTypeTable().GetVoid(); - } - - maple::MIRType *mir_type = mDefaultType; - - unsigned idx = type->GetStrIdx(); - if (mNodeTypeMap.find(idx) != mNodeTypeMap.end()) { - return mNodeTypeMap[idx]; - } - - if (type->IsPrimType()) { - PrimTypeNode *ptnode = static_cast(type); - mir_type = MapPrimType(ptnode); - - // update mNodeTypeMap - mNodeTypeMap[idx] = mir_type; - } else if (type->IsUserType()) { - if (type->IsIdentifier()) { - IdentifierNode *inode = static_cast(type); - mir_type = MapType(inode->GetType()); - } else if (type->IsLiteral()) { - NOTYETIMPL("MapType IsUserType IsLiteral"); - } else { - NOTYETIMPL("MapType IsUserType"); + for(unsigned k = 0; k < module->GetTreesNum(); k++) { + TreeNode *tnode = module->GetTree(k); + if (mFlags & FLG_trace_1) { + tnode->Dump(0); + std::cout << std::endl; + } + } } - // DimensionNode *mDims - // unsigned dnum = inode->GetDimsNum(); - mNodeTypeMap[idx] = mir_type; - } else if (idx) { - AST2MPLMSG("MapType add a class type by idx", idx); - mir_type = maple::GlobalTables::GetTypeTable().GetOrCreateClassType(type->GetName(), *mMirModule); - mir_type->SetMIRTypeKind(maple::kTypeClass); - mir_type = mMirBuilder->GetOrCreatePointerType(mir_type); - mNodeTypeMap[idx] = mir_type; - } else { - NOTYETIMPL("MapType unknown type"); - } - return mir_type; -} - -maple::Opcode A2M::MapUnaOpcode(OprId ast_op) { - maple::Opcode op = maple::OP_undef; - switch (ast_op) { - case OPR_Add: op = maple::OP_add; break; - case OPR_Sub: op = maple::OP_neg; break; - case OPR_Inc: op = maple::OP_incref; break; - case OPR_Dec: op = maple::OP_decref; break; - case OPR_Bcomp: op = maple::OP_bnot; break; - case OPR_Not: op = maple::OP_lnot; break; - default: break; - } - return op; -} -maple::Opcode A2M::MapBinOpcode(OprId ast_op) { - maple::Opcode op = maple::OP_undef; - switch (ast_op) { - case OPR_Add: op = maple::OP_add; break; - case OPR_Sub: op = maple::OP_sub; break; - case OPR_Mul: op = maple::OP_mul; break; - case OPR_Div: op = maple::OP_div; break; - case OPR_Mod: op = maple::OP_rem; break; - case OPR_Band: op = maple::OP_band; break; - case OPR_Bor: op = maple::OP_bior; break; - case OPR_Bxor: op = maple::OP_bxor; break; - case OPR_Shl: op = maple::OP_shl; break; - case OPR_Shr: op = maple::OP_ashr; break; - case OPR_Zext: op = maple::OP_zext; break; - case OPR_Land: op = maple::OP_land; break; - case OPR_Lor: op = maple::OP_lior; break; - default: break; + if (mFlags & FLG_trace_2) { + std::cout << "============= AstGraph ===========" << std::endl; + AstGraph graph(module); + graph.DumpGraph("After LoadFromAstBuf()", &std::cout); + } } - return op; -} -maple::Opcode A2M::MapBinCmpOpcode(OprId ast_op) { - maple::Opcode op = maple::OP_undef; - switch (ast_op) { - case OPR_EQ: op = maple::OP_eq; break; - case OPR_NE: op = maple::OP_ne; break; - case OPR_GT: op = maple::OP_gt; break; - case OPR_LT: op = maple::OP_lt; break; - case OPR_GE: op = maple::OP_ge; break; - case OPR_LE: op = maple::OP_le; break; - default: break; - } - return op; -} + // build dependency of modules + PreprocessModules(); -maple::Opcode A2M::MapBinComboOpcode(OprId ast_op) { - maple::Opcode op = maple::OP_undef; - switch (ast_op) { - case OPR_AddAssign: op = maple::OP_add; break; - case OPR_SubAssign: op = maple::OP_sub; break; - case OPR_MulAssign: op = maple::OP_mul; break; - case OPR_DivAssign: op = maple::OP_div; break; - case OPR_ModAssign: op = maple::OP_rem; break; - case OPR_ShlAssign: op = maple::OP_shl; break; - case OPR_ShrAssign: op = maple::OP_ashr; break; - case OPR_BandAssign: op = maple::OP_band; break; - case OPR_BorAssign: op = maple::OP_bior; break; - case OPR_BxorAssign: op = maple::OP_bxor; break; - case OPR_ZextAssign: op = maple::OP_zext; break; - default: - break; - } - return op; -} + // loop through module handlers in import/export dependency order + for (auto handler: mHandlersInOrder) { + ModuleNode *module = handler->GetASTModule(); -const char *A2M::Type2Label(const maple::MIRType *type) { - maple::PrimType pty = type->GetPrimType(); - switch (pty) { - case maple::PTY_u1: return "Z"; - case maple::PTY_i8: return "C"; - case maple::PTY_u8: return "UC"; - case maple::PTY_i16: return "S"; - case maple::PTY_u16: return "US"; - case maple::PTY_i32: return "I"; - case maple::PTY_u32: return "UI"; - case maple::PTY_i64: return "J"; - case maple::PTY_u64: return "UJ"; - case maple::PTY_f32: return "F"; - case maple::PTY_f64: return "D"; - case maple::PTY_void: return "V"; - default: return "L"; - } -} - -bool A2M::IsStmt(TreeNode *tnode) { - bool status = true; - if (!tnode) return false; + // basic analysis + handler->BasicAnalysis(); - switch (tnode->GetKind()) { - case NK_Parenthesis: { - ParenthesisNode *node = static_cast(tnode); - status = IsStmt(node->GetExpr()); - break; - } - case NK_UnaOperator: { - UnaOperatorNode *node = static_cast(tnode); - OprId ast_op = node->GetOprId(); - if (ast_op != OPR_Inc && ast_op != OPR_Dec) { - status = false; - } - break; - } - case NK_BinOperator: { - BinOperatorNode *bon = static_cast(tnode); - maple::Opcode op = MapBinComboOpcode(bon->GetOprId()); - if (bon->GetOprId() != OPR_Assign && op == maple::OP_undef) { - status = false; + if (mFlags & FLG_trace_2) { + std::cout << "============= After AdjustAST ===========" << std::endl; + for(unsigned k = 0; k < module->GetTreesNum(); k++) { + TreeNode *tnode = module->GetTree(k); + if (mFlags & FLG_trace_1) { + tnode->Dump(0); + std::cout << std::endl; + } } - break; + AstGraph graph(module); + graph.DumpGraph("After AdjustAST()", &std::cout); } - case NK_Block: - case NK_Break: - case NK_Call: - case NK_CondBranch: - case NK_Delete: - case NK_DoLoop: - case NK_ExprList: - case NK_ForLoop: - case NK_Function: - case NK_Interface: - case NK_Lambda: - case NK_New: - case NK_Return: - case NK_Switch: - case NK_SwitchCase: - case NK_VarList: - case NK_WhileLoop: - status = true; - break; - case NK_Annotation: - case NK_AnnotationType: - case NK_Assert: - case NK_Attr: - case NK_Cast: - case NK_Class: - case NK_Dimension: - case NK_Exception: - case NK_Field: - case NK_Identifier: - case NK_Import: - case NK_InstanceOf: - case NK_Literal: - case NK_Package: - case NK_Pass: - case NK_PrimArrayType: - case NK_PrimType: - case NK_SwitchLabel: - case NK_TerOperator: - case NK_UserType: - default: - status = false; - break; - } - return status; -} - -#define USE_SHORT 1 -// used to form mangled function name -#if USE_SHORT -#define CLASSEND ";" -#define SEP "|" -#define LARG "_" // "(" -#define RARG "_" // ")" -#else -#define CLASSEND "_3B" // ";" -#define SEP "_7C" // "|" -#define LARG "_28" // "(" -#define RARG "_29" // ")" -#endif + // build CFG + handler->BuildCFG(); -void A2M::Type2Name(std::string &str, const maple::MIRType *type) { - maple::PrimType pty = type->GetPrimType(); - const char *n = Type2Label(type); - str.append(n); - if (n[0] == 'L') { - while (type->GetPrimType() == maple::PTY_ptr || type->GetPrimType() == maple::PTY_ref) { - const maple::MIRPtrType *ptype = static_cast(type); - type = ptype->GetPointedType(); + if (mFlags & FLG_trace_2) { + handler->Dump("After BuildCFG()"); } - str.append(type->GetName()); - str.append(CLASSEND); - } -} - -// update to use uniq name: str --> str|mUniqNum -void A2M::UpdateUniqName(std::string &str) { - str.append(SEP); - str.append(std::to_string(mUniqNum++)); - return; -} - -// update to use mangled name: className|funcName|(argTypes)retType -void A2M::UpdateFuncName(maple::MIRFunction *func) { - std::string str; - maple::TyIdx tyIdx = func->GetClassTyIdx(); - maple::MIRType *type; - if (tyIdx.GetIdx() != 0) { - type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx); - str.append(type->GetName()); - str.append(SEP); - } - str.append(func->GetName()); - str.append(SEP); - str.append(LARG); - maple::GStrIdx stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName("this"); - for (size_t i = 0; i < func->GetFormalCount(); i++) { - maple::FormalDef def = func->GetFormalDefAt(i); - // exclude type of extra "this" in the function name - if (stridx == def.formalStrIdx) continue; - maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(def.formalTyIdx); - Type2Name(str, type); - } - str.append(RARG); - type = func->GetReturnType(); - Type2Name(str, type); - maple::MIRSymbol *funcst = maple::GlobalTables::GetGsymTable().GetSymbolFromStidx(func->GetStIdx().Idx()); - // remove old entry in strIdxToStIdxMap - maple::GlobalTables::GetGsymTable().RemoveFromStringSymbolMap(*funcst); - AST2MPLMSG("UpdateFuncName()", str); - stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(str); - funcst->SetNameStrIdx(stridx); - // add new entry in strIdxToStIdxMap - maple::GlobalTables::GetGsymTable().AddToStringSymbolMap(*funcst); -} + // control flow analysis + handler->ControlFlowAnalysis(); -ClassNode *A2M::GetSuperClass(ClassNode *klass) { - TreeNode *tnode = klass->GetParent(); - while (tnode && !tnode->IsClass()) { - tnode = tnode->GetParent(); - } - return static_cast(tnode); -} + // type inference + handler->TypeInference(); -BlockNode *A2M::GetSuperBlock(BlockNode *block) { - TreeNode *tnode = block->GetParent(); - while (tnode && !tnode->IsBlock()) { - tnode = tnode->GetParent(); - } - return static_cast(tnode); -} - -maple::MIRSymbol *A2M::GetSymbol(TreeNode *tnode, BlockNode *block) { - unsigned idx = tnode->GetStrIdx(); - maple::MIRSymbol *symbol = nullptr; - - // global symbol - if (!block) { - std::pair P(idx, block); - symbol = mNameBlockVarMap[P]; - return symbol; - } - - // trace block hirachy for defined symbol - BlockNode *blk = block; - do { - std::pair P(idx, blk); - symbol = mNameBlockVarMap[P]; - if (symbol) { - return symbol; + if (mFlags & FLG_trace_2) { + std::cout << "============= AstGraph ===========" << std::endl; + AstGraph graph(module); + graph.DumpGraph("After BuildCFG()", &std::cout); } - blk = GetSuperBlock(blk); - } while (blk); - - // check parameters - maple::MIRFunction *func = GetCurrFunc(blk); - if (!func) { - NOTYETIMPL("Block parent hirachy"); - return symbol; - } - maple::GStrIdx stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(tnode->GetName()); - if (func->IsAFormalName(stridx)) { - maple::FormalDef def = func->GetFormalFromName(stridx); - return def.formalSym; - } - return symbol; -} -maple::MIRSymbol *A2M::CreateTempVar(const char *prefix, maple::MIRType *type) { - if (!type) { - return nullptr; - } - std::string str(prefix); - str.append(SEP); - str.append(std::to_string(mUniqNum++)); - maple::MIRFunction *func = mMirModule->CurFunction(); - maple::MIRSymbol *var = mMirBuilder->CreateLocalDecl(str, *type); - return var; -} - -maple::MIRSymbol *A2M::CreateSymbol(TreeNode *tnode, BlockNode *block) { - std::string name = tnode->GetName(); - maple::MIRType *mir_type; - - if (tnode->IsIdentifier()) { - IdentifierNode *inode = static_cast(tnode); - mir_type = MapType(inode->GetType()); - } else if (tnode->IsLiteral()) { - NOTYETIMPL("CreateSymbol LiteralNode()"); - mir_type = mDefaultType; - } - - // always use pointer type for classes, with PTY_ref - maple::MIRTypeKind kind = mir_type->GetKind(); - if (kind == maple::kTypeClass || kind == maple::kTypeClassIncomplete || - kind == maple::kTypeInterface || kind == maple::kTypeInterfaceIncomplete) { - mir_type = mMirBuilder->GetOrCreatePointerType(mir_type); - } - - maple::MIRSymbol *symbol = nullptr; - if (block) { - maple::MIRFunction *func = GetCurrFunc(block); - symbol = mMirBuilder->GetLocalDecl(name); - std::string str(name); - // symbol with same name already exist, use a uniq new name - if (symbol) { - UpdateUniqName(str); - } - symbol = mMirBuilder->CreateLocalDecl(str, *mir_type); - } else { - symbol = mMirBuilder->GetGlobalDecl(name); - std::string str(name); - // symbol with same name already exist, use a uniq new name - if (symbol) { - UpdateUniqName(str); + if (mFlags & FLG_trace_2) { + std::cout << "============= AstDump ===========" << std::endl; + AstDump astdump(module); + astdump.Dump("After BuildCFG()", &std::cout); } - symbol = mMirBuilder->CreateGlobalDecl(str, *mir_type, maple::kScGlobal); - } - - std::pair P(tnode->GetStrIdx(), block); - mNameBlockVarMap[P] = symbol; - - return symbol; -} - -maple::MIRFunction *A2M::GetCurrFunc(BlockNode *block) { - maple::MIRFunction *func = nullptr; - // func = mBlockFuncMap[block]; - func = mMirModule->CurFunction(); - return func; -} - -maple::MIRClassType *A2M::GetClass(BlockNode *block) { - maple::TyIdx tyidx = GetCurrFunc(block)->GetClassTyIdx(); - return (maple::MIRClassType*)maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyidx); -} -bool3 A2M::IsCompatibleTo(maple::PrimType expected, maple::PrimType prim) { - if (expected == prim) - return true3; + // data flow analysis + handler->DataFlowAnalysis(); - maple::PrimitiveType type(prim); - bool3 comp = false3; - switch (expected) { - case maple::PTY_i8: - case maple::PTY_i16: - case maple::PTY_i32: - case maple::PTY_i64: - case maple::PTY_u8: - case maple::PTY_u16: - case maple::PTY_u32: - case maple::PTY_u64: - case maple::PTY_u1: - if (type.IsInteger()) { - comp = true3; - } - break; - case maple::PTY_ptr: - case maple::PTY_ref: - if (type.IsPointer()) { - comp = true3; - } - if (type.IsInteger()) { - comp = maybe3; - } - break; - case maple::PTY_a32: - case maple::PTY_a64: - if (type.IsAddress()) { - comp = true3; - } - break; - case maple::PTY_f32: - case maple::PTY_f64: - case maple::PTY_f128: - if (type.IsFloat()) { - comp = true3; - } - break; - case maple::PTY_c64: - case maple::PTY_c128: - if (type.IsInteger()) { - comp = true3; - } - break; - case maple::PTY_constStr: - case maple::PTY_gen: - case maple::PTY_agg: - case maple::PTY_unknown: - case maple::PTY_v2i64: - case maple::PTY_v4i32: - case maple::PTY_v8i16: - case maple::PTY_v16i8: - case maple::PTY_v2f64: - case maple::PTY_v4f32: - case maple::PTY_void: - default: - break; - } - return comp; -} - -maple::MIRFunction *A2M::SearchFunc(unsigned idx, maple::MapleVector &args) { - if (mNameFuncMap.find(idx) == mNameFuncMap.end()) { - return nullptr; - } - std::vector candidates; - for (auto it: mNameFuncMap[idx]) { - if (it->GetFormalCount() != args.size()) { - continue; - } - bool matched = true; - bool3 mightmatched = true3; - for (int i = 0; i < it->GetFormalCount(); i++) { - maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(it->GetFormalDefAt(i).formalTyIdx); - bool3 comp = IsCompatibleTo(type->GetPrimType(), args[i]->GetPrimType()); - if (comp == false3) { - matched = false; - mightmatched = false3; - break; - } else if (comp == maybe3) { - matched = false; - } - } - if (matched) { - return it; - } - if (mightmatched != false3) { - candidates.push_back(it); - } - } - if (candidates.size()) { - return candidates[0]; - } - return nullptr; -} - -maple::MIRFunction *A2M::SearchFunc(TreeNode *method, maple::MapleVector &args, BlockNode *block) { - maple::MIRFunction *func = nullptr; - switch (method->GetKind()) { - case NK_Function: { - func = SearchFunc(method->GetStrIdx(), args); - break; - } - case NK_Identifier: { - IdentifierNode *imethod = static_cast(method); - func = SearchFunc(imethod->GetStrIdx(), args); - break; + if (mFlags & FLG_trace_2) { + handler->Dump("After DataFlowAnalysis()"); } - case NK_Field: { - FieldNode *node = static_cast(method); - TreeNode *upper = node->GetUpper(); - TreeNode *field = node->GetField(); - if (field->IsIdentifier()) { - // pass upper as this - maple::BaseNode *bn = ProcessNode(SK_Expr, upper, block); - if (bn) { - args[0] = bn; - } - } - func = SearchFunc(field, args, block); - break; - } - default: - NOTYETIMPL("GetFuncName() method to be handled"); } - return func; -} - -void A2M::MapAttr(maple::GenericAttrs &attr, AttrId id) { - switch (id) { -#undef ATTRIBUTE -#define ATTRIBUTE(X) case ATTR_##X: attr.SetAttr(maple::GENATTR_##X); break; -// #include "supported_attributes.def" -ATTRIBUTE(abstract) -ATTRIBUTE(const) -ATTRIBUTE(volatile) -ATTRIBUTE(final) -ATTRIBUTE(native) -ATTRIBUTE(private) -ATTRIBUTE(protected) -ATTRIBUTE(public) -ATTRIBUTE(static) -ATTRIBUTE(default) -ATTRIBUTE(synchronized) - -// ATTRIBUTE(strictfp) - case ATTR_strictfp: attr.SetAttr(maple::GENATTR_strict); break; - - default: - break; + // build mpl + if (mFlags & FLG_trace_2) { + std::cout << "============= Ast2Mpl Build ===========" << std::endl; } -} + maplefe::Ast2MplBuilder ast2mpl_builder(mASTHandler, mFlags); + ast2mpl_builder.Build(); -void A2M::MapAttr(maple::GenericAttrs &attr, IdentifierNode *inode) { - // SmallVector mAttrs - unsigned anum = inode->GetAttrsNum(); - for (int i = 0; i < anum; i++) { - const AttrId ast_attr = inode->GetAttrAtIndex(i); - MapAttr(attr, ast_attr); - } + ast2mpl_builder.mMirModule->OutputAsciiMpl("", ".mpl"); + return 0; } -void A2M::MapAttr(maple::GenericAttrs &attr, FunctionNode *fnode) { - unsigned anum = fnode->GetAttrsNum(); - for (int i = 0; i < anum; i++) { - const AttrId ast_attr = fnode->GetAttrAtIndex(i); - MapAttr(attr, ast_attr); - } -} } - diff --git a/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp b/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8c3546f32498b5d14b0b4639585a97e747164d --- /dev/null +++ b/src/MapleFE/ast2mpl/src/ast2mpl_builder.cpp @@ -0,0 +1,715 @@ +/* +* Copyright (C) [2020] 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 "ast2mpl_builder.h" +#include "gen_astdump.h" +#include "mir_module.h" +#include "mir_function.h" +#include "maplefe_mir_builder.h" +#include "cvt_block.h" + +namespace maplefe { + +Ast2MplBuilder::Ast2MplBuilder(AST_Handler *h, unsigned f) : mASTHandler(h), mFlags(f) { + mFilename = h->GetModuleHandler((unsigned)0)->GetASTModule()->GetFilename(); + mMirModule = new maple::MIRModule(mFilename); + maple::theMIRModule = mMirModule; + mMirBuilder = new FEMIRBuilder(mMirModule); + mFieldData = new FieldData(); + Init(); +} + +Ast2MplBuilder::~Ast2MplBuilder() { + delete mMirModule; + delete mMirBuilder; + delete mFieldData; + mNodeTypeMap.clear(); +} + +void Ast2MplBuilder::Init() { + // create mDefaultType + maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetOrCreateClassType("DEFAULT_TYPE", *mMirModule); + type->SetMIRTypeKind(maple::kTypeClass); + mDefaultType = mMirBuilder->GetOrCreatePointerType(type); + + // setup flavor and srclang + mMirModule->SetFlavor(maple::kFeProduced); + mMirModule->SetSrcLang(maple::kSrcLangJava); + + // setup INFO_filename + maple::GStrIdx idx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(mFilename); + SET_INFO_PAIR(mMirModule, "INFO_filename", idx.GetIdx(), true); + + // add to src file list + std::string str(mFilename); + size_t pos = str.rfind('/'); + if (pos != std::string::npos) { + idx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(str.substr(pos+1)); + } + mMirModule->PushbackFileInfo(maple::MIRInfoPair(idx, 2)); + + // initialize unique serial number for temporary variables and inner classes + mUniqNum = 1; +} + +// starting point of AST to MPL process +void Ast2MplBuilder::Build() { + mTraceA2m = mFlags & FLG_trace_2; + for (HandlerIndex i = 0; i < mASTHandler->GetSize(); i++) { + Module_Handler *handler = mASTHandler->GetModuleHandler(i); + ModuleNode *module = handler->GetASTModule(); + + AstDump astdump(module); + + if (mTraceA2m) { + 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); + ProcessNodeDecl(SK_Stmt, tnode, nullptr); + } + + // pass 2: handle function def + for (unsigned i = 0; i < module->GetTreesNum(); i++) { + TreeNode *tnode = module->GetTree(i); + ProcessNode(SK_Stmt, tnode, nullptr); + } + if (mTraceA2m) { astdump.Dump("Build", &std::cout); } + } +} + +maple::PrimType Ast2MplBuilder::MapPrim(TypeId id) { + maple::PrimType prim; + switch (id) { + case TY_Boolean: prim = maple::PTY_u1; break; + case TY_Byte: prim = maple::PTY_u8; break; + case TY_Short: prim = maple::PTY_i16; break; + case TY_Int: prim = maple::PTY_i32; break; + case TY_Long: prim = maple::PTY_i64; break; + case TY_Char: prim = maple::PTY_u16; break; + case TY_Float: prim = maple::PTY_f32; break; + case TY_Double: prim = maple::PTY_f64; break; + case TY_Void: prim = maple::PTY_void; break; + case TY_Null: prim = maple::PTY_void; break; + default: MASSERT("Unsupported PrimType"); break; + } + return prim; +} + +maple::MIRType *Ast2MplBuilder::MapPrimType(TypeId id) { + maple::PrimType prim = MapPrim(id); + maple::TyIdx tid(prim); + return maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tid); +} + +maple::MIRType *Ast2MplBuilder::MapPrimType(PrimTypeNode *ptnode) { + return MapPrimType(ptnode->GetPrimType()); +} + +maple::MIRType *Ast2MplBuilder::MapType(TreeNode *type) { + if (!type) { + return maple::GlobalTables::GetTypeTable().GetVoid(); + } + + maple::MIRType *mir_type = mDefaultType; + + unsigned idx = type->GetStrIdx(); + if (mNodeTypeMap.find(idx) != mNodeTypeMap.end()) { + return mNodeTypeMap[idx]; + } + + if (type->IsPrimType()) { + PrimTypeNode *ptnode = static_cast(type); + mir_type = MapPrimType(ptnode); + + // update mNodeTypeMap + mNodeTypeMap[idx] = mir_type; + } else if (type->IsUserType()) { + if (type->IsIdentifier()) { + IdentifierNode *inode = static_cast(type); + mir_type = MapType(inode->GetType()); + } else if (type->IsLiteral()) { + NOTYETIMPL("MapType IsUserType IsLiteral"); + } else { + NOTYETIMPL("MapType IsUserType"); + } + // DimensionNode *mDims + // unsigned dnum = inode->GetDimsNum(); + mNodeTypeMap[idx] = mir_type; + } else if (idx) { + AST2MPLMSG("MapType add a class type by idx", idx); + mir_type = maple::GlobalTables::GetTypeTable().GetOrCreateClassType(type->GetName(), *mMirModule); + mir_type->SetMIRTypeKind(maple::kTypeClass); + mir_type = mMirBuilder->GetOrCreatePointerType(mir_type); + mNodeTypeMap[idx] = mir_type; + } else { + NOTYETIMPL("MapType unknown type"); + } + return mir_type; +} + +maple::Opcode Ast2MplBuilder::MapUnaOpcode(OprId ast_op) { + maple::Opcode op = maple::OP_undef; + switch (ast_op) { + case OPR_Add: op = maple::OP_add; break; + case OPR_Sub: op = maple::OP_neg; break; + case OPR_Inc: op = maple::OP_incref; break; + case OPR_Dec: op = maple::OP_decref; break; + case OPR_Bcomp: op = maple::OP_bnot; break; + case OPR_Not: op = maple::OP_lnot; break; + default: break; + } + return op; +} + +maple::Opcode Ast2MplBuilder::MapBinOpcode(OprId ast_op) { + maple::Opcode op = maple::OP_undef; + switch (ast_op) { + case OPR_Add: op = maple::OP_add; break; + case OPR_Sub: op = maple::OP_sub; break; + case OPR_Mul: op = maple::OP_mul; break; + case OPR_Div: op = maple::OP_div; break; + case OPR_Mod: op = maple::OP_rem; break; + case OPR_Band: op = maple::OP_band; break; + case OPR_Bor: op = maple::OP_bior; break; + case OPR_Bxor: op = maple::OP_bxor; break; + case OPR_Shl: op = maple::OP_shl; break; + case OPR_Shr: op = maple::OP_ashr; break; + case OPR_Zext: op = maple::OP_zext; break; + case OPR_Land: op = maple::OP_land; break; + case OPR_Lor: op = maple::OP_lior; break; + default: break; + } + return op; +} + +maple::Opcode Ast2MplBuilder::MapBinCmpOpcode(OprId ast_op) { + maple::Opcode op = maple::OP_undef; + switch (ast_op) { + case OPR_EQ: op = maple::OP_eq; break; + case OPR_NE: op = maple::OP_ne; break; + case OPR_GT: op = maple::OP_gt; break; + case OPR_LT: op = maple::OP_lt; break; + case OPR_GE: op = maple::OP_ge; break; + case OPR_LE: op = maple::OP_le; break; + default: break; + } + return op; +} + +maple::Opcode Ast2MplBuilder::MapBinComboOpcode(OprId ast_op) { + maple::Opcode op = maple::OP_undef; + switch (ast_op) { + case OPR_AddAssign: op = maple::OP_add; break; + case OPR_SubAssign: op = maple::OP_sub; break; + case OPR_MulAssign: op = maple::OP_mul; break; + case OPR_DivAssign: op = maple::OP_div; break; + case OPR_ModAssign: op = maple::OP_rem; break; + case OPR_ShlAssign: op = maple::OP_shl; break; + case OPR_ShrAssign: op = maple::OP_ashr; break; + case OPR_BandAssign: op = maple::OP_band; break; + case OPR_BorAssign: op = maple::OP_bior; break; + case OPR_BxorAssign: op = maple::OP_bxor; break; + case OPR_ZextAssign: op = maple::OP_zext; break; + default: + break; + } + return op; +} + +#if 1 +const char *Ast2MplBuilder::Type2Label(const maple::MIRType *type) { + maple::PrimType pty = type->GetPrimType(); + switch (pty) { + case maple::PTY_u1: return "Z"; + case maple::PTY_u8: return "B"; + case maple::PTY_i16: return "S"; + case maple::PTY_u16: return "C"; + case maple::PTY_i32: return "I"; + case maple::PTY_i64: return "J"; + case maple::PTY_f32: return "F"; + case maple::PTY_f64: return "D"; + case maple::PTY_void: return "V"; + default: return "L"; + } +} +#else + +const char *Ast2MplBuilder::Type2Label(const maple::MIRType *type) { + maple::PrimType pty = type->GetPrimType(); + switch (pty) { + case maple::PTY_u1: return "Z"; + case maple::PTY_i8: return "C"; + case maple::PTY_u8: return "UC"; + case maple::PTY_i16: return "S"; + case maple::PTY_u16: return "US"; + case maple::PTY_i32: return "I"; + case maple::PTY_u32: return "UI"; + case maple::PTY_i64: return "J"; + case maple::PTY_u64: return "UJ"; + case maple::PTY_f32: return "F"; + case maple::PTY_f64: return "D"; + case maple::PTY_void: return "V"; + default: return "L"; + } +} +#endif + +bool Ast2MplBuilder::IsStmt(TreeNode *tnode) { + bool status = true; + if (!tnode) return false; + + switch (tnode->GetKind()) { + case NK_Parenthesis: { + ParenthesisNode *node = static_cast(tnode); + status = IsStmt(node->GetExpr()); + break; + } + case NK_UnaOperator: { + UnaOperatorNode *node = static_cast(tnode); + OprId ast_op = node->GetOprId(); + if (ast_op != OPR_Inc && ast_op != OPR_Dec) { + status = false; + } + break; + } + case NK_BinOperator: { + BinOperatorNode *bon = static_cast(tnode); + maple::Opcode op = MapBinComboOpcode(bon->GetOprId()); + if (bon->GetOprId() != OPR_Assign && op == maple::OP_undef) { + status = false; + } + break; + } + case NK_Block: + case NK_Break: + case NK_Call: + case NK_CondBranch: + case NK_Delete: + case NK_DoLoop: + case NK_ExprList: + case NK_ForLoop: + case NK_Function: + case NK_Interface: + case NK_Lambda: + case NK_New: + case NK_Return: + case NK_Switch: + case NK_SwitchCase: + case NK_VarList: + case NK_WhileLoop: + status = true; + break; + case NK_Annotation: + case NK_AnnotationType: + case NK_Assert: + case NK_Attr: + case NK_Cast: + case NK_Class: + case NK_Dimension: + case NK_Exception: + case NK_Field: + case NK_Identifier: + case NK_Import: + case NK_InstanceOf: + case NK_Literal: + case NK_Package: + case NK_Pass: + case NK_PrimArrayType: + case NK_PrimType: + case NK_SwitchLabel: + case NK_TerOperator: + case NK_UserType: + default: + status = false; + break; + } + return status; +} + +#define USE_SHORT 1 + +// used to form mangled function name +#if USE_SHORT +#define CLASSEND ";" +#define SEP "|" +#define LARG "_" // "(" +#define RARG "_" // ")" +#else +#define CLASSEND "_3B" // ";" +#define SEP "_7C" // "|" +#define LARG "_28" // "(" +#define RARG "_29" // ")" +#endif + +void Ast2MplBuilder::Type2Name(std::string &str, const maple::MIRType *type) { + maple::PrimType pty = type->GetPrimType(); + const char *n = Type2Label(type); + str.append(n); + if (n[0] == 'L') { + while (type->GetPrimType() == maple::PTY_ptr || type->GetPrimType() == maple::PTY_ref) { + const maple::MIRPtrType *ptype = static_cast(type); + type = ptype->GetPointedType(); + } + str.append(type->GetName()); + str.append(CLASSEND); + } +} + +// update to use uniq name: str --> str|mUniqNum +void Ast2MplBuilder::UpdateUniqName(std::string &str) { + str.append(SEP); + str.append(std::to_string(mUniqNum++)); + return; +} + +// update to use mangled name: className|funcName|(argTypes)retType +void Ast2MplBuilder::UpdateFuncName(maple::MIRFunction *func) { + std::string str; + maple::TyIdx tyIdx = func->GetClassTyIdx(); + maple::MIRType *type; + if (tyIdx.GetIdx() != 0) { + type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx); + str.append(type->GetName()); + str.append(SEP); + } + str.append(func->GetName()); + str.append(SEP); + str.append(LARG); + maple::GStrIdx stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName("this"); + for (size_t i = 0; i < func->GetFormalCount(); i++) { + maple::FormalDef def = func->GetFormalDefAt(i); + // exclude type of extra "this" in the function name + if (stridx == def.formalStrIdx) continue; + maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(def.formalTyIdx); + Type2Name(str, type); + } + str.append(RARG); + type = func->GetReturnType(); + Type2Name(str, type); + + maple::MIRSymbol *funcst = maple::GlobalTables::GetGsymTable().GetSymbolFromStidx(func->GetStIdx().Idx()); + // remove old entry in strIdxToStIdxMap + maple::GlobalTables::GetGsymTable().RemoveFromStringSymbolMap(*funcst); + AST2MPLMSG("UpdateFuncName()", str); + stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(str); + funcst->SetNameStrIdx(stridx); + // add new entry in strIdxToStIdxMap + maple::GlobalTables::GetGsymTable().AddToStringSymbolMap(*funcst); +} + +ClassNode *Ast2MplBuilder::GetSuperClass(ClassNode *klass) { + TreeNode *tnode = klass->GetParent(); + while (tnode && !tnode->IsClass()) { + tnode = tnode->GetParent(); + } + return static_cast(tnode); +} + +BlockNode *Ast2MplBuilder::GetSuperBlock(BlockNode *block) { + TreeNode *tnode = block->GetParent(); + while (tnode && !tnode->IsBlock()) { + tnode = tnode->GetParent(); + } + return static_cast(tnode); +} + +maple::MIRSymbol *Ast2MplBuilder::GetSymbol(TreeNode *tnode, BlockNode *block) { + unsigned idx = tnode->GetStrIdx(); + maple::MIRSymbol *symbol = nullptr; + + // global symbol + if (!block) { + std::pair P(idx, block); + symbol = mNameBlockVarMap[P]; + return symbol; + } + + // trace block hirachy for defined symbol + BlockNode *blk = block; + do { + std::pair P(idx, blk); + symbol = mNameBlockVarMap[P]; + if (symbol) { + return symbol; + } + blk = GetSuperBlock(blk); + } while (blk); + + // check parameters + maple::MIRFunction *func = GetCurrFunc(blk); + if (!func) { + NOTYETIMPL("Block parent hirachy"); + return symbol; + } + maple::GStrIdx stridx = maple::GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(tnode->GetName()); + if (func->IsAFormalName(stridx)) { + maple::FormalDef def = func->GetFormalFromName(stridx); + return def.formalSym; + } + return symbol; +} + +maple::MIRSymbol *Ast2MplBuilder::CreateTempVar(const char *prefix, maple::MIRType *type) { + if (!type) { + return nullptr; + } + std::string str(prefix); + str.append(SEP); + str.append(std::to_string(mUniqNum++)); + maple::MIRFunction *func = mMirModule->CurFunction(); + maple::MIRSymbol *var = mMirBuilder->CreateLocalDecl(str, *type); + return var; +} + +maple::MIRSymbol *Ast2MplBuilder::CreateSymbol(TreeNode *tnode, BlockNode *block) { + std::string name = tnode->GetName(); + maple::MIRType *mir_type; + + if (tnode->IsIdentifier()) { + IdentifierNode *inode = static_cast(tnode); + mir_type = MapType(inode->GetType()); + } else if (tnode->IsLiteral()) { + NOTYETIMPL("CreateSymbol LiteralNode()"); + mir_type = mDefaultType; + } + + // always use pointer type for classes, with PTY_ref + maple::MIRTypeKind kind = mir_type->GetKind(); + if (kind == maple::kTypeClass || kind == maple::kTypeClassIncomplete || + kind == maple::kTypeInterface || kind == maple::kTypeInterfaceIncomplete) { + mir_type = mMirBuilder->GetOrCreatePointerType(mir_type); + } + + maple::MIRSymbol *symbol = nullptr; + if (block) { + maple::MIRFunction *func = GetCurrFunc(block); + symbol = mMirBuilder->GetLocalDecl(name); + std::string str(name); + // symbol with same name already exist, use a uniq new name + if (symbol) { + UpdateUniqName(str); + } + symbol = mMirBuilder->CreateLocalDecl(str, *mir_type); + } else { + symbol = mMirBuilder->GetGlobalDecl(name); + std::string str(name); + // symbol with same name already exist, use a uniq new name + if (symbol) { + UpdateUniqName(str); + } + symbol = mMirBuilder->CreateGlobalDecl(str, *mir_type, maple::kScGlobal); + } + + std::pair P(tnode->GetStrIdx(), block); + mNameBlockVarMap[P] = symbol; + + return symbol; +} + +maple::MIRFunction *Ast2MplBuilder::GetCurrFunc(BlockNode *block) { + maple::MIRFunction *func = nullptr; + // func = mBlockFuncMap[block]; + func = mMirModule->CurFunction(); + return func; +} + +maple::MIRClassType *Ast2MplBuilder::GetClass(BlockNode *block) { + maple::TyIdx tyidx = GetCurrFunc(block)->GetClassTyIdx(); + return (maple::MIRClassType*)maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyidx); +} + +bool3 Ast2MplBuilder::IsCompatibleTo(maple::PrimType expected, maple::PrimType prim) { + if (expected == prim) + return true3; + + maple::PrimitiveType type(prim); + bool3 comp = false3; + switch (expected) { + case maple::PTY_i8: + case maple::PTY_i16: + case maple::PTY_i32: + case maple::PTY_i64: + case maple::PTY_u8: + case maple::PTY_u16: + case maple::PTY_u32: + case maple::PTY_u64: + case maple::PTY_u1: + if (type.IsInteger()) { + comp = true3; + } + break; + case maple::PTY_ptr: + case maple::PTY_ref: + if (type.IsPointer()) { + comp = true3; + } + if (type.IsInteger()) { + comp = maybe3; + } + break; + case maple::PTY_a32: + case maple::PTY_a64: + if (type.IsAddress()) { + comp = true3; + } + break; + case maple::PTY_f32: + case maple::PTY_f64: + case maple::PTY_f128: + if (type.IsFloat()) { + comp = true3; + } + break; + case maple::PTY_c64: + case maple::PTY_c128: + if (type.IsInteger()) { + comp = true3; + } + break; + case maple::PTY_constStr: + case maple::PTY_gen: + case maple::PTY_agg: + case maple::PTY_unknown: + case maple::PTY_v2i64: + case maple::PTY_v4i32: + case maple::PTY_v8i16: + case maple::PTY_v16i8: + case maple::PTY_v2f64: + case maple::PTY_v4f32: + case maple::PTY_void: + default: + break; + } + return comp; +} + +maple::MIRFunction *Ast2MplBuilder::SearchFunc(unsigned idx, maple::MapleVector &args) { + if (mNameFuncMap.find(idx) == mNameFuncMap.end()) { + return nullptr; + } + std::vector candidates; + for (auto it: mNameFuncMap[idx]) { + if (it->GetFormalCount() != args.size()) { + continue; + } + bool matched = true; + bool3 mightmatched = true3; + for (int i = 0; i < it->GetFormalCount(); i++) { + maple::MIRType *type = maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(it->GetFormalDefAt(i).formalTyIdx); + bool3 comp = IsCompatibleTo(type->GetPrimType(), args[i]->GetPrimType()); + if (comp == false3) { + matched = false; + mightmatched = false3; + break; + } else if (comp == maybe3) { + matched = false; + } + } + if (matched) { + return it; + } + if (mightmatched != false3) { + candidates.push_back(it); + } + } + if (candidates.size()) { + return candidates[0]; + } + return nullptr; +} + +maple::MIRFunction *Ast2MplBuilder::SearchFunc(TreeNode *method, maple::MapleVector &args, BlockNode *block) { + maple::MIRFunction *func = nullptr; + switch (method->GetKind()) { + case NK_Function: { + func = SearchFunc(method->GetStrIdx(), args); + break; + } + case NK_Identifier: { + IdentifierNode *imethod = static_cast(method); + func = SearchFunc(imethod->GetStrIdx(), args); + break; + } + case NK_Field: { + FieldNode *node = static_cast(method); + TreeNode *upper = node->GetUpper(); + TreeNode *field = node->GetField(); + if (field->IsIdentifier()) { + // pass upper as this + maple::BaseNode *bn = ProcessNode(SK_Expr, upper, block); + if (bn) { + args[0] = bn; + } + } + func = SearchFunc(field, args, block); + break; + } + default: + NOTYETIMPL("GetFuncName() method to be handled"); + } + + return func; +} + +void Ast2MplBuilder::MapAttr(maple::GenericAttrs &attr, AttrId id) { + switch (id) { +#undef ATTRIBUTE +#define ATTRIBUTE(X) case ATTR_##X: attr.SetAttr(maple::GENATTR_##X); break; +// #include "supported_attributes.def" +ATTRIBUTE(abstract) +ATTRIBUTE(const) +ATTRIBUTE(volatile) +ATTRIBUTE(final) +ATTRIBUTE(native) +ATTRIBUTE(private) +ATTRIBUTE(protected) +ATTRIBUTE(public) +ATTRIBUTE(static) +ATTRIBUTE(default) +ATTRIBUTE(synchronized) + +// ATTRIBUTE(strictfp) + case ATTR_strictfp: attr.SetAttr(maple::GENATTR_strict); break; + + default: + break; + } +} + +void Ast2MplBuilder::MapAttr(maple::GenericAttrs &attr, IdentifierNode *inode) { + // SmallVector mAttrs + unsigned anum = inode->GetAttrsNum(); + for (int i = 0; i < anum; i++) { + const AttrId ast_attr = inode->GetAttrAtIndex(i); + MapAttr(attr, ast_attr); + } +} + +void Ast2MplBuilder::MapAttr(maple::GenericAttrs &attr, FunctionNode *fnode) { + unsigned anum = fnode->GetAttrsNum(); + for (int i = 0; i < anum; i++) { + const AttrId ast_attr = fnode->GetAttrAtIndex(i); + MapAttr(attr, ast_attr); + } +} + +} diff --git a/src/MapleFE/ast2mpl/src/generic_attrs.cpp b/src/MapleFE/ast2mpl/src/generic_attrs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..185d327387249f9ceea60c5fd288970ac463c739 --- /dev/null +++ b/src/MapleFE/ast2mpl/src/generic_attrs.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. + * + * OpenArkCompiler is licensed under 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 "generic_attrs.h" + +namespace maple { +TypeAttrs GenericAttrs::ConvertToTypeAttrs() { + TypeAttrs attr; + constexpr uint32 maxAttrNum = 128; + for (uint32 i = 0; i < maxAttrNum; ++i) { + if (attrFlag[i] == 0) { + continue; + } + auto tA = static_cast(i); + switch (tA) { +#define TYPE_ATTR +#define ATTR(STR) \ + case GENATTR_##STR: \ + attr.SetAttr(ATTR_##STR); \ + break; +#include "all_attributes.def" +#undef ATTR +#undef TYPE_ATTR + default: + ASSERT(false, "unknown TypeAttrs"); + break; + } + } + return attr; +} + +FuncAttrs GenericAttrs::ConvertToFuncAttrs() { + FuncAttrs attr; + constexpr uint32 maxAttrNum = 128; + for (uint32 i = 0; i < maxAttrNum; ++i) { + if (attrFlag[i] == 0) { + continue; + } + auto tA = static_cast(i); + switch (tA) { +#define FUNC_ATTR +#define ATTR(STR) \ + case GENATTR_##STR: \ + attr.SetAttr(FUNCATTR_##STR); \ + break; +#include "all_attributes.def" +#undef ATTR +#undef FUNC_ATTR + default: + ASSERT(false, "unknown FuncAttrs"); + break; + } + } + return attr; +} + +FieldAttrs GenericAttrs::ConvertToFieldAttrs() { + FieldAttrs attr; + constexpr uint32 maxAttrNum = 128; + for (uint32 i = 0; i < maxAttrNum; ++i) { + if (attrFlag[i] == 0) { + continue; + } + auto tA = static_cast(i); + switch (tA) { +#define FIELD_ATTR +#define ATTR(STR) \ + case GENATTR_##STR: \ + attr.SetAttr(FLDATTR_##STR); \ + break; +#include "all_attributes.def" +#undef ATTR +#undef FIELD_ATTR + default: + ASSERT(false, "unknown FieldAttrs"); + break; + } + } + return attr; +} +} \ No newline at end of file diff --git a/src/MapleFE/ast2mpl/src/main.cpp b/src/MapleFE/ast2mpl/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0f89a9114ac926688e5a8b2f6e0d4ff6c9176d8 --- /dev/null +++ b/src/MapleFE/ast2mpl/src/main.cpp @@ -0,0 +1,96 @@ +/* +* Copyright (C) [2020] 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 +#include +#include +#include "gen_astload.h" +#include "ast_handler.h" +#include "ast2mpl.h" + +static void help() { + std::cout << "ast2cpp a.ast[,b.ast] [options]:" << std::endl; + std::cout << " --out=x.cpp : cpp output file" << std::endl; + std::cout << " --help : print this help" << std::endl; + std::cout << " --trace=n : Emit trace with 4-bit combo levels 1...15" << std::endl; + std::cout << " 1 : Emit ast tree visits" << std::endl; + std::cout << " 2 : Emit graph" << std::endl; + std::cout << " --emit-ts-only : Emit ts code only" << std::endl; + std::cout << " --emit-ts : Emit ts code" << std::endl; + std::cout << " --format-cpp : Format cpp" << std::endl; + std::cout << " --no-imported : Do not process the imported modules" << std::endl; + std::cout << "default out name uses the first input name: a.cpp" << std::endl; +} + +int main (int argc, char *argv[]) { + if (argc == 1 || (!strncmp(argv[1], "--help", 6) && (strlen(argv[1]) == 6))) { + help(); + exit(-1); + } + + unsigned flags; + // one or more input .ast files separated by ',' + const char *inputname = argv[1]; + + // Parse the argument + for (unsigned i = 2; i < argc; i++) { + if (!strncmp(argv[i], "--trace=", 8)) { + int val = atoi(argv[i] + 8); + if (val < 1 || val > 15) { + help(); + exit(-1); + } + flags |= val; + } else { + std::cerr << "unknown option " << argv[i] << std::endl; + exit(-1); + } + } + + // input ast files + std::vector inputfiles; + if (inputname) { + std::stringstream ss; + ss.str(inputname); + std::string item; + while (std::getline(ss, item, ',')) { + // std::cout << "item " << item << " xxx"<< std::endl; + inputfiles.push_back(item); + } + } + + unsigned trace = (flags & maplefe::FLG_trace); + maplefe::AST_Handler handler(trace); + for (auto astfile: inputfiles) { + std::ifstream input(astfile, std::ifstream::binary); + input >> std::noskipws; + std::istream_iterator s(input), e; + maplefe::AstBuffer vec(s, e); + maplefe::AstLoad loadAst; + maplefe::ModuleNode *mod = loadAst.LoadFromAstBuf(vec); + // add mod to the vector + while(mod) { + handler.AddModule(mod); + mod = loadAst.Next(); + } + } + + maplefe::A2M *a2m = new maplefe::A2M(&handler, flags); + int res = a2m->ProcessAST(); + + delete a2m; + + return 0; +} diff --git a/src/MapleFE/ast2mpl/src/mpl_processor.cpp b/src/MapleFE/ast2mpl/src/mpl_processor.cpp index 067266a4356a18fca4bce82fc5fe3774780a4012..01f24456e1e0a188101befa6bcfc1d32c5f7baf9 100644 --- a/src/MapleFE/ast2mpl/src/mpl_processor.cpp +++ b/src/MapleFE/ast2mpl/src/mpl_processor.cpp @@ -13,12 +13,12 @@ * See the Mulan PSL v2 for more details. */ -#include "ast2mpl.h" +#include "ast2mpl_builder.h" #include "stringpool.h" namespace maplefe { -maple::BaseNode *A2M::ProcessNodeDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNodeDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { if (!tnode) { return nullptr; } @@ -48,7 +48,7 @@ maple::BaseNode *A2M::ProcessNodeDecl(StmtExprKind skind, TreeNode *tnode, Block return mpl_node; } -maple::BaseNode *A2M::ProcessNode(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNode(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { if (!tnode) { return nullptr; } @@ -65,41 +65,41 @@ maple::BaseNode *A2M::ProcessNode(StmtExprKind skind, TreeNode *tnode, BlockNode return mpl_node; } -maple::BaseNode *A2M::ProcessModule(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessModule(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessModule()"); ModuleNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessPackage(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessPackage(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessPackage()"); PackageNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessDeclare(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessDeclare(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessImport(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessImport(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessImport()"); ImportNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessExport(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessExport(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessExport()"); ExportNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessXXportAsPair(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessXXportAsPair(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessXXportAsPair()"); XXportAsPairNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessIdentifier(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessIdentifier(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { IdentifierNode *node = static_cast(tnode); std::string name = node->GetName(); @@ -157,7 +157,7 @@ maple::BaseNode *A2M::ProcessIdentifier(StmtExprKind skind, TreeNode *tnode, Blo return mMirBuilder->CreateExprDread(symbol); } -maple::BaseNode *A2M::ProcessField(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessField(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { FieldNode *node = static_cast(tnode); maple::BaseNode *bn = nullptr; @@ -246,7 +246,7 @@ maple::BaseNode *A2M::ProcessField(StmtExprKind skind, TreeNode *tnode, BlockNod return bn; } -maple::BaseNode *A2M::ProcessFieldDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFieldDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { FieldNode *node = static_cast(tnode); maple::BaseNode *bn = nullptr; @@ -318,60 +318,60 @@ maple::BaseNode *A2M::ProcessFieldDecl(StmtExprKind skind, TreeNode *tnode, Bloc return bn; } -maple::BaseNode *A2M::ProcessDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { DeclNode *node = static_cast(tnode); TreeNode *vars = node->GetVar(); return ProcessNode(skind, vars, block); } -maple::BaseNode *A2M::ProcessAssert(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAssert(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessAssert()"); AssertNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessDimension(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessDimension(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessDimension()"); DimensionNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessAttr(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAttr(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessAttr()"); // AttrNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessPrimType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessPrimType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessPrimType()"); PrimTypeNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessPrimArrayType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessPrimArrayType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessPrimArrayType()"); PrimArrayTypeNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessUserType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessUserType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessUserType()"); UserTypeNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessCast(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessCast(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessCast()"); CastNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessParenthesis(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessParenthesis(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { ParenthesisNode *node = static_cast(tnode); return ProcessNode(skind, node->GetExpr(), block); } -maple::BaseNode *A2M::ProcessVarList(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessVarList(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { VarListNode *node = static_cast(tnode); for (int i = 0; i < node->GetVarsNum(); i++) { TreeNode *n = node->GetVarAtIndex(i); @@ -398,17 +398,17 @@ maple::BaseNode *A2M::ProcessVarList(StmtExprKind skind, TreeNode *tnode, BlockN return nullptr; } -maple::BaseNode *A2M::ProcessExprList(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessExprList(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessExprList()"); ExprListNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessNamespace(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNamespace(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { LiteralNode *node = static_cast(tnode); LitData data = node->GetData(); maple::MIRType *type = nullptr; @@ -478,7 +478,7 @@ maple::BaseNode *A2M::ProcessLiteral(StmtExprKind skind, TreeNode *tnode, BlockN return bn; } -maple::BaseNode *A2M::ProcessUnaOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessUnaOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { UnaOperatorNode *node = static_cast(tnode); OprId ast_op = node->GetOprId(); TreeNode *ast_rhs = node->GetOpnd(); @@ -500,7 +500,7 @@ maple::BaseNode *A2M::ProcessUnaOperator(StmtExprKind skind, TreeNode *tnode, Bl return mpl_node; } -maple::BaseNode *A2M::ProcessBinOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBinOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { BinOperatorNode *bon = static_cast(tnode); OprId ast_op = bon->GetOprId(); TreeNode *ast_lhs = bon->GetOpndA(); @@ -547,69 +547,69 @@ maple::BaseNode *A2M::ProcessBinOperator(StmtExprKind skind, TreeNode *tnode, Bl return mpl_node; } -maple::BaseNode *A2M::ProcessTerOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTerOperator(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessTerOperator()"); TerOperatorNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessLambda(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessLambda(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessLambda()"); LambdaNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessInstanceOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessInstanceOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessInstanceOf()"); InstanceOfNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessTemplateLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTemplateLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessTemplateLiteral()"); TemplateLiteralNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessRegExpr(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessRegExpr(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessIn(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessIn(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessIn()"); InNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessComputedName(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessComputedName(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessIs(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessIs(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessIs()"); IsNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessTypeOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTypeOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessTypeOf()"); TypeOfNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessAwait(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAwait(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessKeyOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessKeyOf(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessInfer(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessInfer(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessBlockDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBlockDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { BlockNode *ast_block = static_cast(tnode); for (int i = 0; i < ast_block->GetChildrenNum(); i++) { TreeNode *child = ast_block->GetChildAtIndex(i); @@ -618,7 +618,7 @@ maple::BaseNode *A2M::ProcessBlockDecl(StmtExprKind skind, TreeNode *tnode, Bloc return nullptr; } -maple::BaseNode *A2M::ProcessBlock(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBlock(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { BlockNode *ast_block = static_cast(tnode); if (block) { // promote statements to parent @@ -638,13 +638,13 @@ maple::BaseNode *A2M::ProcessBlock(StmtExprKind skind, TreeNode *tnode, BlockNod return nullptr; } -maple::BaseNode *A2M::ProcessFunction(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFunction(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { MASSERT(tnode->IsFunction() && "it is not an FunctionNode"); NOTYETIMPL("ProcessFunction()"); return nullptr; } -maple::BaseNode *A2M::ProcessFuncDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFuncDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { FunctionNode *ast_func = static_cast(tnode); std::string name = ast_func->GetName(); // SmallVector mAttrs; @@ -759,7 +759,7 @@ maple::BaseNode *A2M::ProcessFuncDecl(StmtExprKind skind, TreeNode *tnode, Block return nullptr; } -maple::BaseNode *A2M::ProcessFuncSetup(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFuncSetup(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { FunctionNode *ast_func = static_cast(tnode); maple::MIRFunction *func = mFuncMap[ast_func]; @@ -788,7 +788,7 @@ maple::BaseNode *A2M::ProcessFuncSetup(StmtExprKind skind, TreeNode *tnode, Bloc return nullptr; } -maple::BaseNode *A2M::ProcessClassDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessClassDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { ClassNode *classnode = static_cast(tnode); std::string name = classnode->GetName(); TreeNode *parent = GetSuperClass(classnode); @@ -834,7 +834,7 @@ maple::BaseNode *A2M::ProcessClassDecl(StmtExprKind skind, TreeNode *tnode, Bloc return nullptr; } -maple::BaseNode *A2M::ProcessClass(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessClass(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { ClassNode *classnode = static_cast(tnode); std::string name = classnode->GetName(); maple::MIRType *type = mNodeTypeMap[classnode->GetStrIdx()]; @@ -859,142 +859,142 @@ maple::BaseNode *A2M::ProcessClass(StmtExprKind skind, TreeNode *tnode, BlockNod return nullptr; } -maple::BaseNode *A2M::ProcessInterfaceDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessInterfaceDecl(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessInterfaceDecl()"); InterfaceNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessInterface(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessInterface(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessInterface()"); InterfaceNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessArrayElement(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessArrayElement(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessArrayElement()"); ArrayElementNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessArrayLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessArrayLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessArrayLiteral()"); ArrayLiteralNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessNumIndexSig(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNumIndexSig(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessStrIndexSig(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessStrIndexSig(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessBindingElement(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBindingElement(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessBindingPattern(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBindingPattern(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessTypeAlias(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTypeAlias(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessAsType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAsType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessConditionalType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessConditionalType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessTypeParameter(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTypeParameter(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessNameTypePair(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNameTypePair(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessTupleType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTupleType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessStruct(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessStruct(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessStruct()"); StructNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessStructLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessStructLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessStructLiteral()"); StructLiteralNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessFieldLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFieldLiteral(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessFieldLiteral()"); FieldLiteralNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessAnnotationType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAnnotationType(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessAnnotationType()"); AnnotationTypeNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessAnnotation(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessAnnotation(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessAnnotation()"); AnnotationNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessTry(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessTry(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessTry()"); TryNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessCatch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessCatch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessCatch()"); CatchNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessFinally(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessFinally(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessFinally()"); FinallyNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessThrow(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessThrow(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessThrow()"); ThrowNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessException(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessException(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessException()"); ExceptionNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessReturn(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessReturn(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { ReturnNode *node = static_cast(tnode); maple::BaseNode *val = ProcessNode(SK_Expr, node->GetResult(), block); maple::NaryStmtNode *stmt = mMirBuilder->CreateStmtReturn(val); return stmt; } -maple::BaseNode *A2M::ProcessYield(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessYield(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { return nullptr; } -maple::BaseNode *A2M::ProcessCondBranch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessCondBranch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { CondBranchNode *node = static_cast(tnode); maple::BaseNode *cond = ProcessNode(SK_Expr, node->GetCond(), block); if (!cond) { @@ -1025,19 +1025,19 @@ maple::BaseNode *A2M::ProcessCondBranch(StmtExprKind skind, TreeNode *tnode, Blo return ifnode; } -maple::BaseNode *A2M::ProcessBreak(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessBreak(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessBreak()"); BreakNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessContinue(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessContinue(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessContinue()"); ContinueNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessLoopCondBody(StmtExprKind skind, TreeNode *cond, TreeNode *body, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessLoopCondBody(StmtExprKind skind, TreeNode *cond, TreeNode *body, BlockNode *block) { maple::BaseNode *mircond = ProcessNode(SK_Expr, cond, block); if (!mircond) { NOTYETIMPL("ProcessLoopCondBody() condition"); @@ -1058,7 +1058,7 @@ maple::BaseNode *A2M::ProcessLoopCondBody(StmtExprKind skind, TreeNode *cond, Tr return nullptr; } -maple::BaseNode *A2M::ProcessForLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessForLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { ForLoopNode *node = static_cast(tnode); maple::BlockNode *mblock = mBlockNodeMap[block]; maple::BaseNode *bn = nullptr; @@ -1090,17 +1090,17 @@ maple::BaseNode *A2M::ProcessForLoop(StmtExprKind skind, TreeNode *tnode, BlockN return nullptr; } -maple::BaseNode *A2M::ProcessWhileLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessWhileLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { WhileLoopNode *node = static_cast(tnode); return ProcessLoopCondBody(skind, node->GetCond(), node->GetBody(), block); } -maple::BaseNode *A2M::ProcessDoLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessDoLoop(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { DoLoopNode *node = static_cast(tnode); return ProcessLoopCondBody(skind, node->GetCond(), node->GetBody(), block); } -maple::BaseNode *A2M::ProcessNew(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessNew(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NewNode *node = static_cast(tnode); maple::BaseNode *bn = nullptr; @@ -1155,13 +1155,13 @@ maple::BaseNode *A2M::ProcessNew(StmtExprKind skind, TreeNode *tnode, BlockNode return bn; } -maple::BaseNode *A2M::ProcessDelete(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessDelete(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessDelete()"); DeleteNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessCall(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessCall(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { CallNode *node = static_cast(tnode); maple::MapleVector args(mMirModule->CurFuncCodeMemPoolAllocator()->Adapter()); maple::MIRFunction *func = GetCurrFunc(block); @@ -1207,25 +1207,25 @@ maple::BaseNode *A2M::ProcessCall(StmtExprKind skind, TreeNode *tnode, BlockNode } } -maple::BaseNode *A2M::ProcessSwitchLabel(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessSwitchLabel(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessSwitchLabel()"); SwitchLabelNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessSwitchCase(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessSwitchCase(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessSwitchCase()"); SwitchCaseNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessSwitch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessSwitch(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { NOTYETIMPL("ProcessSwitch()"); SwitchNode *node = static_cast(tnode); return nullptr; } -maple::BaseNode *A2M::ProcessPass(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::ProcessPass(StmtExprKind skind, TreeNode *tnode, BlockNode *block) { PassNode *node = static_cast(tnode); maple::BlockNode *blk = mBlockNodeMap[block]; maple::BaseNode *stmt = nullptr; @@ -1243,7 +1243,7 @@ maple::BaseNode *A2M::ProcessPass(StmtExprKind skind, TreeNode *tnode, BlockNode return nullptr; } -maple::BaseNode *A2M::ProcessUnaOperatorMpl(StmtExprKind skind, +maple::BaseNode *Ast2MplBuilder::ProcessUnaOperatorMpl(StmtExprKind skind, maple::Opcode op, maple::BaseNode *bn, BlockNode *block) { @@ -1288,7 +1288,7 @@ maple::BaseNode *A2M::ProcessUnaOperatorMpl(StmtExprKind skind, return node; } -maple::BaseNode *A2M::ProcessBinOperatorMplAssign(StmtExprKind skind, +maple::BaseNode *Ast2MplBuilder::ProcessBinOperatorMplAssign(StmtExprKind skind, maple::BaseNode *lhs, maple::BaseNode *rhs, BlockNode *block) { @@ -1317,7 +1317,7 @@ maple::BaseNode *A2M::ProcessBinOperatorMplAssign(StmtExprKind skind, return node; } -maple::BaseNode *A2M::ProcessBinOperatorMplComboAssign(StmtExprKind skind, +maple::BaseNode *Ast2MplBuilder::ProcessBinOperatorMplComboAssign(StmtExprKind skind, maple::Opcode op, maple::BaseNode *lhs, maple::BaseNode *rhs, @@ -1327,7 +1327,7 @@ maple::BaseNode *A2M::ProcessBinOperatorMplComboAssign(StmtExprKind skind, return assign; } -maple::BaseNode *A2M::ProcessBinOperatorMplArror(StmtExprKind skind, +maple::BaseNode *Ast2MplBuilder::ProcessBinOperatorMplArror(StmtExprKind skind, maple::BaseNode *lhs, maple::BaseNode *rhs, BlockNode *block) { @@ -1336,7 +1336,7 @@ maple::BaseNode *A2M::ProcessBinOperatorMplArror(StmtExprKind skind, } // Lhs = new ... -maple::BaseNode *A2M::GetNewNodeLhs(NewNode *node, BlockNode *block) { +maple::BaseNode *Ast2MplBuilder::GetNewNodeLhs(NewNode *node, BlockNode *block) { maple::BaseNode *obj = nullptr; TreeNode *id = node->GetId(); diff --git a/src/MapleFE/astopt/include/astopt.h b/src/MapleFE/astopt/include/astopt.h index 9de0985aba1cef1ef17c8b82491f617a37f55fc0..4485a040232333306e311d0d02eb3b66979e5763 100644 --- a/src/MapleFE/astopt/include/astopt.h +++ b/src/MapleFE/astopt/include/astopt.h @@ -51,7 +51,7 @@ public: public: explicit AstOpt(AST_Handler *h, unsigned f); - ~AstOpt() { delete mASTHandler; } + ~AstOpt() {} AST_Handler *GetASTHandler() {return mASTHandler;} AST_XXport *GetASTXXport() {return mASTXXport;} @@ -74,6 +74,8 @@ class BuildNodeIdToNodeVisitor : public AstVisitor { Module_Handler *mHandler; unsigned mFlags; + std::unordered_map mRenameMap; + public: explicit BuildNodeIdToNodeVisitor(AstOpt *opt, unsigned f, bool base = false) : mAstOpt(opt), mFlags(f), AstVisitor((f & FLG_trace_1) && base) {} diff --git a/src/MapleFE/astopt/src/ast_adj.cpp b/src/MapleFE/astopt/src/ast_adj.cpp index 9f6156d0128d398988d52198fd547d242e0ec4d7..e83ccb9ea6d855229076967c60536c6b679c41f0 100644 --- a/src/MapleFE/astopt/src/ast_adj.cpp +++ b/src/MapleFE/astopt/src/ast_adj.cpp @@ -409,9 +409,9 @@ NamespaceNode *AdjustASTVisitor::VisitNamespaceNode(NamespaceNode *node) { node->SetId(upper); NamespaceNode *ns = mHandler->NewTreeNode(); - // name node with field + // name ns with field ns->SetId(field); - // move elements of node ot ns + // move elements of node to ns for (unsigned i = 0; i < node->GetElementsNum(); i++) { ns->AddElement(node->GetElementAtIndex(i)); } diff --git a/src/MapleFE/envsetup.sh b/src/MapleFE/envsetup.sh index be228e7e6afcb663a196b72e0e1d08d9d3595f60..a909f571d940a5466575f008b7f8216dd005900d 100755 --- a/src/MapleFE/envsetup.sh +++ b/src/MapleFE/envsetup.sh @@ -38,7 +38,7 @@ fi export SRCLANG=$LANGSRC export MAPLEFE_ROOT=$(cd $(dirname ${BASH_SOURCE[0]}); pwd) -export MAPLE_ROOT=$(dirname ${MAPLEFE_ROOT}) +export MAPLE_ROOT=$(cd ${MAPLEFE_ROOT}/../../..; pwd) unset MAPLEALL_ROOT export MAPLEALL_ROOT=${MAPLE_ROOT}/OpenArkCompiler diff --git a/src/MapleFE/java/include/ast2mpl_java.h b/src/MapleFE/java/include/ast2mpl_java.h deleted file mode 100644 index 088a13fea2fd905374453902308287466aa2176e..0000000000000000000000000000000000000000 --- a/src/MapleFE/java/include/ast2mpl_java.h +++ /dev/null @@ -1,40 +0,0 @@ -/* -* Copyright (C) [2020] 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. -*/ - -///////////////////////////////////////////////////////////////////////////////// -// Java Specific AST2MPL // -///////////////////////////////////////////////////////////////////////////////// - -#ifndef __AST2MPL_JAVA_H__ -#define __AST2MPL_JAVA_H__ - -#include "ast2mpl.h" - -namespace maplefe { - -class A2MJava : public A2M { -private: -public: - A2MJava(ModuleNode *m) : A2M(m) { } - - const char *Type2Label(const maple::MIRType *type); - - maple::PrimType MapPrim(TypeId id); - maple::MIRType *MapPrimType(TypeId id); - maple::MIRType *MapPrimType(PrimTypeNode *ptnode); -}; - -} -#endif diff --git a/src/MapleFE/java/src/Makefile b/src/MapleFE/java/src/Makefile index 0d6c99e6369042ffad73d5bf4557685f1ef568e1..7715078eb86192d2422d3f29aec0b0c0a0a5c5aa 100644 --- a/src/MapleFE/java/src/Makefile +++ b/src/MapleFE/java/src/Makefile @@ -13,6 +13,7 @@ # include ../../Makefile.in +BUILDBIN=$(BUILDDIR)/bin BUILD=$(BUILDDIR)/java BUILDGEN=$(BUILDDIR)/gen BUILDASTGEN=$(BUILDDIR)/ast_gen/shared @@ -30,7 +31,6 @@ OBJS :=$(foreach obj,$(OBJ), $(BUILD)/$(obj)) $(OBJG) DEPS :=$(foreach dep,$(DEP), $(BUILD)/$(dep)) $(DEPG) INCLUDES := -I $(MAPLEFE_ROOT)/shared/include \ - -I $(MAPLEFE_ROOT)/ast2mpl/include \ -I $(MAPLEFE_ROOT)/java/include \ -I $(MAPLEFE_ROOT)/autogen/include \ -I ${BUILDDIR}/ast_gen/shared \ @@ -38,9 +38,9 @@ INCLUDES := -I $(MAPLEFE_ROOT)/shared/include \ INCLUDEGEN := -I $(BUILDGEN) -I $(MAPLEFE_ROOT)/shared/include -TARGET=java2mpl +TARGET=java2ast -SHAREDLIB = $(BUILDDIR)/ast2mpl/ast2mpl.a $(BUILDDIR)/shared/shared.a $(BUILDASTGEN)/genast.a +SHAREDLIB = $(BUILDDIR)/shared/shared.a $(BUILDASTGEN)/genast.a .PHONY: all all: $(BUILD)/$(TARGET) @@ -73,7 +73,8 @@ $(BUILDGEN)/%.d : $(BUILDGEN)/%.cpp # TARGET depends on OBJS and shared OBJS from shared directory # as well as mapleall libraries $(BUILD)/$(TARGET): $(OBJS) $(SHAREDLIB) - $(LD) -o $(BUILD)/$(TARGET) $(OBJS) $(SHAREDLIB) $(MAPLELIBS) + @mkdir -p $(BUILDBIN) + $(LD) -o $(BUILDBIN)/$(TARGET) $(OBJS) $(SHAREDLIB) $(MAPLELIBS) clean: rm -rf $(BUILD) diff --git a/src/MapleFE/java/src/ast2mpl_java.cpp b/src/MapleFE/java/src/ast2mpl_java.cpp deleted file mode 100644 index 7a61e67610bac935d3d7943162c81bcc0bd6f0a3..0000000000000000000000000000000000000000 --- a/src/MapleFE/java/src/ast2mpl_java.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* -* Copyright (C) [2020] 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 "ast2mpl_java.h" - -namespace maplefe { - -maple::PrimType A2MJava::MapPrim(TypeId id) { - maple::PrimType prim; - switch (id) { - case TY_Boolean: prim = maple::PTY_u1; break; - case TY_Byte: prim = maple::PTY_u8; break; - case TY_Short: prim = maple::PTY_i16; break; - case TY_Int: prim = maple::PTY_i32; break; - case TY_Long: prim = maple::PTY_i64; break; - case TY_Char: prim = maple::PTY_u16; break; - case TY_Float: prim = maple::PTY_f32; break; - case TY_Double: prim = maple::PTY_f64; break; - case TY_Void: prim = maple::PTY_void; break; - case TY_Null: prim = maple::PTY_void; break; - default: MASSERT("Unsupported PrimType"); break; - } - return prim; -} - -maple::MIRType *A2MJava::MapPrimType(TypeId id) { - maple::PrimType prim = MapPrim(id); - maple::TyIdx tid(prim); - return maple::GlobalTables::GetTypeTable().GetTypeFromTyIdx(tid); -} - -maple::MIRType *A2MJava::MapPrimType(PrimTypeNode *ptnode) { - return MapPrimType(ptnode->GetPrimType()); -} - -const char *A2MJava::Type2Label(const maple::MIRType *type) { - maple::PrimType pty = type->GetPrimType(); - switch (pty) { - case maple::PTY_u1: return "Z"; - case maple::PTY_u8: return "B"; - case maple::PTY_i16: return "S"; - case maple::PTY_u16: return "C"; - case maple::PTY_i32: return "I"; - case maple::PTY_i64: return "J"; - case maple::PTY_f32: return "F"; - case maple::PTY_f64: return "D"; - case maple::PTY_void: return "V"; - default: return "L"; - } -} - -} diff --git a/src/MapleFE/java/src/main.cpp b/src/MapleFE/java/src/main.cpp index 2917222f9fb3c9eaf4d29d11f9e87a4bc00f0371..e90b50d01f5137deaea432f63fb09184bf69b3b2 100644 --- a/src/MapleFE/java/src/main.cpp +++ b/src/MapleFE/java/src/main.cpp @@ -17,8 +17,10 @@ #include "common_header_autogen.h" #include "ruletable_util.h" #include "gen_summary.h" +#include "gen_aststore.h" +#include "gen_astdump.h" +#include "gen_astgraph.h" #include "vfy_java.h" -#include "ast2mpl_java.h" static void help() { std::cout << "java2mpl sourcefile [options]:\n" << std::endl; @@ -35,6 +37,8 @@ static void help() { std::cout << " --trace-patch-was-succ : Trace Patching of WasSucc nodes" << std::endl; std::cout << " --trace-warning : Print Warning" << std::endl; std::cout << " --trace-a2m : Trace MPL Builder" << std::endl; + std::cout << " --dump-ast : Dump AST in text format" << std::endl; + std::cout << " --dump-dot : Dump AST in dot format" << std::endl; } int main (int argc, char *argv[]) { @@ -45,7 +49,9 @@ int main (int argc, char *argv[]) { maplefe::Parser *parser = new maplefe::Parser(argv[1]); - bool trace_a2m = false; + bool dump_ast = false; + bool dump_dot = false; + bool succ; // Parse the argument for (unsigned i = 2; i < argc; i++) { @@ -71,8 +77,10 @@ int main (int argc, char *argv[]) { parser->mTracePatchWasSucc = true; } else if (!strncmp(argv[i], "--trace-warning", 15) && (strlen(argv[i]) == 15)) { parser->mTraceWarning = true; - } else if (!strncmp(argv[i], "--trace-a2m", 11) && (strlen(argv[i]) == 11)) { - trace_a2m = true; + } else if (!strncmp(argv[i], "--dump-ast", 10) && (strlen(argv[i]) == 10)) { + dump_ast = true; + } else if (!strncmp(argv[i], "--dump-dot", 10) && (strlen(argv[i]) == 10)) { + dump_dot = true; } else { std::cerr << "unknown option " << argv[i] << std::endl; exit(-1); @@ -80,20 +88,40 @@ int main (int argc, char *argv[]) { } parser->InitRecursion(); - parser->Parse(); + succ = parser->Parse(); + if (!succ) { + delete parser; + return 1; + } + // the module from parser maplefe::ModuleNode *module = parser->GetModule(); maplefe::VerifierJava vfy_java(module); vfy_java.Do(); - maplefe::A2MJava *a2m = new maplefe::A2MJava(module); - a2m->ProcessAST(trace_a2m); + if(dump_ast) { + maplefe::AstDump astdump(module); + astdump.Dump("ts2ast: Initial AST", &std::cout); + } + + if(dump_dot) { + maplefe::AstGraph graph(module); + graph.DumpGraph("ts2ast: Initial AST", &std::cout); + } - a2m->mMirModule->OutputAsciiMpl("", ".mpl"); + maplefe::AstStore saveAst(module); + saveAst.StoreInAstBuf(); + maplefe::AstBuffer &ast_buf = saveAst.GetAstBuf(); - delete parser; - delete a2m; + std::ofstream ofs; + std::string fname(module->GetFilename()); + fname += ".ast"; + ofs.open(fname, std::ofstream::out); + const char *addr = (const char *)(&(ast_buf[0])); + ofs.write(addr, ast_buf.size()); + ofs.close(); + delete parser; return 0; } diff --git a/src/MapleFE/scripts/build_mapleall.sh b/src/MapleFE/scripts/build_mapleall.sh index b3f7a5de004590b1d8a8da6f5c9917d3685ae576..c1c5d7375cdb59f496a9c361166f40eab0b2a456 100755 --- a/src/MapleFE/scripts/build_mapleall.sh +++ b/src/MapleFE/scripts/build_mapleall.sh @@ -17,14 +17,12 @@ set -e if [ ! -d $MAPLEALL_ROOT ]; then cd $MAPLE_ROOT - git clone https://gitee.com/openarkcompiler/OpenArkCompiler.git + git clone https://gitee.com/openarkcompiler/OpenArkCompiler.git -b dev_MapleFE fi cd $MAPLEALL_ROOT -git checkout master +git checkout dev_MapleFE git pull -# use a version that is compatible at moment -git checkout 2a89d0a89 source build/envsetup.sh arm debug make setup make clobber diff --git a/src/MapleFE/test/Makefile b/src/MapleFE/test/Makefile index 4d7ba955b0a91e77aa0a9a509798c8d0908cd8d9..aed83f9c0ad98a99312e7e2785030cbc13cd311a 100644 --- a/src/MapleFE/test/Makefile +++ b/src/MapleFE/test/Makefile @@ -36,7 +36,7 @@ vpath %.cpp $(DIRS) .PHONY: $(SRCLANG) .PHONY: mssetup -$(SRCLANG): mssetup +$(SRCLANG): env LANG=en_US.UTF-8 ./new_runtests.pl $(SRCLANG) full: @@ -59,13 +59,22 @@ mssetup: % : %.java @cp $< $(build) - @echo gdb --args ../output/java/java/java2mpl $(build)/$@.java $(FLAGS) - ../output/java/java/java2mpl $(build)/$@.java $(FLAGS) + @echo gdb --args ../output/java/bin/java2ast $(build)/$@.java $(FLAGS1) + @echo gdb --args ../output/java/bin/ast2mpl $(build)/$@.java.ast $(FLAGS2) + ../output/java/bin/java2ast $(build)/$@.java $(FLAGS1) + ../output/java/bin/ast2mpl $(build)/$@.java.ast $(FLAGS2) @echo "\n======================= $@.java =========================" @cat -n $(build)/$@.java + @echo "========================================================\n" + @echo gdb --args ../output/java/bin/java2ast $(build)/$@.java $(FLAGS1) + @echo gdb --args ../output/java/bin/ast2mpl $(build)/$@.java.ast $(FLAGS2) @echo "\n======================= $@.mpl =========================" @cat -n $(build)/$@.mpl @echo "========================================================\n" + @echo gdb --args ../output/java/bin/java2ast $(build)/$@.java $(FLAGS1) + @echo gdb --args ../output/java/bin/ast2mpl $(build)/$@.java.ast $(FLAGS2) + @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 diff --git a/src/MapleFE/test/java/java2mpl/illegal-expr-2.java b/src/MapleFE/test/java/java2mpl/illegal-expr-2.java.disabled similarity index 100% rename from src/MapleFE/test/java/java2mpl/illegal-expr-2.java rename to src/MapleFE/test/java/java2mpl/illegal-expr-2.java.disabled diff --git a/src/MapleFE/test/java/java2mpl/illegal-expr.java b/src/MapleFE/test/java/java2mpl/illegal-expr.java.disabled similarity index 100% rename from src/MapleFE/test/java/java2mpl/illegal-expr.java rename to src/MapleFE/test/java/java2mpl/illegal-expr.java.disabled diff --git a/src/MapleFE/test/new_runtests.pl b/src/MapleFE/test/new_runtests.pl index d1a678b8f9e498b8938f8f7c79d8b28cdba04a40..06479ff92804476597d5731effb295d0039554d3 100755 --- a/src/MapleFE/test/new_runtests.pl +++ b/src/MapleFE/test/new_runtests.pl @@ -37,13 +37,15 @@ my $outroot = ''; print "Running $lang\n"; if ($lang =~ /\Qjava\E/) { $pinput = "java"; - $cmnd = "../output/java/java/java2mpl"; - $cmnd1 = "echo"; + $cmnd = "../output/java/bin/java2ast"; + $cmnd1 = "../output/java/bin/ast2mpl"; + $flag = ""; $outroot = "$currdir/../output/$pinput/test"; } elsif ($lang =~ /\Qtypescript\E/) { $pinput = "ts"; $cmnd = "../output/typescript/bin/ts2ast"; $cmnd1 = "../output/typescript/bin/ast2cpp"; + $flag = "--no-imported"; $outroot = "$currdir/../output/typescript/test"; } else { print "$lang is an invalid option\n"; @@ -103,6 +105,7 @@ foreach my $file (@paths) { #my $res = system("$pwd/$cmnd $outroot/$file > $outroot/$outresult"); #my $res = system('$pwd/$cmnd $outroot/$file; $pwd/$cmnd1 $outroot/$file.ast > $outroot/$outresult'); my $res = system("$pwd/$cmnd $outroot/$file > $outroot/$outresult"); + #print "$pwd/$cmnd $outroot/$file > $outroot/$outresult"; if ($res > 0) { print " ==$pinput===> $file\n"; @@ -112,7 +115,8 @@ foreach my $file (@paths) { #print "---------------------------\n"; next; } else { - my $res1 = system("$cmnd1 $outroot/$file.ast --no-imported > $outroot/$outresult.1"); + my $res1 = system("$cmnd1 $outroot/$file.ast $flag > $outroot/$outresult.1"); + #print "$cmnd1 $outroot/$file.ast $flag > $outroot/$outresult.1"; if ($res1 > 0) { print " ==$pinput===> $file\n"; diff --git a/src/MapleFE/test/typescript/ts2cxx-test.sh b/src/MapleFE/test/typescript/ts2cxx-test.sh index 4a294dfaa32e68129ca6e8b635020f0c979faea5..598b802b4af165683737822c591329cf3b469585 100755 --- a/src/MapleFE/test/typescript/ts2cxx-test.sh +++ b/src/MapleFE/test/typescript/ts2cxx-test.sh @@ -2,8 +2,10 @@ # Usage: cd MapleFE/test/typescript/unit_tests; ../ts2cpp-test.sh *.ts [ $# -lt 1 ] && exec $0 $(git ls-files "*.ts") SUCC= -TSOUT=$(cd $(dirname $0)/../../; pwd)/output/typescript -RTSRC=$(cd $(dirname $0)/../../; pwd)/ast2cpp/runtime/src +MPLFEPATH=$(cd $(dirname $0)/../../; pwd) +TSOUT=$MPLFEPATH/output/typescript +RTSRC=$MPLFEPATH/ast2cpp/runtime/src +RTINC=$MPLFEPATH/ast2cpp/runtime/include TS2AST=$TSOUT/bin/ts2ast AST2CPP=$TSOUT/bin/ast2cpp TSCSH=$(dirname $0)/tsc.sh @@ -46,7 +48,7 @@ for f in $list; do done dep=$(echo $dep | xargs -n1 | sort -u) $AST2CPP $f.ast || { echo "(ast2cpp)$f" >> ts2cpp.failures.out; break; } - g++ -std=c++17 -g $t.cpp $RTSRC/*.cpp $dep -o $t.out || { echo "(g++)$f" >> ts2cpp.failures2.out; break; } + g++ -std=c++17 -g -I$RTINC $t.cpp $RTSRC/*.cpp $dep -o $t.out || { echo "(g++)$f" >> ts2cpp.failures2.out; break; } ./$t.out 2>&1 > $f-run.out || { echo "(run)$f" >> ts2cpp.failures2.out; break; } $TSCSH $f diff $f-run.out $f-nodejs.out diff --git a/src/MapleFE/test/typescript/unit_tests/conditional-type3.ts.result b/src/MapleFE/test/typescript/unit_tests/conditional-type3.ts.result new file mode 100644 index 0000000000000000000000000000000000000000..916713c050178409e45d488605ab3d378df5f608 --- /dev/null +++ b/src/MapleFE/test/typescript/unit_tests/conditional-type3.ts.result @@ -0,0 +1,7 @@ +Matched 43 tokens. +Matched 83 tokens. +============= Module =========== +== Sub Tree == + type Names = intersect = {[K in keyof T] : T[K] extends (args) -> ? K : never }[ keyof T] & string +== Sub Tree == + type MyType = Required[M] extends (args) -> ? string : number diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index 0007fd82cb558bdd399f818f9d3d68a5326bea43..8fac435a969f3fd89624437d32bcb37fb507752d 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -1687,8 +1687,9 @@ rule ConditionalType : ONEOF(MemberExpression + "extends" + Type + '?' + Type + ObjectType + "extends" + Type + '?' + Type + ':' + Type, "unknown" + "extends" + Type + '?' + Type + ':' + Type, PrimaryType + "extends" + Type + '?' + Type + ':' + Type, - TypeQuery + "extends" + Type + '?' + Type + ':' + Type) - attr.action.%1,%2,%3,%4,%5,%6 : BuildConditionalType(%1, %3, %5, %7) + TypeQuery + "extends" + Type + '?' + Type + ':' + Type, + TypeArray + "extends" + Type + '?' + Type + ':' + Type) + attr.action.%1,%2,%3,%4,%5,%6,%7 : BuildConditionalType(%1, %3, %5, %7) rule KeyOf : ONEOF("keyof" + Identifier, "keyof" + '(' + TypeQuery + ')',