annotate gen/asmstmt.cpp @ 232:092468448d25 trunk

[svn r248] Fixed: labels in inline asm block now work for the normal case. Fixed: inline asm blocks are now emitted as a single asm entity.
author lindquist
date Sun, 08 Jun 2008 06:15:51 +0200
parents 61aa721a6b7f
children 76ee1bbe487e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
1 // Taken from GDC source tree, licence unclear?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
2 //
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
3 // Taken from an earlier version of DMD -- why is it missing from 0.79?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
4
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
5 #include "gen/llvm.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
6 #include "llvm/InlineAsm.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
7
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
8 //#include "d-gcc-includes.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
9 //#include "total.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
10 #include "dmd/statement.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
11 #include "dmd/scope.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
12 #include "dmd/declaration.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
13 #include "dmd/dsymbol.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
14
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
15 #include <cassert>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
16 #include <deque>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
17 #include <iostream>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
18 #include <sstream>
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
19 #include <cstring>
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
20
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
21 //#include "d-lang.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
22 //#include "d-codegen.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
23
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
24 #include "gen/irstate.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
25 #include "gen/dvalue.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
26 #include "gen/tollvm.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
27 #include "gen/logger.h"
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
28
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
29 typedef enum {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
30 Arg_Integer,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
31 Arg_Pointer,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
32 Arg_Memory,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
33 Arg_FrameRelative,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
34 Arg_LocalSize,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
35 Arg_Dollar
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
36 } AsmArgType;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
37
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
38 typedef enum {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
39 Mode_Input,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
40 Mode_Output,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
41 Mode_Update
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
42 } AsmArgMode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
43
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
44 struct AsmArg {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
45 AsmArgType type;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
46 Expression * expr;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
47 AsmArgMode mode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
48 AsmArg(AsmArgType type, Expression * expr, AsmArgMode mode) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
49 this->type = type;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
50 this->expr = expr;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
51 this->mode = mode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
52 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
53 };
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
54
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
55 struct AsmCode {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
56 char * insnTemplate;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
57 unsigned insnTemplateLen;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
58 Array args; // of AsmArg
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
59 unsigned moreRegs;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
60 unsigned dollarLabel;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
61 int clobbersMemory;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
62 AsmCode() {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
63 insnTemplate = NULL;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
64 insnTemplateLen = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
65 moreRegs = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
66 dollarLabel = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
67 clobbersMemory = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
68 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
69 };
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
70
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
71 AsmStatement::AsmStatement(Loc loc, Token *tokens) :
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
72 Statement(loc)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
73 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
74 this->tokens = tokens; // Do I need to copy these?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
75 asmcode = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
76 asmalign = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
77 refparam = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
78 naked = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
79 regs = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
80 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
81
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
82 Statement *AsmStatement::syntaxCopy()
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
83 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
84 // copy tokens? copy 'code'?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
85 AsmStatement * a_s = new AsmStatement(loc,tokens);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
86 a_s->asmcode = asmcode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
87 a_s->refparam = refparam;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
88 a_s->naked = naked;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
89 a_s->regs = a_s->regs;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
90 return a_s;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
91 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
92
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
93 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
94 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
95 bool sep = 0, nsep = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
96 buf->writestring("asm { ");
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
97
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
98 for (Token * t = tokens; t; t = t->next) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
99 switch (t->value) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
100 case TOKlparen:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
101 case TOKrparen:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
102 case TOKlbracket:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
103 case TOKrbracket:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
104 case TOKcolon:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
105 case TOKsemicolon:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
106 case TOKcomma:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
107 case TOKstring:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
108 case TOKcharv:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
109 case TOKwcharv:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
110 case TOKdcharv:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
111 nsep = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
112 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
113 default:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
114 nsep = 1;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
115 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
116 if (sep + nsep == 2)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
117 buf->writeByte(' ');
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
118 sep = nsep;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
119 buf->writestring(t->toChars());
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
120 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
121 buf->writestring("; }");
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
122 buf->writenl();
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
123 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
124
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
125 int AsmStatement::comeFrom()
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
126 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
127 return FALSE;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
128 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
129
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
130 /* GCC does not support jumps from asm statements. When optimization
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
131 is turned on, labels referenced only from asm statements will not
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
132 be output at the correct location. There are ways around this:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
133
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
134 1) Reference the label with a reachable goto statement
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
135 2) Have reachable computed goto in the function
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
136 3) Hack cfgbuild.c to act as though there is a computed goto.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
137
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
138 These are all pretty bad, but if would be nice to be able to tell
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
139 GCC not to optimize in this case (even on per label/block basis).
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
140
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
141 The current solution is output our own private labels (as asm
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
142 statements) along with the "real" label. If the label happens to
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
143 be referred to by a goto statement, the "real" label will also be
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
144 output in the correct location.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
145
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
146 Also had to add 'asmLabelNum' to LabelDsymbol to indicate it needs
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
147 special processing.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
148
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
149 (junk) d-lang.cc:916:case LABEL_DECL: // C doesn't do this. D needs this for referencing labels in inline assembler since there may be not goto referencing it.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
150
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
151 */
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
152
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
153 static unsigned d_priv_asm_label_serial = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
154
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
155 // may need to make this target-specific
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
156 static void d_format_priv_asm_label(char * buf, unsigned n)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
157 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
158 //ASM_GENERATE_INTERNAL_LABEL(buf, "LDASM", n);//inserts a '*' for use with assemble_name
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
159 sprintf(buf, ".LDASM%u", n);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
160 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
161
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
162 void
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
163 d_expand_priv_asm_label(IRState * irs, unsigned n)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
164 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
165 /* char buf[64];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
166 d_format_priv_asm_label(buf, n);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
167 strcat(buf, ":");
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
168 tree insnt = build_string(strlen(buf), buf);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
169 #if D_GCC_VER < 40
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
170 expand_asm(insnt, 1);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
171 #else
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
172 tree t = d_build_asm_stmt(insnt, NULL_TREE, NULL_TREE, NULL_TREE);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
173 ASM_VOLATILE_P( t ) = 1;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
174 ASM_INPUT_P( t) = 1; // what is this doing?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
175 irs->addExp(t);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
176 #endif*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
177 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
178
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
179
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
180 // StringExp::toIR usually adds a NULL. We don't want that...
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
181
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
182 /*static tree
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
183 naturalString(Expression * e)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
184 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
185 // don't fail, just an error?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
186 assert(e->op == TOKstring);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
187 StringExp * s = (StringExp *) e;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
188 assert(s->sz == 1);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
189 return build_string(s->len, (char *) s->string);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
190 }*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
191
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
192
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
193 #include "d-asm-i386.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
194
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
195 bool d_have_inline_asm() { return true; }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
196
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
197 Statement *AsmStatement::semantic(Scope *sc)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
198 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
199
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
200 sc->func->inlineAsm = 1;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
201 sc->func->inlineStatus = ILSno; // %% not sure
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
202 // %% need to set DECL_UNINLINABLE too?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
203 sc->func->hasReturnExp = 1; // %% DMD does this, apparently...
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
204
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
205 // empty statement -- still do the above things because they might be expected?
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
206 if (! tokens)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
207 return this;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
208
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
209 AsmProcessor ap(sc, this);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
210 ap.run();
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
211 return this;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
212 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
213
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
214 void
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
215 AsmStatement::toIR(IRState * irs)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
216 {
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
217 Logger::println("AsmStatement::toIR(): %s", loc.toChars());
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
218 LOG_SCOPE;
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
219
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
220 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
221 // gen.doLineNote( loc );
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
222
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
223 if (! asmcode)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
224 return;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
225
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
226 static std::string i_cns = "i";
231
61aa721a6b7f [svn r247] fixed accessing global symbols from inline asm.
lindquist
parents: 229
diff changeset
227 static std::string p_cns = "i";
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
228 static std::string l_cns = "X";
222
251548c1035d [svn r238] use *m for memory input constraints and pass in their address
ChristianK
parents: 221
diff changeset
229 static std::string m_cns = "*m";
221
68687d8c3e9a [svn r237] some inline asm output now seems to work, see tangotests/asm2.d
lindquist
parents: 220
diff changeset
230 static std::string mw_cns = "=*m";
223
5ffca623b5df [svn r239] also use indirect modifier for update constraints
ChristianK
parents: 222
diff changeset
231 static std::string mrw_cns = "+*m";
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
232 static std::string memory_name = "memory";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
233
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
234 AsmCode * code = (AsmCode *) asmcode;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
235 std::deque<LLValue*> input_values;
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
236 std::deque<std::string> input_constraints;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
237 std::deque<LLValue*> output_values;
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
238 std::deque<std::string> output_constraints;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
239 std::deque<std::string> clobbers;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
240
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
241 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
242 #define HOST_WIDE_INT long
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
243 HOST_WIDE_INT var_frame_offset; // "frame_offset" is a macro
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
244 bool clobbers_mem = code->clobbersMemory;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
245 int input_idx = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
246 int n_outputs = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
247 int arg_map[10];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
248
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
249 assert(code->args.dim <= 10);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
250
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
251 for (unsigned i = 0; i < code->args.dim; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
252 AsmArg * arg = (AsmArg *) code->args.data[i];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
253
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
254 bool is_input = true;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
255 LLValue* arg_val = 0;
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
256 std::string cns;
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
257
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
258 std::cout << std::endl;
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
259
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
260 switch (arg->type) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
261 case Arg_Integer:
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
262 arg_val = arg->expr->toElem(irs)->getRVal();
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
263 do_integer:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
264 cns = i_cns;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
265 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
266 case Arg_Pointer:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
267 // FIXME
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
268 std::cout << "asm fixme Arg_Pointer" << std::endl;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
269 if (arg->expr->op == TOKdsymbol)
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
270 {
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
271 assert(0);
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
272 DsymbolExp* dse = (DsymbolExp*)arg->expr;
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
273 LabelDsymbol* lbl = dse->s->isLabel();
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
274 assert(lbl);
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
275 arg_val = lbl->statement->llvmBB;
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
276 if (!arg_val)
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
277 {
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
278 arg_val = lbl->statement->llvmBB = llvm::BasicBlock::Create("label", irs->topfunc());
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
279 }
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
280 cns = l_cns;
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
281 }
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
282 else
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
283 {
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
284 arg_val = arg->expr->toElem(irs)->getRVal();
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
285 cns = p_cns;
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
286 }
226
4145266ff4bd [svn r242] fixed (I think) arg_pointer inline asm args
lindquist
parents: 225
diff changeset
287 /*if (arg->expr->op == TOKvar)
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
288 arg_val = arg->expr->toElem(irs);
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
289 else if (arg->expr->op == TOKdsymbol)
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
290 arg_val = arg->expr->toElem(irs);
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
291 else
226
4145266ff4bd [svn r242] fixed (I think) arg_pointer inline asm args
lindquist
parents: 225
diff changeset
292 assert(0);*/
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
293
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
294 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
295 case Arg_Memory:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
296 // FIXME
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
297 std::cout << "asm fixme Arg_Memory" << std::endl;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
298 arg_val = arg->expr->toElem(irs)->getRVal();
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
299 // if (arg->expr->op == TOKvar)
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
300 // arg_val = arg->expr->toElem(irs);
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
301 // else
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
302 // arg_val = arg->expr->toElem(irs);
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
303
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
304 switch (arg->mode) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
305 case Mode_Input: cns = m_cns; break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
306 case Mode_Output: cns = mw_cns; is_input = false; break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
307 case Mode_Update: cns = mrw_cns; is_input = false; break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
308 default: assert(0); break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
309 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
310 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
311 case Arg_FrameRelative:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
312 // FIXME
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
313 std::cout << "asm fixme Arg_FrameRelative" << std::endl;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
314 assert(0);
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
315 /* if (arg->expr->op == TOKvar)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
316 arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
317 else
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
318 assert(0);*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
319 if ( getFrameRelativeValue(arg_val, & var_frame_offset) ) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
320 // arg_val = irs->integerConstant(var_frame_offset);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
321 cns = i_cns;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
322 } else {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
323 this->error("%s", "argument not frame relative");
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
324 return;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
325 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
326 if (arg->mode != Mode_Input)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
327 clobbers_mem = true;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
328 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
329 case Arg_LocalSize:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
330 // FIXME
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
331 std::cout << "asm fixme Arg_LocalSize" << std::endl;
229
cac3d27ae481 [svn r245] initial support for labels in inline asm, broken :/
lindquist
parents: 227
diff changeset
332 assert(0);
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
333 /* var_frame_offset = cfun->x_frame_offset;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
334 if (var_frame_offset < 0)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
335 var_frame_offset = - var_frame_offset;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
336 arg_val = irs->integerConstant( var_frame_offset );*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
337 goto do_integer;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
338 default:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
339 assert(0);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
340 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
341
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
342 if (is_input) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
343 arg_map[i] = --input_idx;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
344 //inputs.cons(tree_cons(NULL_TREE, cns, NULL_TREE), arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
345 input_values.push_back(arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
346 input_constraints.push_back(cns);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
347 } else {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
348 arg_map[i] = n_outputs++;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
349 //outputs.cons(tree_cons(NULL_TREE, cns, NULL_TREE), arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
350 output_values.push_back(arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
351 output_constraints.push_back(cns);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
352 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
353 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
354
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
355 // Telling GCC that callee-saved registers are clobbered makes it preserve
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
356 // those registers. This changes the stack from what a naked function
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
357 // expects.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
358
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
359 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
360 // if (! irs->func->naked) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
361 for (int i = 0; i < 32; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
362 if (regs & (1 << i)) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
363 //clobbers.cons(NULL_TREE, regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
364 clobbers.push_back(regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
365 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
366 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
367 for (int i = 0; i < 32; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
368 if (code->moreRegs & (1 << (i-32))) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
369 //clobbers.cons(NULL_TREE, regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
370 clobbers.push_back(regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
371 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
372 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
373 if (clobbers_mem)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
374 clobbers.push_back(memory_name);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
375 //clobbers.cons(NULL_TREE, memory_name);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
376 // }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
377
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
378
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
379 // Remap argument numbers
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
380 for (unsigned i = 0; i < code->args.dim; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
381 if (arg_map[i] < 0)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
382 arg_map[i] = -arg_map[i] - 1 + n_outputs;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
383 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
384
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
385 bool pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
386 char * p = code->insnTemplate;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
387 char * q = p + code->insnTemplateLen;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
388 //printf("start: %.*s\n", code->insnTemplateLen, code->insnTemplate);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
389 while (p < q) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
390 if (pct) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
391 if (*p >= '0' && *p <= '9') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
392 // %% doesn't check against nargs
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
393 *p = '0' + arg_map[*p - '0'];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
394 pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
395 } else if (*p == '$') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
396 pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
397 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
398 //assert(*p == '%');// could be 'a', etc. so forget it..
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
399 } else if (*p == '$')
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
400 pct = true;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
401 ++p;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
402 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
403
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
404 printf("final: %.*s\n", code->insnTemplateLen, code->insnTemplate);
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
405
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
406 std::string insnt(code->insnTemplate, code->insnTemplateLen);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
407
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
408 // rewrite GCC-style constraints to LLVM-style constraints
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
409 std::string llvmOutConstraints;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
410 std::string llvmInConstraints;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
411 std::string llvmClobbers;
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
412 int n = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
413 typedef std::deque<std::string>::iterator it;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
414 for(it i = output_constraints.begin(), e = output_constraints.end(); i != e; ++i, ++n) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
415 // rewrite update constraint to in and out constraints
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
416 if((*i)[0] == '+') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
417 (*i)[0] = '=';
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
418 std::string input_constraint;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
419 std::stringstream ss;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
420 ss << n;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
421 ss >> input_constraint;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
422 //FIXME: I think multiple inout constraints will mess up the order!
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
423 input_constraints.push_front(input_constraint);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
424 input_values.push_front(output_values[n]);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
425 }
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
426 llvmOutConstraints += *i;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
427 llvmOutConstraints += ",";
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
428 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
429 for(it i = input_constraints.begin(), e = input_constraints.end(); i != e; ++i) {
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
430 llvmInConstraints += *i;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
431 llvmInConstraints += ",";
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
432 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
433
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
434 for(it i = clobbers.begin(), e = clobbers.end(); i != e; ++i) {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
435 llvmClobbers += "~{" + *i + "},";
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
436 }
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
437
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
438 // excessive commas are removed later...
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
439
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
440 // push asm statement
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
441 IRAsmStmt* asmStmt = new IRAsmStmt;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
442 asmStmt->code = insnt;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
443 asmStmt->out_c = llvmOutConstraints;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
444 asmStmt->in_c = llvmInConstraints;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
445 asmStmt->clobbers = llvmClobbers;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
446 asmStmt->out.insert(asmStmt->out.begin(), output_values.begin(), output_values.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
447 asmStmt->in.insert(asmStmt->in.begin(), input_values.begin(), input_values.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
448 irs->ASMs.push_back(asmStmt);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
449 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
450
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
451 //////////////////////////////////////////////////////////////////////////////
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
452
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
453 AsmBlockStatement::AsmBlockStatement(Loc loc, Statements* s)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
454 : CompoundStatement(loc, s)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
455 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
456 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
457
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
458 // rewrite argument indices to the block scope indices
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
459 static void remap_outargs(std::string& insnt, size_t nargs, size_t& idx)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
460 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
461 static const std::string digits[10] =
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
462 {
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
463 "0","1","2","3","4",
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
464 "5","6","7","8","9"
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
465 };
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
466 assert(nargs <= 10);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
467
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
468 static const std::string prefix("<<<out");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
469 static const std::string suffix(">>>");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
470 std::string argnum;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
471 std::string needle;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
472 char buf[10];
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
473 for (unsigned i = 0; i < nargs; i++) {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
474 needle = prefix + digits[i] + suffix;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
475 sprintf(buf, "%u", idx++);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
476 insnt.replace(insnt.find(needle), needle.size(), buf);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
477 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
478 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
479
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
480 // rewrite argument indices to the block scope indices
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
481 static void remap_inargs(std::string& insnt, size_t nargs, size_t& idx)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
482 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
483 static const std::string digits[10] =
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
484 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
485 "0","1","2","3","4",
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
486 "5","6","7","8","9"
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
487 };
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
488 assert(nargs <= 10);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
489
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
490 static const std::string prefix("<<<in");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
491 static const std::string suffix(">>>");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
492 std::string argnum;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
493 std::string needle;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
494 char buf[10];
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
495 for (unsigned i = 0; i < nargs; i++) {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
496 needle = prefix + digits[i] + suffix;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
497 sprintf(buf, "%u", idx++);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
498 insnt.replace(insnt.find(needle), needle.size(), buf);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
499 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
500 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
501
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
502 void AsmBlockStatement::toIR(IRState* p)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
503 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
504 Logger::println("AsmBlockStatement::toIR(): %s", loc.toChars());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
505 LOG_SCOPE;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
506 Logger::println("BEGIN ASM");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
507
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
508 assert(!p->inASM);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
509 p->inASM = true;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
510
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
511 // rest idx
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
512 size_t asmIdx = 0;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
513 assert(p->ASMs.empty());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
514
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
515 // do asm statements
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
516 for (int i=0; i<statements->dim; i++)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
517 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
518 Statement* s = (Statement*)statements->data[i];
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
519 if (s) {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
520 s->toIR(p);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
521 }
220
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
522 }
ccc2e6898a78 [svn r236] added initial codegen of inline asm, pretty buggy and incomplete still. see the tangotests/asm1.d test for a sample of what does
lindquist
parents: 219
diff changeset
523
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
524 // build asm block
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
525 std::vector<LLValue*> outargs;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
526 std::vector<LLValue*> inargs;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
527 std::vector<const LLType*> outtypes;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
528 std::vector<const LLType*> intypes;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
529 std::string out_c;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
530 std::string in_c;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
531 std::string clobbers;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
532 std::string code;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
533
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
534 size_t n = p->ASMs.size();
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
535 for (size_t i=0; i<n; ++i)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
536 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
537 IRAsmStmt* a = p->ASMs[i];
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
538 assert(a);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
539 size_t onn = a->out.size();
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
540 for (size_t j=0; j<onn; ++j)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
541 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
542 outargs.push_back(a->out[j]);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
543 outtypes.push_back(a->out[j]->getType());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
544 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
545 if (!a->out_c.empty())
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
546 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
547 out_c += a->out_c;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
548 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
549 if (!a->clobbers.empty())
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
550 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
551 clobbers += a->clobbers;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
552 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
553 remap_outargs(a->code, onn, asmIdx);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
554 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
555 for (size_t i=0; i<n; ++i)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
556 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
557 IRAsmStmt* a = p->ASMs[i];
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
558 assert(a);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
559 size_t inn = a->in.size();
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
560 for (size_t j=0; j<inn; ++j)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
561 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
562 inargs.push_back(a->in[j]);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
563 intypes.push_back(a->in[j]->getType());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
564 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
565 if (!a->in_c.empty())
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
566 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
567 in_c += a->in_c;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
568 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
569 remap_inargs(a->code, inn, asmIdx);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
570 if (!code.empty())
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
571 code += " ; ";
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
572 code += a->code;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
573 }
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
574 p->ASMs.clear();
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
575
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
576 out_c += in_c;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
577 out_c += clobbers;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
578 if (!out_c.empty())
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
579 out_c.resize(out_c.size()-1);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
580
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
581 Logger::println("code = \"%s\"", code.c_str());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
582 Logger::println("constraints = \"%s\"", out_c.c_str());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
583
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
584 std::vector<const LLType*> types;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
585 types.insert(types.end(), outtypes.begin(), outtypes.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
586 types.insert(types.end(), intypes.begin(), intypes.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
587 llvm::FunctionType* fty = llvm::FunctionType::get(llvm::Type::VoidTy, types, false);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
588 Logger::cout() << "function type = " << *fty << '\n';
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
589 llvm::InlineAsm* ia = llvm::InlineAsm::get(fty, code, out_c, true);
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
590
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
591 std::vector<LLValue*> args;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
592 args.insert(args.end(), outargs.begin(), outargs.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
593 args.insert(args.end(), inargs.begin(), inargs.end());
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
594 llvm::CallInst* call = p->ir->CreateCall(ia, args.begin(), args.end(), "");
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
595
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
596 p->inASM = false;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
597 Logger::println("END ASM");
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
598 }
232
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
599
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
600 // the whole idea of this statement is to avoid the flattening
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
601 Statements* AsmBlockStatement::flatten(Scope* sc)
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
602 {
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
603 return NULL;
092468448d25 [svn r248] Fixed: labels in inline asm block now work for the normal case.
lindquist
parents: 231
diff changeset
604 }