-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathasfermi.cpp
442 lines (391 loc) · 14.7 KB
/
asfermi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using namespace std;
#include <stdarg.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <list>
#include <stack>
#include "SubString.h"
#include "DataTypes.h"
#include "Cubin.h"
#include "GlobalVariables.h"
#include "helper.h"
#include "SpecificParsers.h"
#include "stdafx.h" //Mark
#include "RulesModifier.h"
#include "RulesOperand.h"
#include "RulesInstruction.h"
#include "RulesDirective.h"
//-----Forward declarations
void ProcessCommandsAndReadSource(int argc, char** args);
void Initialize();
void WriteToCubinReplace();
void WriteToCubinDirectOutput();
//extern void ExternInitialize();
//-----End of forward declarations
int main(int argc, char** args)
{
try
{
//---Preprocess
ProcessCommandsAndReadSource(argc, args);
Initialize(); //Initialize instruction and directive rules
//ExternInitialize(); //initialize custom rules
//---Process
csMasterParser->Parse(0); //starting from line 0
//---Postprocess
if(csErrorPresent)
throw 98; //cannot proceed due to errors
if(csOperationMode == Replace)
WriteToCubinReplace();
else if(csOperationMode == DirectOutput)
WriteToCubinDirectOutput();
else
throw 97; //Mode not supported
puts("Done");
}
catch(int e)
{
hpExceptionHandler(e);
hpCleanUp();
return -1;
}
//---Normal exit
hpCleanUp();
#ifdef DebugMode
getchar();
#endif
return 0;
}
void WriteToCubinReplace()
{
if(csInstructionOffset + csOutputInstructionOffset> csOutputSectionSize )
throw 9; //output section not large enough
vector<Instruction>::iterator inst = csInstructions.begin();
csOutput.seekp(csOutput.beg + ::csOutputSectionOffset + ::csOutputInstructionOffset);
while(inst != csInstructions.end())
{
csOutput.write((char*)&inst->OpcodeWord0, 4);
if(inst->Is8)
csOutput.write((char*)&inst->OpcodeWord1, 4);
inst++;
}
csOutput.flush();
}
void WriteToCubinDirectOutput()
{
if(csInstructions.size()!=0)
throw 100; //last kernel not ended
if(csKernelList.size()==0)
throw 101; //no valid kernel found
//head sections: (null), .shstrtab, .strtab, .symtab
//kern sections: .text.kername, .nv.constant0.kername, (not implemented).nv.constant16.kername,
// .nv.info.kername, (optional).nv.shared.kername, (optional).nv.local.kername
//tail sections: .nv.constant2, .nv.info
/*
Stage1: Set section index and section name for all sections
1. head sections: SectionIndex, SHStrTabOffset
2. kern sections: SectionIndex, SHStrTabOffset, StrOffset of .text
3. tail sections: SectionIndex, SHStrTabOffset
4. Confirm sizes of .shstrtab and .shstr and total section count
*/
hpCubinStage1();
/*
Stage2: Set SectionSize for all kernels
1. head sections: SectionSize, SectionContent
.symtab: Set SymbolIndex for all sections; set GlobalSymbolIndex for all kernels
*/
hpCubinStage2();
/*
Stage3
1. kern sections: SectionSize, SectionContent
2. tail sections: SectionSize, SectionContent
*/
hpCubinStage3();
//Stage4: Setup all section headers
hpCubinStage4();
//Stage5: Setup all program segments
hpCubinStage5();
//Stage6: Setup ELF header
hpCubinStage6();
//Stage7: Write to cubin
hpCubinStage7();
}
void ProcessCommandsAndReadSource(int argc, char** args)
{
if(argc<4) //at least 4 strings: progpath inputpath -o outputname
{
csExceptionPrintUsage = true;
throw 20; // invalid arguments
}
int currentArg = 1; //used to refer the next argument to be examined
//Check input state. Single line instruction or input file?
if(strcmp(args[1], "-I") == 0) // Single line instruction.
{
//At least 7 args: progpath -I "instruction" -r target kernelname offset
if(argc<7)
{
csExceptionPrintUsage = true;
throw 20;
}
csSource = args[2]; //instruction string
csLines.push_back(Line(SubString(0, strlen(csSource)), 0)); //directly create a single line, issue: single-line mode doesn't support comment
currentArg = 3;
if(strcmp(args[3], "-r")!=0)
throw 10; //Single-line mode only supported in Replace Mode.
}
else //progpath inputpath ...
{
hpReadSource(args[1]); //Call helper function to read input file and create lines
currentArg = 2;
}
//Check options
while(currentArg<argc)
{
if(strcmp(args[currentArg], "-r")==0) //replace mode
{
if(argc - currentArg < 4)
{
csExceptionPrintUsage = true;
throw 20; // invalid arguments
}
csOperationMode = Replace;
hpCheckOutputForReplace(args[currentArg+1], args[currentArg+2], args[currentArg+3]); //Use helper function to check validity of input arguments
//csOutput is opened without closing
currentArg += 4;
}
else if(strcmp(args[currentArg], "-o")==0) //direct output mode
{
if(argc - currentArg < 2)
{
csExceptionPrintUsage = true;
throw 20; // invalid arguments
}
csOperationMode = DirectOutput;
csOutput.open(args[currentArg + 1], fstream::out | fstream::binary |fstream::trunc);
if(!csOutput.is_open() || !csOutput.good())
throw 4; //failed to open output file
currentArg += 2;
//note that csOutput is not closed
}
else if(strcmp(args[currentArg], "-sm_20")==0)
{
cubinArchitecture = sm_20;
currentArg += 1;
}
else if(strcmp(args[currentArg], "-sm_21")==0)
{
cubinArchitecture = sm_21;
currentArg += 1;
}
else if(strcmp(args[currentArg], "-sm_30")==0)
{
cubinArchitecture = sm_30;
currentArg += 1;
}
else if(strcmp(args[currentArg], "-32")==0)
{
hpCubinSet64(false);
currentArg += 1;
}
else if(strcmp(args[currentArg], "-64")==0)
{
hpCubinSet64(true);
currentArg += 1;
}
else if(strcmp(args[currentArg], "-SelfDebug")==0)
{
csSelfDebug = true;
currentArg += 1;
}
else
{
csExceptionPrintUsage = true;
throw 0; //invalid argument
}
}
}
void OrganiseRules()
{
int size = csInstructionRulePrepList.size();
csInstructionRuleCount = size;
csInstructionRules = new InstructionRule*[size];
csInstructionRuleIndices = new int[size];
int index = 0;
for(list<InstructionRule*>::iterator rule = csInstructionRulePrepList.begin(); rule != csInstructionRulePrepList.end(); rule++)
{
csInstructionRules[index] = *rule;
csInstructionRuleIndices[index] = (*rule)->ComputeIndex();
index++;
}
InstructionRule *instSaver;
int indexsaver;
//sort the indices
for(int i = size - 1; i > 0; i--)
{
for(int j =0; j< i; j++)
{
//larger one behind
if(csInstructionRuleIndices[j] > csInstructionRuleIndices[j+1])
{
instSaver = csInstructionRules[j];
indexsaver = csInstructionRuleIndices[j];
csInstructionRules[j] = csInstructionRules[j+1];
csInstructionRuleIndices[j] = csInstructionRuleIndices[j+1];
csInstructionRules[j+1] = instSaver;
csInstructionRuleIndices[j+1] = indexsaver;
}
else if(csInstructionRuleIndices[j] == csInstructionRuleIndices[j+1])
{
throw 50; //repeating indices
}
}
}
size = csDirectiveRulePrepList.size();
csDirectiveRuleCount = size;
csDirectiveRules = new DirectiveRule*[size];
csDirectiveRuleIndices = new int[size];
index = 0;
for(list<DirectiveRule*>::iterator rule = csDirectiveRulePrepList.begin(); rule != csDirectiveRulePrepList.end(); rule++)
{
csDirectiveRules[index] = *rule;
csDirectiveRuleIndices[index] = (*rule)->ComputeIndex();
index++;
}
DirectiveRule *direSaver;
//sort the indices
for(int i = size - 1; i > 0; i--)
{
for(int j =0; j< i; j++)
{
//larger one behind
if(csDirectiveRuleIndices[j] > csDirectiveRuleIndices[j+1])
{
direSaver = csDirectiveRules[j];
indexsaver = csDirectiveRuleIndices[j];
csDirectiveRules[j] = csDirectiveRules[j+1];
csDirectiveRuleIndices[j] = csDirectiveRuleIndices[j+1];
csDirectiveRules[j+1] = direSaver;
csDirectiveRuleIndices[j+1] = indexsaver;
}
else if(csDirectiveRuleIndices[j] == csDirectiveRuleIndices[j+1])
{
throw 50; //repeating indices
}
}
}
}
void Initialize() //set up the various lists
{
//Set default parsers
csMasterParserList.push_back ((MasterParser*) &MPDefault);
csLineParserList.push_back ((LineParser*) &LPDefault);
csInstructionParserList.push_back((InstructionParser*)&IPDefault);
csDirectiveParserList.push_back ((DirectiveParser*) &DPDefault);
csMasterParser = (MasterParser*)&MPDefault;
csLineParser = (LineParser*) &LPDefault;
csInstructionParser = (InstructionParser*)&IPDefault;
csDirectiveParser = (DirectiveParser*)&DPDefault;
//Load instruction rules
//data movement: 14
csInstructionRulePrepList.push_back((InstructionRule*)&IRMOV);
csInstructionRulePrepList.push_back((InstructionRule*)&IRMOV32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDU);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDC);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDS);
csInstructionRulePrepList.push_back((InstructionRule*)&IRST);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSTL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSTS);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDLK);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLDSLK);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSTUL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSTSUL);
//conversion: 4
csInstructionRulePrepList.push_back((InstructionRule*)&IRF2I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRF2F);
csInstructionRulePrepList.push_back((InstructionRule*)&IRI2F);
csInstructionRulePrepList.push_back((InstructionRule*)&IRI2I);
//execution control: 17
csInstructionRulePrepList.push_back((InstructionRule*)&IRSSY);
csInstructionRulePrepList.push_back((InstructionRule*)&IRBRA);
csInstructionRulePrepList.push_back((InstructionRule*)&IRJMP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRJCAL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRCAL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRPRET);
csInstructionRulePrepList.push_back((InstructionRule*)&IRRET);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSCHI);
csInstructionRulePrepList.push_back((InstructionRule*)&IREXIT);
csInstructionRulePrepList.push_back((InstructionRule*)&IRPBK);
csInstructionRulePrepList.push_back((InstructionRule*)&IRBRK);
csInstructionRulePrepList.push_back((InstructionRule*)&IRPCNT);
csInstructionRulePrepList.push_back((InstructionRule*)&IRCONT);
csInstructionRulePrepList.push_back((InstructionRule*)&IRPLONGJMP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLONGJMP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRNOP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRBAR);
csInstructionRulePrepList.push_back((InstructionRule*)&IRB2R);
csInstructionRulePrepList.push_back((InstructionRule*)&IRMEMBAR);
csInstructionRulePrepList.push_back((InstructionRule*)&IRATOM);
csInstructionRulePrepList.push_back((InstructionRule*)&IRRED);
csInstructionRulePrepList.push_back((InstructionRule*)&IRVOTE);
//floating point op: 12
csInstructionRulePrepList.push_back((InstructionRule*)&IRFADD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFADD32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFMUL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFMUL32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFFMA);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFSETP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRFCMP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRMUFU);
csInstructionRulePrepList.push_back((InstructionRule*)&IRDADD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRDMUL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRDFMA);
csInstructionRulePrepList.push_back((InstructionRule*)&IRDSETP);
//integer opp: 8
csInstructionRulePrepList.push_back((InstructionRule*)&IRIADD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRIADD32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRIMUL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRIMUL32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRIMAD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRISCADD);
csInstructionRulePrepList.push_back((InstructionRule*)&IRISETP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRICMP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRVADD);
//Logic and shift: 7
csInstructionRulePrepList.push_back((InstructionRule*)&IRLOP);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLOP32I);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSHR);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSHL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRBFE);
csInstructionRulePrepList.push_back((InstructionRule*)&IRBFI);
csInstructionRulePrepList.push_back((InstructionRule*)&IRSEL);
//miscellaneous: 4
csInstructionRulePrepList.push_back((InstructionRule*)&IRS2R);
csInstructionRulePrepList.push_back((InstructionRule*)&IRLEPC);
csInstructionRulePrepList.push_back((InstructionRule*)&IRCCTL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRCCTLL);
csInstructionRulePrepList.push_back((InstructionRule*)&IRPSETP);
//load directive rules
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRKernel);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DREndKernel);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRLabel);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRParam);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRShared);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRLocal);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRMinStack);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRMinFrame);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRConstant2);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRConstant);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DREndConstant);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRRegCount);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRBarCount);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRRawInstruction);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRArch);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRMachine);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRAlign);
csDirectiveRulePrepList.push_back((DirectiveRule*)&DRSelfDebug);
OrganiseRules();
}