@@ -55,13 +55,22 @@ public class GraalPythonCC extends GraalPythonCompiler {
55
55
private String outputFilename ;
56
56
private boolean linkExecutable ;
57
57
private boolean link ;
58
+ private boolean linkLLI ;
58
59
private Boolean compile ;
59
60
private List <String > clangArgs ;
61
+ private List <String > execLinkArgs ;
60
62
private List <String > fileInputs ;
61
63
62
64
GraalPythonCC () {
63
65
}
64
66
67
+ private static List <String > execLinkPrefix = Arrays .asList (new String []{
68
+ "clang" ,
69
+ "-fembed-bitcode" ,
70
+ "-fPIC" ,
71
+ "-ggdb" ,
72
+ "-O1" ,
73
+ });
65
74
private static List <String > clangPrefix = Arrays .asList (new String []{
66
75
"clang" ,
67
76
"-emit-llvm" ,
@@ -100,10 +109,12 @@ private void run(String[] args) {
100
109
private void parseOptions (String [] args ) {
101
110
outputFilename = A_OUT ;
102
111
linkExecutable = true ;
112
+ linkLLI = false ;
103
113
link = true ;
104
114
verbose = false ;
105
115
compile = null ;
106
116
clangArgs = new ArrayList <>(clangPrefix );
117
+ execLinkArgs = new ArrayList <>(execLinkPrefix );
107
118
fileInputs = new ArrayList <>();
108
119
for (int i = 0 ; i < args .length ; i ++) {
109
120
String arg = args [i ];
@@ -123,6 +134,9 @@ private void parseOptions(String[] args) {
123
134
link = false ;
124
135
linkExecutable = false ;
125
136
break ;
137
+ case "--link-lli-scripts" :
138
+ linkLLI = true ;
139
+ continue ; // skip adding this to clang args
126
140
case "-v" :
127
141
if (!verbose ) {
128
142
verbose = true ;
@@ -144,6 +158,8 @@ private void parseOptions(String[] args) {
144
158
throw new RuntimeException ("cannot mix source and compiled sources" );
145
159
}
146
160
fileInputs .add (arg );
161
+ } else {
162
+ execLinkArgs .add (arg );
147
163
}
148
164
}
149
165
clangArgs .add (arg );
@@ -173,6 +189,13 @@ private void launchCC() {
173
189
String bcFile = bcFileFromFilename (f );
174
190
assert Files .exists (Paths .get (bcFile ));
175
191
optFile (bcFile );
192
+ try {
193
+ String objFile = objectFileFromFilename (f );
194
+ logV ("Optimized:" , bcFile , "->" , objFile );
195
+ Files .move (Paths .get (bcFile ), Paths .get (objFile ));
196
+ } catch (IOException e ) {
197
+ throw new RuntimeException (e );
198
+ }
176
199
}
177
200
} else {
178
201
optFile (outputFilename );
@@ -192,15 +215,21 @@ private void launchCC() {
192
215
}
193
216
}
194
217
195
- private static void linkExecutable (String executableScript , String linkedBcFile ) throws IOException {
196
- List <String > cmdline = GraalPythonMain .getCmdline (Arrays .asList (), Arrays .asList ());
197
- cmdline .add ("-LLI" );
198
- cmdline .add (linkedBcFile );
199
- Path executablePath = Paths .get (executableScript );
200
- Files .write (executablePath , String .join (" " , cmdline ).getBytes ());
201
- HashSet <PosixFilePermission > perms = new HashSet <>(Arrays .asList (new PosixFilePermission []{PosixFilePermission .OWNER_EXECUTE , PosixFilePermission .GROUP_EXECUTE }));
202
- perms .addAll (Files .getPosixFilePermissions (executablePath ));
203
- Files .setPosixFilePermissions (executablePath , perms );
218
+ private void linkExecutable (String executableScript , String linkedBcFile ) throws IOException {
219
+ if (linkLLI ) {
220
+ List <String > cmdline = GraalPythonMain .getCmdline (Arrays .asList (), Arrays .asList ());
221
+ cmdline .add ("-LLI" );
222
+ cmdline .add (linkedBcFile );
223
+ Path executablePath = Paths .get (executableScript );
224
+ Files .write (executablePath , String .join (" " , cmdline ).getBytes ());
225
+ HashSet <PosixFilePermission > perms = new HashSet <>(Arrays .asList (new PosixFilePermission []{PosixFilePermission .OWNER_EXECUTE , PosixFilePermission .GROUP_EXECUTE }));
226
+ perms .addAll (Files .getPosixFilePermissions (executablePath ));
227
+ } else {
228
+ execLinkArgs .add (linkedBcFile );
229
+ execLinkArgs .add ("-o" );
230
+ execLinkArgs .add (executableScript );
231
+ exec (execLinkArgs );
232
+ }
204
233
}
205
234
206
235
private void linkShared (List <String > bitcodeFiles ) {
@@ -221,4 +250,17 @@ private void optFile(String bcFile) {
221
250
opt .add (bcFile );
222
251
exec (opt );
223
252
}
253
+
254
+ private static String bcFileFromFilename (String f ) {
255
+ int dotIdx = f .lastIndexOf ('.' );
256
+ if (dotIdx > 1 ) {
257
+ return f .substring (0 , dotIdx + 1 ) + "bc" ;
258
+ } else {
259
+ return f + ".bc" ;
260
+ }
261
+ }
262
+
263
+ private static String objectFileFromFilename (String f ) {
264
+ return bcFileFromFilename (f ).replaceAll ("\\ .bc$" , ".o" );
265
+ }
224
266
}
0 commit comments