Skip to content

Commit

Permalink
minimal ts conv of llvm-ir (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
partouf committed Dec 24, 2022
1 parent d4d595c commit 0723662
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
34 changes: 18 additions & 16 deletions lib/llvm-ir.js → lib/llvm-ir.ts
Expand Up @@ -24,9 +24,16 @@

import _ from 'underscore';

import {IRResultLine} from '../types/asmresult/asmresult.interfaces';

import * as utils from './utils';

export class LlvmIrParser {
private maxIrLines: number;
private debugReference: RegExp;
private metaNodeRe: RegExp;
private metaNodeOptionsRe: RegExp;

constructor(compilerProps) {
this.maxIrLines = 5000;
if (compilerProps) {
Expand All @@ -38,7 +45,7 @@ export class LlvmIrParser {
this.metaNodeOptionsRe = /(\w+): (!?\d+|\w+|""|"(?:[^"]|\\")*[^\\]")/gi;
}

getFileName(debugInfo, scope) {
getFileName(debugInfo, scope): string | null {
const stdInLooking = /.*<stdin>|^-$|example\.[^/]+$|<source>/;

if (!debugInfo[scope]) {
Expand Down Expand Up @@ -75,17 +82,16 @@ export class LlvmIrParser {
return null;
}

getSourceColumn(debugInfo, scope) {
getSourceColumn(debugInfo, scope): number | undefined {
if (!debugInfo[scope]) {
return null;
return;
}
if (debugInfo[scope].column) {
return Number(debugInfo[scope].column);
}
if (debugInfo[scope].scope) {
return this.getSourceColumn(debugInfo, debugInfo[scope].scope);
}
return null;
}

parseMetaNode(line) {
Expand All @@ -95,7 +101,7 @@ export class LlvmIrParser {
if (!match) {
return null;
}
let metaNode = {
const metaNode = {
metaId: match[1],
metaType: match[2],
};
Expand All @@ -114,22 +120,19 @@ export class LlvmIrParser {
}

processIr(ir, filters) {
const result = [];
let irLines = utils.splitLines(ir);
let debugInfo = {};
const result: IRResultLine[] = [];
const irLines = utils.splitLines(ir);
const 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});
result.push({text: ''});
}
prevLineEmpty = true;
continue;
Expand All @@ -140,11 +143,10 @@ export class LlvmIrParser {
}

// Non-Meta IR line. Metadata is attached to it using "!dbg !123"
match = line.match(this.debugReference);
const match = line.match(this.debugReference);
if (match) {
result.push({
text: filters.trim ? utils.squashHorizontalWhitespace(line) : line,
source: source,
scope: match[1],
});
prevLineEmpty = false;
Expand All @@ -159,13 +161,13 @@ export class LlvmIrParser {
if (filters.directives && this.isLineLlvmDirective(line)) {
continue;
}
result.push({text: filters.trim ? utils.squashHorizontalWhitespace(line) : line, source: source});
result.push({text: filters.trim ? utils.squashHorizontalWhitespace(line) : line});
prevLineEmpty = false;
}

if (result.length >= this.maxIrLines) {
result.length = this.maxIrLines + 1;
result[this.maxIrLines] = {text: '[truncated; too many lines]', source: null};
result[this.maxIrLines] = {text: '[truncated; too many lines]'};
}

for (const line of result) {
Expand Down
12 changes: 6 additions & 6 deletions test/llvm-ir-parser-tests.js
Expand Up @@ -172,14 +172,14 @@ describe('llvm-ir getSourceColumn', function () {
expect(llvmIrParser.getSourceColumn(debugInfo, '!12')).to.equal(10);
});

it('should return null on non-existend node', function () {
expect(llvmIrParser.getSourceColumn(debugInfo, '!16')).to.equal(null);
expect(llvmIrParser.getSourceColumn(debugInfo, '!30')).to.equal(null);
it('should return undefined on non-existend node', function () {
expect(llvmIrParser.getSourceColumn(debugInfo, '!16')).to.equal(undefined);
expect(llvmIrParser.getSourceColumn(debugInfo, '!30')).to.equal(undefined);
});

it('should return null if no higher scope has a column', function () {
expect(llvmIrParser.getSourceColumn(debugInfo, '!14')).to.equal(null);
expect(llvmIrParser.getSourceColumn(debugInfo, '!15')).to.equal(null);
it('should return undefined if no higher scope has a column', function () {
expect(llvmIrParser.getSourceColumn(debugInfo, '!14')).to.equal(undefined);
expect(llvmIrParser.getSourceColumn(debugInfo, '!15')).to.equal(undefined);
});
});

Expand Down
4 changes: 4 additions & 0 deletions types/asmresult/asmresult.interfaces.ts
Expand Up @@ -41,3 +41,7 @@ export type ParsedAsmResult = {
execTime?: string;
languageId?: string;
};

export type IRResultLine = ParsedAsmResultLine & {
scope?: string;
};

0 comments on commit 0723662

Please sign in to comment.