blob: 0a983437534155cb8c2b5cb513297d6fd373a1cf [file] [log] [blame] [raw]
RabsRincon6ef87b52018-02-27 14:58:21 +01001// Copyright (c) 2018, Patrick Quist
partouf404a9c12018-01-06 07:23:28 +01002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23// POSSIBILITY OF SUCH DAMAGE.
24"use strict";
25
Partoufe9202f12018-06-08 16:02:32 +020026const AsmRegex = require('./asmregex').AsmRegex;
27
28class AsmRaw extends AsmRegex {
partouf404a9c12018-01-06 07:23:28 +010029 constructor() {
Partoufe9202f12018-06-08 16:02:32 +020030 super();
partouf404a9c12018-01-06 07:23:28 +010031 }
32
partouf404a9c12018-01-06 07:23:28 +010033 processBinaryAsm(asm, filters) {
34 let result = [];
35 const asmLines = asm.split("\n");
36 const asmOpcodeRe = /^\s*([0-9a-f]+):\s*(([0-9a-f][0-9a-f] ?)+)\s*(.*)/;
partouf88b141d2018-01-07 20:44:34 +010037 const labelRe = /^([0-9a-f]+)\s+<([^>]+)>:$/;
partouf404a9c12018-01-06 07:23:28 +010038 const destRe = /.*\s([0-9a-f]+)\s+<([^>]+)>$/;
39 let source = null;
40
41 if (asmLines.length === 1 && asmLines[0][0] === '<') {
42 return [{text: asmLines[0], source: null}];
43 }
44
Matt Godbolteabe33c2018-10-22 23:00:28 -050045 asmLines.forEach(line => {
partouf88b141d2018-01-07 20:44:34 +010046 let match = line.match(labelRe);
partouf404a9c12018-01-06 07:23:28 +010047 if (match) {
partouf7ce75942018-01-07 20:58:22 +010048 result.push({text: match[2] + ":", source: null});
partouf404a9c12018-01-06 07:23:28 +010049 return;
partouf88b141d2018-01-07 20:44:34 +010050 } else {
Partoufe9202f12018-06-08 16:02:32 +020051 match = line.match(this.labelDef);
partouf88b141d2018-01-07 20:44:34 +010052 if (match) {
53 result.push({text: match[1] + ":", source: null});
54 return;
55 }
partouf404a9c12018-01-06 07:23:28 +010056 }
57
58 match = line.match(asmOpcodeRe);
59 if (match) {
60 const address = parseInt(match[1], 16);
61 const opcodes = match[2].split(" ").filter(function (x) {
62 return x;
63 });
Matt Godbolteabe33c2018-10-22 23:00:28 -050064 const disassembly = " " + AsmRegex.filterAsmLine(match[4], filters);
partouf404a9c12018-01-06 07:23:28 +010065 let links = null;
66 const destMatch = line.match(destRe);
67 if (destMatch) {
68 links = [{
69 offset: disassembly.indexOf(destMatch[1]),
70 length: destMatch[1].length,
71 to: parseInt(destMatch[1], 16)
72 }];
73 }
74 result.push({opcodes: opcodes, address: address, text: disassembly, source: source, links: links});
75 }
Matt Godbolteabe33c2018-10-22 23:00:28 -050076 });
partouf404a9c12018-01-06 07:23:28 +010077
78 return result;
79 }
80
81 process(asm, filters) {
82 return this.processBinaryAsm(asm, filters);
83 }
84}
85
86module.exports = {
87 AsmParser: AsmRaw
88};