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
|
// Copyright (c) 2018, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import _ from 'underscore';
import { ParseFilters } from '../../types/features/filters.interfaces';
import * as utils from '../utils';
// Note(jeremy-rifkin):
// For now this filters out a bunch of metadata we aren't interested in
// Maybe at a later date we'll want to allow user-controllable filters
// It'd be helpful to better display annotations about branch weights
// and parse debug info too at some point.
// TODO: !tbaa
export class LlvmPrintAfterAllParser {
filters: RegExp[];
lineFilters: RegExp[];
irDumpAfterHeader: RegExp;
defineLine: RegExp;
label: RegExp;
instruction: RegExp;
constructor(compilerProps) {
//this.maxIrLines = 5000;
//if (compilerProps) {
// this.maxIrLines = compilerProps('maxLinesOfAsm', this.maxIrLines);
//}
this.filters = [
/^; ModuleID = '.+'$/, // module id line
/^(source_filename|target datalayout|target triple) = ".+"$/, // module metadata
/^; Function Attrs: .+$/, // function attributes
/^\s+call void @llvm.dbg.value.+$/, // dbg calls
/^\s+call void @llvm.dbg.declare.+$/, // dbg calls
/^declare .+$/, // declare directives
/^(!\d+) = (?:distinct )?!DI([A-Za-z]+)\(([^)]+?)\)/, // meta
/^(!\d+) = !\{.*\}/, // meta
/^(![A-Za-z\._-]+) = !\{.*\}/, // meta
/^attributes #\d+ = { .+ }$/, // attributes directive
];
this.lineFilters = [
/,? ![A-Za-z0-9]+((?=( \{)?$))/, // debug annotation
/,? #\d+((?=( \{)?$))/, // attribute annotation
];
this.irDumpAfterHeader = /^\*\*\* (.+) \*\*\*$/;
this.defineLine = /^define .+ @([A-Za-z0-9_]+)\(.+$/;
this.label = /^\d+:(\s+;.+)?$/;
this.instruction = /^\s+.+$/;
}
process(ir: {text: string}[], _: ParseFilters) {
let last_item: string | null = null; // aweful impurity to used to filter adjacent blank lines
// Filter a lot of junk before processing
const preprocessed_lines = ir
.filter(line => this.filters.every(re => line.text.match(re) === null)) // apply filters
.filter(line => { // Filter duplicate blank lines
const b = line.text === "" && last_item === "";
last_item = line.text;
return !b;
})
.map(_line => {
let line = _line.text;
while(true) {
let newLine = line;
for(const re of this.lineFilters) {
newLine = newLine.replace(re, "");
}
if(newLine === line) {
break;
} else {
line = newLine;
}
}
_line.text = line;
return _line;
})
return preprocessed_lines;
}
/*getFileName(debugInfo, scope) {
const stdInLooking = /.*<stdin>|^-$|example\.[^/]+$|<source>/;
if (!debugInfo[scope]) {
// No such meta info.
return null;
}
// MetaInfo is a file node
if (debugInfo[scope].filename) {
const filename = debugInfo[scope].filename;
return !stdInLooking.test(filename) ? filename : null;
}
// MetaInfo has a file reference.
if (debugInfo[scope].file) {
return this.getFileName(debugInfo, debugInfo[scope].file);
}
if (!debugInfo[scope].scope) {
// No higher scope => can't find file.
return null;
}
// "Bubbling" up.
return this.getFileName(debugInfo, debugInfo[scope].scope);
}
getSourceLineNumber(debugInfo, scope) {
if (!debugInfo[scope]) {
return null;
}
if (debugInfo[scope].line) {
return Number(debugInfo[scope].line);
}
if (debugInfo[scope].scope) {
return this.getSourceLineNumber(debugInfo, debugInfo[scope].scope);
}
return null;
}
getSourceColumn(debugInfo, scope) {
if (!debugInfo[scope]) {
return null;
}
if (debugInfo[scope].column) {
return Number(debugInfo[scope].column);
}
if (debugInfo[scope].scope) {
return this.getSourceColumn(debugInfo, debugInfo[scope].scope);
}
return null;
}
parseMetaNode(line) {
// Metadata Nodes
// See: https://llvm.org/docs/LangRef.html#metadata
const match = line.match(this.metaNodeRe);
if (!match) {
return null;
}
let metaNode = {
metaId: match[1],
metaType: match[2],
};
let keyValuePair;
while ((keyValuePair = this.metaNodeOptionsRe.exec(match[3]))) {
const key = keyValuePair[1];
metaNode[key] = keyValuePair[2];
// Remove "" from string
if (metaNode[key][0] === '"') {
metaNode[key] = metaNode[key].substr(1, metaNode[key].length - 2);
}
}
return metaNode;
}
processIr(ir, filters) {
const result = [];
let irLines = utils.splitLines(ir);
let debugInfo = {};
let prevLineEmpty = false;
// Filters
const commentOnly = /^\s*(;.*)$/;
for (const line of irLines) {
let source = null;
let match;
if (line.trim().length === 0) {
// Avoid multiple successive empty lines.
if (!prevLineEmpty) {
result.push({text: '', source: null});
}
prevLineEmpty = true;
continue;
}
if (filters.commentOnly && commentOnly.test(line)) {
continue;
}
// Non-Meta IR line. Metadata is attached to it using "!dbg !123"
match = line.match(this.debugReference);
if (match) {
result.push({
text: filters.trim ? utils.squashHorizontalWhitespace(line) : line,
source: source,
scope: match[1],
});
prevLineEmpty = false;
continue;
}
const metaNode = this.parseMetaNode(line);
if (metaNode) {
debugInfo[metaNode.metaId] = metaNode;
}
if (filters.directives && this.isLineLlvmDirective(line)) {
continue;
}
result.push({text: filters.trim ? utils.squashHorizontalWhitespace(line) : line, source: source});
prevLineEmpty = false;
}
if (result.length >= this.maxIrLines) {
result.length = this.maxIrLines + 1;
result[this.maxIrLines] = {text: '[truncated; too many lines]', source: null};
}
for (const line of result) {
if (!line.scope) continue;
line.source = {
file: this.getFileName(debugInfo, line.scope),
line: this.getSourceLineNumber(debugInfo, line.scope),
column: this.getSourceColumn(debugInfo, line.scope),
};
}
return {
asm: result,
labelDefinitions: {},
};
}
process(ir, filters) {
if (_.isString(ir)) {
return this.processIr(ir, filters);
}
return {
asm: [],
labelDefinitions: {},
};
}
isLineLlvmDirective(line) {
return !!(
/^!\d+ = (distinct )?!(DI|{)/.test(line) ||
line.startsWith('!llvm') ||
line.startsWith('source_filename = ') ||
line.startsWith('target datalayout = ') ||
line.startsWith('target triple = ')
);
}
isLlvmIr(code) {
return code.includes('@llvm') && code.includes('!DI') && code.includes('!dbg');
}*/
}
|