Java Primer: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
Line 306:
Almost no programming language has standard constructs for all the things a processor can do, in particular not languages that were designed to be portable and memory-safe. For that reason we are required to add support for that outside of the language. Fortunately, Java does come with one construct specifically design to wrap the language to platform specifics: <tt>native</tt>
 
<sourcesyntaxhighlight lang="java">
// File: os/nl/combuster/minijavaos/Hal.java
package nl.combuster.minijavaos;
Line 314:
public static native void poke(int address, byte data);
}
</syntaxhighlight>
</source>
At some point the OS needs to access memory unsafely, and this is the method we'll use. The native keyword also implies that there is no implementation in java and that it'll come from elsewhere.
 
There are more things that might be difficult to do in straight Java: this example omits memory management in its entirety, but there is still a quirk: Every object subclasses from java.lang.Object. To make matters more complicated, the compiler does not allow us to write java.lang classes in Java. In this case it's a particular chicken-and-egg problem: how would we even compile a class that extends itself? We are in some way forced to treat java.lang.Object special, and basically use its constructor as a terminator for a constructor chain. At a later stage, we might even want to do something more in there, but for now we don't need to. The first piece of native support code deals with these problems:
 
<sourcesyntaxhighlight lang="asm">
; File: os/hal.asm
section .text
Line 335:
ret 2
 
</syntaxhighlight>
</source>
 
== A primitive kernel ==
Line 409:
Linking also happens with the same script as [[Bare Bones]]
 
<sourcesyntaxhighlight lang="c">
/* File: os/linker.ld */
/* The bootloader will look at this image and start execution at the symbol
Line 455:
a segment with the same name. Simply add stuff here as needed. */
}
</syntaxhighlight>
</source>
 
==== GRUB ====
Some configuration files for GRUB legacy. Doing this with a CD is the easiest way, really:
<sourcesyntaxhighlight lang="bash">
#
# File: os/grub.cfg
Line 470:
root (cd)
kernel (cd)/kernel
</syntaxhighlight>
</source>
 
==== Build instructions ====
Line 530:
 
If you paid attention, you'll notice that the JAR step requires a manifest at <tt>compiler/compiler.manifest</tt>. It only really marks it as runnable so that we can use it easily:
<sourcesyntaxhighlight lang="bash">
Main-Class: nl.combuster.minijava.Compiler
</syntaxhighlight>
</source>
 
Lastly, the [http://forge.ow2.org/project/download.php?group_id=23&file_id=20549 one library] our compiler uses to do most of its magic. You can just copy the source into the compiler folder and the build script will pick it up.