aboutsummaryrefslogtreecommitdiff
path: root/lib/compilers/golang.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/compilers/golang.js')
-rw-r--r--lib/compilers/golang.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/compilers/golang.js b/lib/compilers/golang.js
index f558c5eb3..7e6af5362 100644
--- a/lib/compilers/golang.js
+++ b/lib/compilers/golang.js
@@ -50,6 +50,7 @@ class GolangCompiler extends BaseCompiler {
let file = null;
let fileCount = 0;
let func = null;
+ let funcCollisions = {};
let labels = {};
let usedLabels = {};
let lines = code.map(obj => {
@@ -82,13 +83,30 @@ class GolangCompiler extends BaseCompiler {
if (match) {
// Normalize function name.
func = match[1].replace(/[.()*]+/g, "_");
+
+ // It's possible for normalized function names to collide.
+ // Keep a count of collisions per function name. Labels get
+ // suffixed with _[collisions] when collisions > 0.
+ let collisions = funcCollisions[func];
+ if (collisions == null) {
+ collisions = 0;
+ } else {
+ collisions++;
+ }
+
+ funcCollisions[func] = collisions;
}
let res = [];
if (pcMatch && !labels[pcMatch]) {
// Create pseudo-label.
let label = pcMatch.replace(/^0{0,4}/, '');
- label = `${func}_pc${label}:`;
+ let suffix = '';
+ if (funcCollisions[func] > 0) {
+ suffix = `_${funcCollisions[func]}`;
+ }
+
+ label = `${func}_pc${label}${suffix}:`;
if (!labels[label]) {
res.push(label);
labels[label] = true;
@@ -107,7 +125,7 @@ class GolangCompiler extends BaseCompiler {
}
ins = ins.toLowerCase();
- args = this.replaceJump(func, ins, args, usedLabels);
+ args = this.replaceJump(func, funcCollisions[func], ins, args, usedLabels);
res.push(`\t${ins}${args}`);
return res;
});
@@ -123,7 +141,7 @@ class GolangCompiler extends BaseCompiler {
.join("\n");
}
- replaceJump(func, ins, args, usedLabels) {
+ replaceJump(func, collisions, ins, args, usedLabels) {
// Check if last argument is a decimal number.
const re = /(\s+)([0-9]+)(\s?)$/;
let match = args.match(re);
@@ -134,6 +152,9 @@ class GolangCompiler extends BaseCompiler {
// Check instruction has a jump prefix
if (_.any(jumpPrefixes, (prefix) => { return ins.startsWith(prefix); })) {
let label = `${func}_pc${match[2]}`;
+ if (collisions > 0) {
+ label += `_${collisions}`;
+ }
usedLabels[label + ":"] = true; // record label use for later filtering
return `${match[1]}${label}${match[3]}`;
}