From b3c08210e1e4916695d3637fdb05693ee8f50c5e Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sun, 5 Aug 2018 18:46:58 +0200 Subject: [PATCH] Move main implementation of AsmStatement to InlineAsmStatement --- dub.sdl | 2 +- src/dmd/astbase.d | 15 ++++++++++- src/dmd/iasm.d | 52 ++++++++++++++++++++++++++++++++++++++ src/dmd/iasmdmd.d | 18 +++---------- src/dmd/parsetimevisitor.d | 3 +++ src/dmd/s2ir.d | 2 +- src/dmd/statement.d | 34 ++++++++++++++++++++----- src/dmd/statement.h | 9 ++++++- src/dmd/strictvisitor.d | 1 + src/dmd/visitor.h | 4 +++ src/posix.mak | 2 +- src/win32.mak | 2 +- 12 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 src/dmd/iasm.d diff --git a/dub.sdl b/dub.sdl index 4a259ee712..82794f2c7c 100644 --- a/dub.sdl +++ b/dub.sdl @@ -62,5 +62,5 @@ subPackage { versions "MARS" sourcePaths "src/dmd" excludedSourceFiles "src/dmd/backend/*" - excludedSourceFiles "src/dmd/{e2ir,eh,glue,iasmdmd,objc_glue,s2ir,tocsym,toctype,toobj,todt,toir}.d" + excludedSourceFiles "src/dmd/{e2ir,eh,glue,iasm,iasmdmd,objc_glue,s2ir,tocsym,toctype,toobj,todt,toir}.d" } diff --git a/src/dmd/astbase.d b/src/dmd/astbase.d index b695aed4c3..fd894a3fbd 100644 --- a/src/dmd/astbase.d +++ b/src/dmd/astbase.d @@ -2424,7 +2424,7 @@ struct ASTBase } } - extern (C++) final class AsmStatement : Statement + extern (C++) class AsmStatement : Statement { Token* tokens; @@ -2440,6 +2440,19 @@ struct ASTBase } } + extern (C++) final class InlineAsmStatement : AsmStatement + { + extern (D) this(const ref Loc loc, Token* tokens) + { + super(loc, tokens); + } + + override void accept(Visitor v) + { + v.visit(this); + } + } + extern (C++) class ExpStatement : Statement { Expression exp; diff --git a/src/dmd/iasm.d b/src/dmd/iasm.d new file mode 100644 index 0000000000..f1023d9b57 --- /dev/null +++ b/src/dmd/iasm.d @@ -0,0 +1,52 @@ +/** + * Compiler implementation of the + * $(LINK2 http://www.dlang.org, D programming language). + * + * Copyright (C) 2018 by The D Language Foundation, All Rights Reserved + * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright) + * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) + * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/iasm.d, _iasm.d) + * Documentation: https://dlang.org/phobos/dmd_iasm.html + * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/iasm.d + */ + +/* Inline assembler for the D programming language compiler + */ + +module dmd.iasm; + +import dmd.dscope; +import dmd.func; +import dmd.statement; + +version (MARS) +{ + import dmd.iasmdmd; +} + +/************************ AsmStatement ***************************************/ + +extern(C++) Statement asmSemantic(AsmStatement s, Scope *sc) +{ + //printf("AsmStatement.semantic()\n"); + + FuncDeclaration fd = sc.parent.isFuncDeclaration(); + assert(fd); + + if (!s.tokens) + return null; + + // Assume assembler code takes care of setting the return value + sc.func.hasReturnExp |= 8; + + version (MARS) + { + auto ias = new InlineAsmStatement(s.loc, s.tokens); + return inlineAsmSemantic(ias, sc); + } + else + { + error("D inline assembler statements are not supported"); + return new ErrorStatement(); + } +} diff --git a/src/dmd/iasmdmd.d b/src/dmd/iasmdmd.d index 1e240423f1..efc6f90b92 100644 --- a/src/dmd/iasmdmd.d +++ b/src/dmd/iasmdmd.d @@ -28,7 +28,6 @@ import dmd.dsymbol; import dmd.errors; import dmd.expression; import dmd.expressionsem; -import dmd.func; import dmd.globals; import dmd.id; import dmd.identifier; @@ -113,7 +112,7 @@ struct ASM_STATE LabelDsymbol psDollar; Dsymbol psLocalsize; bool bReturnax; - AsmStatement statement; + InlineAsmStatement statement; Scope* sc; Token* tok; TOK tokValue; @@ -4357,23 +4356,17 @@ extern (C++) public regm_t iasm_regs(block *bp) } -/************************ AsmStatement ***************************************/ +/************************ InlineAsmStatement *********************************/ -extern (C++) public Statement asmSemantic(AsmStatement s, Scope *sc) +public Statement inlineAsmSemantic(InlineAsmStatement s, Scope *sc) { - //printf("AsmStatement.semantic()\n"); + //printf("InlineAsmStatement.semantic()\n"); OP *o; OPND opnd1, opnd2, opnd3, opnd4; OPND* o1, o2, o3, o4; PTRNTAB ptb; int usNumops; - FuncDeclaration fd = sc.parent.isFuncDeclaration(); - - assert(fd); - - if (!s.tokens) - return null; asmstate.ucItype = 0; asmstate.bReturnax = false; @@ -4394,9 +4387,6 @@ version (none) // don't use bReturnax anymore, and will fail anyway if we use re asmstate.bReturnax = false; } - // Assume assembler code takes care of setting the return value - sc.func.hasReturnExp |= 8; - if (!asmstate.bInit) { asmstate.bInit = true; diff --git a/src/dmd/parsetimevisitor.d b/src/dmd/parsetimevisitor.d index 6b1d367345..1695aca3bb 100644 --- a/src/dmd/parsetimevisitor.d +++ b/src/dmd/parsetimevisitor.d @@ -126,6 +126,9 @@ public: void visit(AST.CompoundDeclarationStatement s) { visit(cast(AST.CompoundStatement)s); } void visit(AST.CompoundAsmStatement s) { visit(cast(AST.CompoundStatement)s); } + // AsmStatements + void visit(AST.InlineAsmStatement s) { visit(cast(AST.AsmStatement)s); } + //========================================================================================= // Types void visit(AST.TypeBasic t) { visit(cast(AST.Type)t); } diff --git a/src/dmd/s2ir.d b/src/dmd/s2ir.d index b173de1623..f7ecb6d21c 100644 --- a/src/dmd/s2ir.d +++ b/src/dmd/s2ir.d @@ -1512,7 +1512,7 @@ private extern (C++) class S2irVisitor : Visitor /**************************************** */ - override void visit(AsmStatement s) + override void visit(InlineAsmStatement s) // { .visit(irs, s); } { block *bpre; diff --git a/src/dmd/statement.d b/src/dmd/statement.d index 4dd5c2bb4c..7275d68af6 100644 --- a/src/dmd/statement.d +++ b/src/dmd/statement.d @@ -2379,14 +2379,9 @@ extern (C++) final class LabelDsymbol : Dsymbol /*********************************************************** */ -extern (C++) final class AsmStatement : Statement +extern (C++) class AsmStatement : Statement { Token* tokens; - code* asmcode; - uint asmalign; // alignment of this statement - uint regs; // mask of registers modified (must match regm_t in back end) - bool refparam; // true if function parameter is referenced - bool naked; // true if function is to be naked extern (D) this(const ref Loc loc, Token* tokens) { @@ -2405,6 +2400,33 @@ extern (C++) final class AsmStatement : Statement } } +/*********************************************************** + * https://dlang.org/spec/iasm.html + */ +extern (C++) final class InlineAsmStatement : AsmStatement +{ + code* asmcode; + uint asmalign; // alignment of this statement + uint regs; // mask of registers modified (must match regm_t in back end) + bool refparam; // true if function parameter is referenced + bool naked; // true if function is to be naked + + extern (D) this(const ref Loc loc, Token* tokens) + { + super(loc, tokens); + } + + override Statement syntaxCopy() + { + return new InlineAsmStatement(loc, tokens); + } + + override void accept(Visitor v) + { + v.visit(this); + } +} + /*********************************************************** * a complete asm {} block */ diff --git a/src/dmd/statement.h b/src/dmd/statement.h index a01ce6c0f7..6a9fdcaf0d 100644 --- a/src/dmd/statement.h +++ b/src/dmd/statement.h @@ -677,6 +677,14 @@ class AsmStatement : public Statement { public: Token *tokens; + + Statement *syntaxCopy(); + void accept(Visitor *v) { v->visit(this); } +}; + +class InlineAsmStatement : public AsmStatement +{ +public: code *asmcode; unsigned asmalign; // alignment of this statement unsigned regs; // mask of registers modified (must match regm_t in back end) @@ -684,7 +692,6 @@ public: bool naked; // true if function is to be naked Statement *syntaxCopy(); - void accept(Visitor *v) { v->visit(this); } }; diff --git a/src/dmd/strictvisitor.d b/src/dmd/strictvisitor.d index 442bd40a84..7fd448fde1 100644 --- a/src/dmd/strictvisitor.d +++ b/src/dmd/strictvisitor.d @@ -100,6 +100,7 @@ extern(C++) class StrictVisitor(AST) : ParseTimeVisitor!AST override void visit(AST.CompoundStatement) { assert(0); } override void visit(AST.CompoundDeclarationStatement) { assert(0); } override void visit(AST.CompoundAsmStatement) { assert(0); } + override void visit(AST.InlineAsmStatement) { assert(0); } override void visit(AST.Type) { assert(0); } override void visit(AST.TypeBasic) { assert(0); } override void visit(AST.TypeError) { assert(0); } diff --git a/src/dmd/visitor.h b/src/dmd/visitor.h index e9f755b15b..bad84f3bf9 100644 --- a/src/dmd/visitor.h +++ b/src/dmd/visitor.h @@ -53,6 +53,7 @@ class DebugStatement; class GotoStatement; class LabelStatement; class AsmStatement; +class InlineAsmStatement; class CompoundAsmStatement; class ImportStatement; @@ -411,6 +412,9 @@ public: virtual void visit(CompoundDeclarationStatement *s) { visit((CompoundStatement *)s); } virtual void visit(CompoundAsmStatement *s) { visit((CompoundStatement *)s); } + // AsmStatements + virtual void visit(InlineAsmStatement *s) { visit((AsmStatement *)s); } + // Types virtual void visit(TypeBasic *t) { visit((Type *)t); } virtual void visit(TypeError *t) { visit((Type *)t); } diff --git a/src/posix.mak b/src/posix.mak index d00ff6362f..f857a2c164 100644 --- a/src/posix.mak +++ b/src/posix.mak @@ -323,7 +323,7 @@ GLUE_OBJS = G_GLUE_OBJS = $(addprefix $G/, $(GLUE_OBJS)) GLUE_SRCS=$(addsuffix .d, $(addprefix $D/,irstate toctype glue gluelayer todt tocsym toir dmsc \ - tocvdebug s2ir toobj e2ir eh iasmdmd objc_glue)) + tocvdebug s2ir toobj e2ir eh iasm iasmdmd objc_glue)) DMD_SRCS=$(FRONT_SRCS) $(GLUE_SRCS) $(BACK_HDRS) $(TK_HDRS) diff --git a/src/win32.mak b/src/win32.mak index 8ee4c37248..4f76633948 100644 --- a/src/win32.mak +++ b/src/win32.mak @@ -175,7 +175,7 @@ LEXER_ROOT=$(ROOT)/array.d $(ROOT)/ctfloat.d $(ROOT)/file.d $(ROOT)/filename.d \ PARSER_SRCS=$D/astbase.d $D/parsetimevisitor.d $D/parse.d $D/transitivevisitor.d $D/permissivevisitor.d $D/strictvisitor.d GLUE_SRCS=$D/irstate.d $D/toctype.d $D/glue.d $D/gluelayer.d $D/todt.d $D/tocsym.d $D/toir.d $D/dmsc.d \ - $D/tocvdebug.d $D/s2ir.d $D/toobj.d $D/e2ir.d $D/objc_glue.d $D/eh.d $D/iasmdmd.d + $D/tocvdebug.d $D/s2ir.d $D/toobj.d $D/e2ir.d $D/objc_glue.d $D/eh.d $D/iasm.d $D/iasmdmd.d BACK_HDRS=$C/cc.d $C/cdef.d $C/cgcv.d $C/code.d $C/cv4.d $C/dt.d $C/el.d $C/global.d \ $C/obj.d $C/oper.d $C/outbuf.d $C/rtlsym.d $C/code_x86.d $C/iasm.d \