Repository navigation
Rebuild only what a change affects in the XDK build #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
523229a
ca56fcc
ef5b37f
4cab09c
ea03507
8ea0d53
739d0f3
d5350b7
2974cee
73d2336
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package org.xvm.asm; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * What a module that imports a compiled module compiles against: the module file without its methods' code, the | ||
| * source text of their bodies, the time it was built and the directory it was compiled from, together with whether | ||
| * each method is abstract, native or has code. | ||
| * | ||
| * <p>The compiler reads no method's code or source text from the modules it compiles against, so two module files | ||
| * with the same API compile every module that imports them to the same bytes. A build can then skip compiling the | ||
| * importers again after a change that leaves the API the same, such as one inside a method body. | ||
| */ | ||
| public final class ModuleApi { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why this class exists. Wouldn't it make more sense to just put the logic onto FileStructure, ModuleStructure, etc.? |
||
| private static final int ABSTRACT = 1; | ||
| private static final int NATIVE = 2; | ||
| private static final int CODE = 4; | ||
|
|
||
| private ModuleApi() { | ||
| } | ||
|
|
||
| /** | ||
| * @param module a module file, as the compiler writes it | ||
| * | ||
| * @return the API of the module | ||
| * | ||
| * @throws IOException if the module file cannot be read | ||
| */ | ||
| public static byte[] of(InputStream module) | ||
| throws IOException { | ||
| var file = new FileStructure(module, false, false); | ||
| try (var ignored = ConstantPool.withPool(file.getConstantPool())) { | ||
| byte[] methods = strip(file); | ||
| var out = new ByteArrayOutputStream(); | ||
| file.writeTo(out); | ||
| out.write(methods); | ||
| return out.toByteArray(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write the API of a module file to another file. | ||
| * | ||
| * @param module a module file, as the compiler writes it | ||
| * @param api the file to write the API to | ||
| * | ||
| * @throws IOException if the module file cannot be read or the API cannot be written | ||
| */ | ||
| public static void write(Path module, Path api) | ||
| throws IOException { | ||
| byte[] bytes; | ||
| try (InputStream in = Files.newInputStream(module)) { | ||
| bytes = of(in); | ||
| } | ||
| try (OutputStream out = Files.newOutputStream(api)) { | ||
| out.write(bytes); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove what the API leaves out from each module in the file. | ||
| * | ||
| * @return for each method, whether it is abstract, native or has code, in the order the file holds the methods, | ||
| * which is the same for two files whose other contents are the same | ||
| */ | ||
| static byte[] strip(FileStructure file) { | ||
| var flags = new ByteArrayOutputStream(); | ||
| for (Component component : tree(file).toList()) { | ||
| switch (component) { | ||
| case ModuleStructure module -> { | ||
| module.setTimestamp(null); | ||
| module.setSourceDir(null); | ||
| } | ||
| case MethodStructure method -> flags.write(strip(method)); | ||
| default -> { | ||
| } | ||
| } | ||
| } | ||
| return flags.toByteArray(); | ||
| } | ||
|
|
||
| /** | ||
| * Drop the code of a method and the source text of its body. | ||
| * | ||
| * @return whether the method is abstract, native or has code | ||
| */ | ||
| private static int strip(MethodStructure method) { | ||
| boolean fAbstract = method.isAbstract(); | ||
| int flags = (fAbstract ? ABSTRACT : 0) | ||
| | (method.isNative() ? NATIVE : 0) | ||
| | (method.hasCode() ? CODE : 0); | ||
| // marking a method abstract drops its code; it is then marked as it was | ||
| method.setAbstract(true); | ||
| method.setAbstract(fAbstract); | ||
| method.configureSource(null, 0); | ||
| return flags; | ||
| } | ||
|
|
||
| /** | ||
| * @return the component and every component that it holds, each with its conditional variants, in the order the | ||
| * file holds them | ||
| */ | ||
| private static Stream<Component> tree(Component component) { | ||
| return Stream.concat(Stream.of(component), component.children().stream() | ||
| .flatMap(child -> Stream.<Component>iterate(child, Objects::nonNull, Component::getNextSibling)) | ||
| .flatMap(ModuleApi::tree)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |
| import java.security.DigestOutputStream; | ||
| import java.security.MessageDigest; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
|
|
@@ -255,7 +257,8 @@ public void setSourceDir(LiteralConstant constDir) { | |
| } | ||
|
|
||
| /** | ||
| * Determine the date/time that the module was created. | ||
| * Determine the date/time that the module was created. The compiler records the epoch, so that the | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use epoch; use null. That means "does not have a date/time". |
||
| * same source compiles to the same bytes, unless it was asked for a time ({@code --timestamp}). | ||
| * | ||
| * @return the literal constant indicating the compilation timestamp, or null | ||
| */ | ||
|
|
@@ -944,9 +947,12 @@ public String getDescription() { | |
| } | ||
|
|
||
| sb.append(", source-dir=") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly good, except that the FileStructure already has both the time and the dir being optional (i.e. null is fine for both), so let's just change our default handling so that it's null unless they tell us to do something. |
||
| .append(m_constDir == null ? "none" : m_constDir.getValueString()) | ||
| .append(", timestamp=") | ||
| .append(m_constDir == null ? "none" : m_constTimestamp.getValueString()); | ||
| .append(m_constDir == null ? "none" : m_constDir.getValueString()); | ||
| if (m_constTimestamp != null && !Instant.EPOCH.toString().equals(m_constTimestamp.getValue())) { | ||
| // only a time that was asked for; the epoch says nothing | ||
| sb.append(", timestamp=") | ||
| .append(m_constTimestamp.getValueString()); | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
|
|
||
| import java.lang.reflect.Field; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
@@ -430,8 +432,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { | |
| return; | ||
| } | ||
|
|
||
| // create the FileStructure and "this" ModuleStructure | ||
| FileStructure struct = new FileStructure(sModule); | ||
| // create the FileStructure and "this" ModuleStructure; the epoch keeps the output reproducible | ||
| FileStructure struct = new FileStructure(sModule, Instant.EPOCH); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate having this constructor. Just get rid of the constructor that takes a time, and default the time to null, and we'll tell the FileStructure what time we want it to have if that's what we want. |
||
| component = struct.getModule(); | ||
| pool = struct.getConstantPool(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest:
Optionally stamp the compiled modules with a time. For
<time>, use the literalnowor an ISO-8601 instant like2027-06-13T00:00:00Z.