RabsRincon | 6ef87b5 | 2018-02-27 14:58:21 +0100 | [diff] [blame] | 1 | // Copyright (c) 2018, Patrick Quist |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 2 | // 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 | |
Partouf | e9202f1 | 2018-06-08 16:02:32 +0200 | [diff] [blame] | 26 | const AsmRegex = require('./asmregex').AsmRegex; |
| 27 | |
| 28 | class AsmRaw extends AsmRegex { |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 29 | constructor() { |
Partouf | e9202f1 | 2018-06-08 16:02:32 +0200 | [diff] [blame] | 30 | super(); |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 31 | } |
| 32 | |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 33 | 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*(.*)/; |
partouf | 88b141d | 2018-01-07 20:44:34 +0100 | [diff] [blame] | 37 | const labelRe = /^([0-9a-f]+)\s+<([^>]+)>:$/; |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 38 | 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 Godbolt | eabe33c | 2018-10-22 23:00:28 -0500 | [diff] [blame] | 45 | asmLines.forEach(line => { |
partouf | 88b141d | 2018-01-07 20:44:34 +0100 | [diff] [blame] | 46 | let match = line.match(labelRe); |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 47 | if (match) { |
partouf | 7ce7594 | 2018-01-07 20:58:22 +0100 | [diff] [blame] | 48 | result.push({text: match[2] + ":", source: null}); |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 49 | return; |
partouf | 88b141d | 2018-01-07 20:44:34 +0100 | [diff] [blame] | 50 | } else { |
Partouf | e9202f1 | 2018-06-08 16:02:32 +0200 | [diff] [blame] | 51 | match = line.match(this.labelDef); |
partouf | 88b141d | 2018-01-07 20:44:34 +0100 | [diff] [blame] | 52 | if (match) { |
| 53 | result.push({text: match[1] + ":", source: null}); |
| 54 | return; |
| 55 | } |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 56 | } |
| 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 Godbolt | eabe33c | 2018-10-22 23:00:28 -0500 | [diff] [blame] | 64 | const disassembly = " " + AsmRegex.filterAsmLine(match[4], filters); |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 65 | 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 Godbolt | eabe33c | 2018-10-22 23:00:28 -0500 | [diff] [blame] | 76 | }); |
partouf | 404a9c1 | 2018-01-06 07:23:28 +0100 | [diff] [blame] | 77 | |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | process(asm, filters) { |
| 82 | return this.processBinaryAsm(asm, filters); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | module.exports = { |
| 87 | AsmParser: AsmRaw |
| 88 | }; |