| 1 |
/** |
| 2 |
* Inline assembler for the D programming language compiler. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/iasm.html, Inline Assembler) |
| 5 |
* |
| 6 |
* Copyright (C) 2018-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/iasm.d, _iasm.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_iasm.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/iasm.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.iasm; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
|
| 18 |
import dmd.dscope; |
| 19 |
import dmd.dsymbol; |
| 20 |
import dmd.expression; |
| 21 |
import dmd.func; |
| 22 |
import dmd.mtype; |
| 23 |
import dmd.tokens; |
| 24 |
import dmd.statement; |
| 25 |
import dmd.statementsem; |
| 26 |
|
| 27 |
version (NoBackend) |
| 28 |
{ |
| 29 |
} |
| 30 |
else version (IN_GCC) |
| 31 |
{ |
| 32 |
import dmd.iasmgcc; |
| 33 |
} |
| 34 |
else |
| 35 |
{ |
| 36 |
import dmd.iasmdmd; |
| 37 |
version = MARS; |
| 38 |
} |
| 39 |
|
| 40 |
/************************ AsmStatement ***************************************/ |
| 41 |
|
| 42 |
Statement asmSemantic(AsmStatement s, Scope* sc) |
| 43 |
{ |
| 44 |
//printf("AsmStatement.semantic()\n"); |
| 45 |
|
| 46 |
FuncDeclaration fd = sc.parent.isFuncDeclaration(); |
| 47 |
assert(fd); |
| 48 |
|
| 49 |
if (!s.tokens) |
| 50 |
return null; |
| 51 |
|
| 52 |
// Assume assembler code takes care of setting the return value |
| 53 |
sc.func.hasInlineAsm = true; |
| 54 |
|
| 55 |
version (NoBackend) |
| 56 |
{ |
| 57 |
return null; |
| 58 |
} |
| 59 |
else version (MARS) |
| 60 |
{ |
| 61 |
/* If it starts with a string literal, it's gcc inline asm |
| 62 |
*/ |
| 63 |
if (s.tokens.value == TOK.string_) |
| 64 |
{ |
| 65 |
/* Replace the asm statement with an assert(0, msg) that trips at runtime. |
| 66 |
*/ |
| 67 |
const loc = s.loc; |
| 68 |
auto e = new IntegerExp(loc, 0, Type.tint32); |
| 69 |
auto msg = new StringExp(loc, "Gnu Asm not supported - compile this function with gcc or clang"); |
| 70 |
auto ae = new AssertExp(loc, e, msg); |
| 71 |
auto se = new ExpStatement(loc, ae); |
| 72 |
return statementSemantic(se, sc); |
| 73 |
} |
| 74 |
auto ias = new InlineAsmStatement(s.loc, s.tokens); |
| 75 |
ias.caseSensitive = s.caseSensitive; |
| 76 |
return inlineAsmSemantic(ias, sc); |
| 77 |
} |
| 78 |
else version (IN_GCC) |
| 79 |
{ |
| 80 |
auto eas = new GccAsmStatement(s.loc, s.tokens); |
| 81 |
return gccAsmSemantic(eas, sc); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
s.error("D inline assembler statements are not supported"); |
| 86 |
return new ErrorStatement(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/************************ CAsmDeclaration ************************************/ |
| 91 |
|
| 92 |
void asmSemantic(CAsmDeclaration ad, Scope* sc) |
| 93 |
{ |
| 94 |
version (NoBackend) |
| 95 |
{ |
| 96 |
} |
| 97 |
else version (IN_GCC) |
| 98 |
{ |
| 99 |
return gccAsmSemantic(ad, sc); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
import dmd.errors : error; |
| 104 |
error(ad.code.loc, "Gnu Asm not supported - compile this file with gcc or clang"); |
| 105 |
} |
| 106 |
} |