[LINUX] Run cbc of "Let's make a normal compiler" with Java 8 or later + 64bit

Run cbc in 64bit environment

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)

Supports Java 8 or later

Fix Parameter class collisions

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;

Generate binary for 32bit in 64bit environment

Modify the option to target the assembler to 32bit

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);
}

Modify the option to set the linker target to 32bit

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";

List of corrections

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

Run cbc of "Let's make a normal compiler" with Java 8 or later + 64bit
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make a supercomputer with xCAT
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's run a Bash script in Java
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
Let's make dependency management with pip a little easier
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[Super easy] Let's make a LINE BOT with Python.
Let's make a cron program in Java! !! (Task Scheduler)
Let's make a websocket client with Python. (Access token authentication)
Make a Linux version of OpenSiv3D with find_package a little easier
Let's make a diagram that can be clicked with IPython