Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellilltokiwa committed Sep 12, 2024
1 parent e1428ec commit 0544cc2
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 203 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/apple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ jobs:
- name: build (no java modules)
run: make no-java

- name: build docs
run: make doc
# NYI: HACK: uncomment!
# - name: build docs
# run: make doc

- name: run tests
run: |
Expand Down
93 changes: 93 additions & 0 deletions src/dev/flang/ast/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,99 @@ public Resolved(SrcModule mod, CreateType ct, AbstractFeature universe)
res.resolveTypes(t.feature());
}
}
public static interface LookupFeature
{
AbstractFeature lookup(AbstractFeature target, FeatureName fn);
}
public Resolved(LookupFeature lf, AbstractFeature universe)
{
this.universe = universe;
t_i8 = lookup(lf, "i8", 1).selfType();
t_i16 = lookup(lf, "i16", 1).selfType();
t_i32 = lookup(lf, "i32", 1).selfType();
t_i64 = lookup(lf, "i64", 1).selfType();
t_u8 = lookup(lf, "u8", 1).selfType();
t_u16 = lookup(lf, "u16", 1).selfType();
t_u32 = lookup(lf, "u32", 1).selfType();
t_u64 = lookup(lf, "u64", 1).selfType();
t_f32 = lookup(lf, "f32", 1).selfType();
t_f64 = lookup(lf, "f64", 1).selfType();
t_bool = lookup(lf, "bool", 0).selfType();
t_fuzion = lookup(lf, "fuzion", 0).selfType();
t_String = lookup(lf, FuzionConstants.STRING_NAME, 0).selfType();
t_Const_String = lookup(lf, "Const_String", 0).selfType();
t_Any = lookup(lf, FuzionConstants.ANY_NAME, 0).selfType();
t_unit = lookup(lf, FuzionConstants.UNIT_NAME, 0).selfType();
t_void = lookup(lf, "void", 0).selfType();
t_codepoint = lookup(lf, "codepoint", 1).selfType();
f_id = lookup(lf, "id", 2);
f_void = lookup(lf, "void", 0);
f_choice = lookup(lf, "choice", 1);
f_TRUE = lookup(lf, "TRUE", 0);
f_FALSE = lookup(lf, "FALSE", 0);
f_true = lookup(lf, "true", 0);
f_false = lookup(lf, "false", 0);
f_bool = lookup(lf, "bool", 0);
f_bool_NOT = null;
f_bool_AND = null;
f_bool_OR = null;
f_bool_IMPLIES = null;
f_bool_TERNARY = null;
f_Const_String_utf8_data = lookup(lf, lookup(lf, "Const_String", 0), "utf8_data", 0);
f_debug = lookup(lf, "debug", 0);
f_debug_level = lookup(lf, "debug_level", 0);
f_Function = lookup(lf, FUNCTION_NAME, 2);
f_Function_call = lookup(lf, f_Function, "call", 1);
f_safety = lookup(lf, "safety", 0);
f_array = lookup(lf, "array", 5);
f_array_internal_array = lookup(lf, f_array, "internal_array", 0);
f_error = lookup(lf, "error", 1);
f_error_msg = lookup(lf, f_error, "msg", 0);
f_fuzion = lookup(lf, "fuzion", 0);
f_fuzion_java = lookup(lf, f_fuzion, "java", 0);
f_fuzion_Java_Object = lookup(lf, f_fuzion_java, "Java_Object", 1);
f_fuzion_Java_Object_Ref = lookup(lf, f_fuzion_Java_Object, "Java_Ref", 0);
f_fuzion_sys = lookup(lf, f_fuzion, "sys", 0);
f_fuzion_sys_array = lookup(lf, f_fuzion_sys, "internal_array", 3);
f_fuzion_sys_array_data = lookup(lf, f_fuzion_sys_array, "data", 0);
f_fuzion_sys_array_length = lookup(lf, f_fuzion_sys_array, "length", 0);
f_concur = lookup(lf, "concur", 0);
f_concur_atomic = lookup(lf, f_concur, "atomic", 2);
f_concur_atomic_v = lookup(lf, f_concur_atomic, "v", 0);
f_Type = lookup(lf, "Type", 0);
f_Type_infix_colon = lookup(lf, f_Type, "infix :", 1);
f_Type_infix_colon_true = lookup(lf, f_Type, "infix_colon_true", 1);
f_Type_infix_colon_false = lookup(lf, f_Type, "infix_colon_false", 1);
f_type_as_value = lookup(lf, "type_as_value", 1);
f_Lazy = lookup(lf, LAZY_NAME, 1);
f_Unary = lookup(lf, UNARY_NAME, 2);
f_auto_unwrap = lookup(lf, "auto_unwrap", 2);
resolved = this;
numericTypes = new TreeSet<AbstractType>(new List<>(
t_i8,
t_i16,
t_i32,
t_i64,
t_u8,
t_u16,
t_u32,
t_u64,
t_f32,
t_f64));
((ArtificialBuiltInType) t_ADDRESS ).resolveArtificialType(lookup(lf, FuzionConstants.ANY_NAME, 0));
((ArtificialBuiltInType) t_UNDEFINED).resolveArtificialType(universe);
((ArtificialBuiltInType) t_ERROR ).resolveArtificialType(f_ERROR);
}
private AbstractFeature lookup(LookupFeature lf, AbstractFeature target, String name, int argCount)
{
var result = lf.lookup(target, FeatureName.get(name, argCount));
check(result != null);
return result;
}
private AbstractFeature lookup(LookupFeature lf, String name, int argCount)
{
return lookup(lf, universe, name, argCount);
}
}


Expand Down
123 changes: 69 additions & 54 deletions src/dev/flang/fe/FrontEnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import dev.flang.util.ANY;
import dev.flang.util.Errors;
import dev.flang.util.FuzionConstants;
import dev.flang.util.List;
import dev.flang.util.SourceDir;


Expand All @@ -70,9 +69,6 @@ public class FrontEnd extends ANY
/*---------------------------- constants ----------------------------*/


static final FeatureName UNIVERSE_NAME = FeatureName.get(FuzionConstants.UNIVERSE_NAME, 0);


/**
* Offset added to global indices to detect false usage of these early on.
*/
Expand All @@ -94,6 +90,8 @@ public class FrontEnd extends ANY
*/
class Universe extends Feature
{
private final FeatureName _fn = FeatureName.get(FuzionConstants.UNIVERSE_NAME, 0);

{ setState(State.LOADING); }
public boolean isUniverse()
{
Expand All @@ -102,7 +100,7 @@ public boolean isUniverse()

public FeatureName featureName()
{
return UNIVERSE_NAME;
return _fn;
}
}

Expand All @@ -115,18 +113,6 @@ public FeatureName featureName()
public final FrontEndOptions _options;


/**
* The universe.
*/
public final AbstractFeature _universe;


/**
* The base module if it was loaded from base.fum, null otherwise.
*/
public final LibraryModule _baseModule;


/**
* The library modules loaded so far. Maps the module name, e.g. "base" to
* the corresponding LibraryModule instance.
Expand All @@ -137,7 +123,7 @@ public FeatureName featureName()
/**
* The module we are compiling. null if !options._loadSources or Errors.count() != 0
*/
private final SourceModule _sourceModule;
private SourceModule _sourceModule;


/**
Expand All @@ -146,6 +132,12 @@ public FeatureName featureName()
private int _totalModuleData = 0;


/**
* The compiled main module.
*/
private LibraryModule _mainModule;


/*-------------------------- constructors ---------------------------*/


Expand All @@ -155,40 +147,19 @@ public FeatureName featureName()
public FrontEnd(FrontEndOptions options)
{
_options = options;
Types.reset(options);
FeatureAndOuter.reset();
Errors.reset();
FeatureName.reset();
Expr.reset();
Call.reset();
Contract.reset();
HasGlobalIndex.reset();
reset();
var universe = new Universe();
_universe = universe;

var sourcePaths = options.sourcePaths();
var sourceDirs = new SourceDir[sourcePaths.length + options._modules.size()];
for (int i = 0; i < sourcePaths.length; i++)
{
sourceDirs[i] = new SourceDir(sourcePaths[i]);
}
var lms = new List<LibraryModule>();
if (options._loadBaseLib)
{
_baseModule = module(FuzionConstants.BASE_MODULE_NAME, modulePath(FuzionConstants.BASE_MODULE_NAME));
if (_baseModule != null)
{
lms.add(_baseModule);
}
}
else
{
_baseModule = null;
}
lms.addAll(options._modules.stream().map(mn -> loadModule(mn))
.filter(m -> m != null)
.toList());
var dependsOn = lms.toArray(LibraryModule[]::new);

loadModules(universe);

var dependsOn = _modules.values().toArray(LibraryModule[]::new);
if (options._loadSources)
{
_sourceModule = new SourceModule(options, sourceDirs, dependsOn, universe);
Expand All @@ -201,6 +172,33 @@ public FrontEnd(FrontEndOptions options)
}


private void loadModules(Universe universe)
{
if (_options._loadBaseLib)
{
module(FuzionConstants.BASE_MODULE_NAME, modulePath(FuzionConstants.BASE_MODULE_NAME), universe);
}
_options._modules.stream().forEach(mn -> loadModule(mn, universe));
}


private void reset()
{
_totalModuleData = 0;
Types.reset(_options);
FeatureAndOuter.reset();
Errors.reset();
FeatureName.reset();
Expr.reset();
Call.reset();
Contract.reset();
HasGlobalIndex.reset();
_sourceModule = null;
_modules.clear();
_mainModule = null;
}


/**
* Determine the path of the base modules, "$(FUZION)/modules".
*/
Expand Down Expand Up @@ -236,13 +234,13 @@ private Path modulePath(String name)
/**
* Load module from given path.
*/
private LibraryModule module(String m, Path p)
private LibraryModule module(String m, Path p, AbstractFeature universe)
{
LibraryModule result = null;
try (var ch = (FileChannel) Files.newByteChannel(p, EnumSet.of(StandardOpenOption.READ)))
{
var data = ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size());
result = libModule(data, new LibraryModule[0]);
result = libModule(data, universe);
if (!m.equals(result.name()))
{
Errors.error("Module name mismatch for module file '" + p + "' expected name '" +
Expand All @@ -262,16 +260,15 @@ private LibraryModule module(String m, Path p)
/**
* create a new LibraryModule from `data`
*/
private LibraryModule libModule(ByteBuffer data, LibraryModule[] dependsOn)
private LibraryModule libModule(ByteBuffer data, AbstractFeature universe)
{
LibraryModule result;
var base = _totalModuleData;
_totalModuleData = base + data.limit();
result = new LibraryModule(GLOBAL_INDEX_OFFSET + base,
this,
data,
dependsOn,
_universe);
universe);
return result;
}

Expand All @@ -283,15 +280,15 @@ private LibraryModule libModule(ByteBuffer data, LibraryModule[] dependsOn)
*
* @return the loaded module or null if it was not found or an error occurred.
*/
LibraryModule loadModule(String m)
LibraryModule loadModule(String m, AbstractFeature universe)
{
var result = _modules.get(m);
if (result == null)
{
var p = modulePath(m);
if (p != null)
{
result = module(m, p);
result = module(m, p, universe);
}
else
{
Expand All @@ -309,7 +306,9 @@ LibraryModule loadModule(String m)

public MIR createMIR()
{
return _sourceModule.createMIR(_sourceModule._main);
var main = _sourceModule._main;
var mm = mainModule();
return mm.createMIR(main);
}


Expand All @@ -331,9 +330,25 @@ public Collection<LibraryModule> getModules()
/**
* Get the compiled module main.
*/
public Module mainModule()
public LibraryModule mainModule()
{
if (_mainModule == null)
{
_sourceModule.checkMain();
Errors.showAndExit();

var data = _sourceModule.data("main");
reset();
_mainModule = libModule(data, null /* use universe of module */);
var ignore = new Types.Resolved((target,fn) -> _mainModule.lookupFeature(target, fn, null), _mainModule.libraryUniverse());
}
return _mainModule;
}


public LibraryModule baseModule()
{
return libModule(_sourceModule.data("main"), _modules.values().toArray(new LibraryModule[_modules.size()]));
return _modules.get("base");
}

}
Expand Down
21 changes: 16 additions & 5 deletions src/dev/flang/fe/LibraryFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,29 @@ public int modifiers()
}


@Override
public boolean isUniverse()
{
return featureName().baseName().equals("universe");
}


/**
* Find the outer feature of this feature.
*/
public AbstractFeature outer()
{
var result = _outer;
if (result == null)
AbstractFeature result = null;
if (!isUniverse())
{
result = _libModule.featureOuter(_index);
_outer = result;
}
result = _outer;
if (result == null)
{
result = _libModule.featureOuter(_index);
_outer = result;
}

}
return result;
}

Expand Down
Loading

0 comments on commit 0544cc2

Please sign in to comment.