When I tried to get started with the compiler with "Let's make a normal compiler", I couldn't even move Hello World in a 64-bit environment, so it was a memo of the corrections. In addition, the patch is released below, so please take advantage of it. kamaboko123/cbc-64bit-patch
There are two main corrections.
--Support for Java 8 or later --Assemble and link as a 32-bit binary in a 64-bit environment
The environment is Ubuntu 16.04 + OpenJDK9. (We have not confirmed the operation in other environments)
Modify net / loveruby / cflat / parser / Parser.jj
.
cbc's net.loveruby.cflat.entity.Parameter
is
It conflicts with java.lang.reflect.Parameter
, which seems to have been introduced in the standard library since Java8.
Simply modify the part that says Parameter
so that it has a complete name.
If it's troublesome, this is one line.
sed "s/Parameter/net.loveruby.cflat.entity.Parameter/g" net/loveruby/cflat/parser/Parser.jj > net/loveruby/cflat/parser/Parser.jj.tmp; mv net/loveruby/cflat/parser/Parser.jj.tmp net/loveruby/cflat/parser/Parser.jj;
Add an option between the 19th and 20th lines of net / loveruby / cflat / sysdep / GNUAssembler.java
.
public void assemble(String srcPath, String destPath,
AssemblerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add("as");
cmd.add("--32"); //Added option to target 32bit
cmd.addAll(opts.args);
cmd.add("-o");
cmd.add(destPath);
cmd.add(srcPath);
CommandUtils.invoke(cmd, errorHandler, opts.verbose);
}
Not only the assembler, but also the linker must explicitly specify to target 32bit.
The corrections are on lines 29-30 and 59-60 of net / loveruby / cflat / sysdep / GNULinker.java
.
Near lines 29-30
public void generateExecutable(List<String> args,
String destPath, LinkerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add(LINKER);
cmd.add("-melf_i386"); //add to
cmd.add("-dynamic-linker");
cmd.add(DYNAMIC_LINKER);
if (opts.generatingPIE) {
cmd.add("-pie");
}
if (! opts.noStartFiles) {
cmd.add(opts.generatingPIE
? C_RUNTIME_START_PIE
: C_RUNTIME_START);
cmd.add(C_RUNTIME_INIT);
}
cmd.addAll(args);
if (! opts.noDefaultLibs) {
cmd.add("-lc");
cmd.add("-lcbc");
}
if (! opts.noStartFiles) {
cmd.add(C_RUNTIME_FINI);
}
cmd.add("-o");
cmd.add(destPath);
CommandUtils.invoke(cmd, errorHandler, opts.verbose);
}
Near lines 59-60
public void generateSharedLibrary(List<String> args,
String destPath, LinkerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add(LINKER);
cmd.add("-melf_i386"); //add to
cmd.add("-shared");
if (! opts.noStartFiles) {
cmd.add(C_RUNTIME_INIT);
}
cmd.addAll(args);
if (! opts.noDefaultLibs) {
cmd.add("-lc");
cmd.add("-lcbc");
}
if (! opts.noStartFiles) {
cmd.add(C_RUNTIME_FINI);
}
cmd.add("-o");
cmd.add(destPath);
CommandUtils.invoke(cmd, errorHandler, opts.verbose);
}
You may also need to modify the runtime (startup routine?) Path in some environments.
In Ubuntu16.04, the C runtime does not exist in / usr / lib
, it exists in / usr / lib32
.
There is a way to solve it by creating a symbolic link, but I think it is better to mess with the source code side because it only cures the following 4 lines.
Lines 12-16 of net / loveruby / cflat / sysdep / GNULinker.java
.
//Before correction
//static final private String C_RUNTIME_INIT = "/usr/lib/crti.o";
//static final private String C_RUNTIME_START = "/usr/lib/crt1.o";
//static final private String C_RUNTIME_START_PIE = "/usr/lib/Scrt1.o";
//static final private String C_RUNTIME_FINI = "/usr/lib/crtn.o";
//Revised
static final private String C_RUNTIME_INIT = "/usr/lib32/crti.o";
static final private String C_RUNTIME_START = "/usr/lib32/crt1.o";
static final private String C_RUNTIME_START_PIE = "/usr/lib32/Scrt1.o";
static final private String C_RUNTIME_FINI = "/usr/lib32/crtn.o";
It's diff.
diff --git a/net/loveruby/cflat/parser/Parser.jj b/net/loveruby/cflat/parser/Parser.jj
index f6811b3..a73904a 100644
--- a/net/loveruby/cflat/parser/Parser.jj
+++ b/net/loveruby/cflat/parser/Parser.jj
@@ -567,7 +567,7 @@ Params params():
LOOKAHEAD(<VOID> ")")
t=<VOID>
{
- return new Params(location(t), new ArrayList<Parameter>());
+ return new Params(location(t), new ArrayList<net.loveruby.cflat.entity.Parameter>());
}
| params=fixedparams()
["," "..." { params.acceptVarargs(); }]
@@ -580,8 +580,8 @@ Params params():
// #@@range/fixedparams{
Params fixedparams():
{
- List<Parameter> params = new ArrayList<Parameter>();
- Parameter param, param1;
+ List<net.loveruby.cflat.entity.Parameter> params = new ArrayList<net.loveruby.cflat.entity.Parameter>();
+ net.loveruby.cflat.entity.Parameter param, param1;
}
{
param1=param() { params.add(param1); }
@@ -593,13 +593,13 @@ Params fixedparams():
// #@@}
// #@@range/param{
-Parameter param():
+net.loveruby.cflat.entity.Parameter param():
{
TypeNode t;
String n;
}
{
- t=type() n=name() { return new Parameter(t, n); }
+ t=type() n=name() { return new net.loveruby.cflat.entity.Parameter(t, n); }
}
// #@@}
diff --git a/net/loveruby/cflat/sysdep/GNUAssembler.java b/net/loveruby/cflat/sysdep/GNUAssembler.java
index 42a5a33..b95275b 100644
--- a/net/loveruby/cflat/sysdep/GNUAssembler.java
+++ b/net/loveruby/cflat/sysdep/GNUAssembler.java
@@ -17,6 +17,7 @@ class GNUAssembler implements Assembler {
AssemblerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add("as");
+ cmd.add("--32");
cmd.addAll(opts.args);
cmd.add("-o");
cmd.add(destPath);
diff --git a/net/loveruby/cflat/sysdep/GNULinker.java b/net/loveruby/cflat/sysdep/GNULinker.java
index 0487d6b..a5620aa 100644
--- a/net/loveruby/cflat/sysdep/GNULinker.java
+++ b/net/loveruby/cflat/sysdep/GNULinker.java
@@ -10,10 +10,10 @@ class GNULinker implements Linker {
// #@@range/vars{
static final private String LINKER = "/usr/bin/ld";
static final private String DYNAMIC_LINKER = "/lib/ld-linux.so.2";
- static final private String C_RUNTIME_INIT = "/usr/lib/crti.o";
- static final private String C_RUNTIME_START = "/usr/lib/crt1.o";
- static final private String C_RUNTIME_START_PIE = "/usr/lib/Scrt1.o";
- static final private String C_RUNTIME_FINI = "/usr/lib/crtn.o";
+ static final private String C_RUNTIME_INIT = "/usr/lib32/crti.o";
+ static final private String C_RUNTIME_START = "/usr/lib32/crt1.o";
+ static final private String C_RUNTIME_START_PIE = "/usr/lib32/Scrt1.o";
+ static final private String C_RUNTIME_FINI = "/usr/lib32/crtn.o";
// #@@}
ErrorHandler errorHandler;
@@ -27,6 +27,7 @@ class GNULinker implements Linker {
String destPath, LinkerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add(LINKER);
+ cmd.add("-melf_i386");
cmd.add("-dynamic-linker");
cmd.add(DYNAMIC_LINKER);
if (opts.generatingPIE) {
@@ -57,6 +58,7 @@ class GNULinker implements Linker {
String destPath, LinkerOptions opts) throws IPCException {
List<String> cmd = new ArrayList<String>();
cmd.add(LINKER);
+ cmd.add("-melf_i386");
cmd.add("-shared");
if (! opts.noStartFiles) {
cmd.add(C_RUNTIME_INIT);
Recommended Posts