annotate gen/asmstmt.cpp @ 219:761c8352f494 trunk

[svn r235] rough port of GDC's inline assembler code, unfinished
author ChristianK
date Thu, 05 Jun 2008 19:22:37 +0200
parents
children ccc2e6898a78
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
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
5 //#include "d-gcc-includes.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
6 //#include "total.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
7 #include "dmd/statement.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
8 #include "dmd/scope.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
9 #include "dmd/declaration.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
10 #include "dmd/dsymbol.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
11
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
12 #include "llvm/InlineAsm.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
13
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
14 #include <cassert>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
15 #include <deque>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
16 #include <iostream>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
17 #include <sstream>
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
18
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
19 //#include "d-lang.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
20 //#include "d-codegen.h"
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
21
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
22 typedef enum {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
23 Arg_Integer,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
24 Arg_Pointer,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
25 Arg_Memory,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
26 Arg_FrameRelative,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
27 Arg_LocalSize,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
28 Arg_Dollar
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
29 } AsmArgType;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
30
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
31 typedef enum {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
32 Mode_Input,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
33 Mode_Output,
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
34 Mode_Update
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
35 } AsmArgMode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
36
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
37 struct AsmArg {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
38 AsmArgType type;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
39 Expression * expr;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
40 AsmArgMode mode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
41 AsmArg(AsmArgType type, Expression * expr, AsmArgMode mode) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
42 this->type = type;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
43 this->expr = expr;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
44 this->mode = mode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
45 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
46 };
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
47
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
48 struct AsmCode {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
49 char * insnTemplate;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
50 unsigned insnTemplateLen;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
51 Array args; // of AsmArg
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
52 unsigned moreRegs;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
53 unsigned dollarLabel;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
54 int clobbersMemory;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
55 AsmCode() {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
56 insnTemplate = NULL;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
57 insnTemplateLen = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
58 moreRegs = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
59 dollarLabel = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
60 clobbersMemory = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
61 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
62 };
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
63
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
64 llvm::InlineAsm*
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
65 d_build_asm_stmt(std::string code, std::deque<DValue*> const& output_values, std::deque<DValue*> const& input_values, std::string constraints)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
66 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
67 //FIXME: Return InlineAsm::get(..) here.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
68 return NULL;
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 {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
217 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
218 // gen.doLineNote( loc );
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
219
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
220 if (! asmcode)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
221 return;
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 static std::string i_cns = "i";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
224 static std::string p_cns = "p";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
225 static std::string m_cns = "m";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
226 static std::string mw_cns = "=m";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
227 static std::string mrw_cns = "+m";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
228 static std::string memory_name = "memory";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
229
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
230 AsmCode * code = (AsmCode *) asmcode;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
231 std::deque<DValue*> input_values;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
232 std::deque<std::string> input_constraints;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
233 std::deque<DValue*> output_values;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
234 std::deque<std::string> output_constraints;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
235 std::deque<std::string> clobbers;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
236
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
237 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
238 #define HOST_WIDE_INT long
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
239 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
240 bool clobbers_mem = code->clobbersMemory;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
241 int input_idx = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
242 int n_outputs = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
243 int arg_map[10];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
244
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
245 assert(code->args.dim <= 10);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
246
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
247 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
248 AsmArg * arg = (AsmArg *) code->args.data[i];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
249
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
250 bool is_input = true;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
251 DValue* arg_val;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
252 std::string cns;
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 switch (arg->type) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
255 case Arg_Integer:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
256 arg_val = arg->expr->toElem(irs);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
257 do_integer:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
258 cns = i_cns;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
259 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
260 case Arg_Pointer:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
261 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
262 /* if (arg->expr->op == TOKvar)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
263 arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
264 else if (arg->expr->op == TOKdsymbol) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
265 arg_val = irs->getLabelTree( (LabelDsymbol *) ((DsymbolExp *) arg->expr)->s );
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
266 } else
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
267 assert(0);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
268 arg_val = irs->addressOf(arg_val);*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
269 cns = p_cns;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
270 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
271 case Arg_Memory:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
272 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
273 /* if (arg->expr->op == TOKvar)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
274 arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
275 else
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
276 arg_val = arg->expr->toElem(irs);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
277 if (DECL_P( arg_val ))
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
278 TREE_ADDRESSABLE( arg_val ) = 1;*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
279 switch (arg->mode) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
280 case Mode_Input: cns = m_cns; break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
281 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
282 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
283 default: assert(0); break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
284 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
285 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
286 case Arg_FrameRelative:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
287 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
288 /* if (arg->expr->op == TOKvar)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
289 arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
290 else
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
291 assert(0);*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
292 if ( getFrameRelativeValue(arg_val, & var_frame_offset) ) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
293 // arg_val = irs->integerConstant(var_frame_offset);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
294 cns = i_cns;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
295 } else {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
296 this->error("%s", "argument not frame relative");
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
297 return;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
298 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
299 if (arg->mode != Mode_Input)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
300 clobbers_mem = true;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
301 break;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
302 case Arg_LocalSize:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
303 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
304 /* var_frame_offset = cfun->x_frame_offset;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
305 if (var_frame_offset < 0)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
306 var_frame_offset = - var_frame_offset;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
307 arg_val = irs->integerConstant( var_frame_offset );*/
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
308 goto do_integer;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
309 default:
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
310 assert(0);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
311 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
312
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
313 if (is_input) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
314 arg_map[i] = --input_idx;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
315 //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
316 input_values.push_back(arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
317 input_constraints.push_back(cns);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
318 } else {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
319 arg_map[i] = n_outputs++;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
320 //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
321 output_values.push_back(arg_val);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
322 output_constraints.push_back(cns);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
323 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
324 }
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 // 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
327 // 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
328 // expects.
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
329
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
330 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
331 // if (! irs->func->naked) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
332 for (int i = 0; i < 32; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
333 if (regs & (1 << i)) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
334 //clobbers.cons(NULL_TREE, regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
335 clobbers.push_back(regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
336 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
337 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
338 for (int i = 0; i < 32; i++) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
339 if (code->moreRegs & (1 << (i-32))) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
340 //clobbers.cons(NULL_TREE, regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
341 clobbers.push_back(regInfo[i].gccName);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
342 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
343 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
344 if (clobbers_mem)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
345 clobbers.push_back(memory_name);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
346 //clobbers.cons(NULL_TREE, memory_name);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
347 // }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
348
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
349
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
350 // Remap argument numbers
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
351 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
352 if (arg_map[i] < 0)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
353 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
354 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
355
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
356 bool pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
357 char * p = code->insnTemplate;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
358 char * q = p + code->insnTemplateLen;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
359 //printf("start: %.*s\n", code->insnTemplateLen, code->insnTemplate);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
360 while (p < q) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
361 if (pct) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
362 if (*p >= '0' && *p <= '9') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
363 // %% doesn't check against nargs
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
364 *p = '0' + arg_map[*p - '0'];
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
365 pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
366 } else if (*p == '$') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
367 pct = false;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
368 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
369 //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
370 } else if (*p == '$')
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
371 pct = true;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
372 ++p;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
373 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
374
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
375 //printf("final: %.*s\n", code->insnTemplateLen, code->insnTemplate);
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 std::string insnt(code->insnTemplate, code->insnTemplateLen);
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 // rewrite GCC-style constraints to LLVM-style constraints
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
380 std::string llvmConstraints;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
381 int n = 0;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
382 typedef std::deque<std::string>::iterator it;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
383 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
384 // rewrite update constraint to in and out constraints
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
385 if((*i)[0] == '+') {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
386 (*i)[0] = '=';
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
387 std::string input_constraint;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
388 std::stringstream ss;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
389 ss << n;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
390 ss >> input_constraint;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
391 //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
392 input_constraints.push_front(input_constraint);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
393 input_values.push_front(output_values[n]);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
394 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
395 llvmConstraints += *i;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
396 llvmConstraints += ",";
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 for(it i = input_constraints.begin(), e = input_constraints.end(); i != e; ++i) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
399 llvmConstraints += *i;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
400 llvmConstraints += ",";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
401 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
402 for(it i = clobbers.begin(), e = clobbers.end(); i != e; ++i) {
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
403 llvmConstraints += "~{" + *i + "},";
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
404 }
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
405 llvmConstraints.resize(llvmConstraints.size()-1);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
406
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
407 std::cout << "Inline Asm code: " << std::endl;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
408 std::cout << insnt << std::endl;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
409 std::cout << "LLVM constraints: " << llvmConstraints << std::endl;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
410 std::cout << std::endl;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
411
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
412 llvm::InlineAsm* t = d_build_asm_stmt(insnt, output_values, input_values, llvmConstraints);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
413 // FIXME
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
414 //ASM_VOLATILE_P( t ) = 1;
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
415 //irs->addExp( t );
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
416 if (code->dollarLabel)
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
417 d_expand_priv_asm_label(irs, code->dollarLabel);
761c8352f494 [svn r235] rough port of GDC's inline assembler code, unfinished
ChristianK
parents:
diff changeset
418 }