Initial commit of main compiler sources (or should I say ... SAUCES!)

This commit is contained in:
2025-06-08 23:58:21 +10:00
parent 60c566025c
commit 06f2613083
214 changed files with 22210 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package slangc.bytecode;
import slangc.api.BytecodeOutput;
public abstract class BytecodeObject {
public static enum Type {
NULL,
BOOLEAN,
INT32,
INT64,
FLOAT,
DOUBLE,
STRING,
TYPESIG,
TYPE,
METHODSIG,
METHOD,
FIELDSIG,
FIELD
}
public BytecodeObject() {
// TODO Auto-generated constructor stub
}
public abstract int sizeOfArrayPart();
public abstract void writeArrayPart(BytecodeOutput output);
}

View File

@@ -0,0 +1,36 @@
package slangc.bytecode;
import slang.data.Mappable;
public final class FieldSignature implements Mappable {
public final TypeSignature owner;
public final boolean isStatic;
public final TypeSignature storageType;
public final String name;
public FieldSignature(TypeSignature owner, boolean isStatic, TypeSignature storageType, String name) {
super();
this.owner = owner;
this.isStatic = isStatic;
this.storageType = storageType;
this.name = name;
}
public String toString() {
return owner.toString() + (isStatic ? "##" : "#") + name + ":" + storageType.toString();
}
@Override
public int mappableHash() {
return Mappable.hashOf(toString());
}
@Override
public boolean mappableEquals(Mappable obj) {
if (obj instanceof FieldSignature) {
return this.toString().equals(((FieldSignature)obj).toString());
} else {
return false; //super.equals(obj);
}
}
}

View File

@@ -0,0 +1,12 @@
package slangc.bytecode;
public class Instruction {
public Instruction() {
// TODO Auto-generated constructor stub
}
public short encodeFirstOp(MajorOpcode major, MinorOpcode minor, StackType type, int extraops) {
return 0; //TODO
}
}

View File

@@ -0,0 +1,10 @@
package slangc.bytecode;
public enum MajorOpcode {
INVALID_ZERO,
LOADSTORE,
CALL,
NEW_ARRAY,
CONVERT,
ALU
}

View File

@@ -0,0 +1,60 @@
package slangc.bytecode;
import slang.data.Mappable;
public final class MethodSignature implements Mappable {
public static enum Kind {
STATIC_INIT,
CONSTRUCTOR,
STATIC_METHOD,
INSTANCE_METHOD,
INTERFACE_METHOD
}
public final TypeSignature owner;
public final Kind kind;
public final TypeSignature returnType;
public final String name;
public final TypeSignature[] argumentTypes;
public MethodSignature(TypeSignature owner, Kind kind, TypeSignature returnType, String name,
TypeSignature[] argumentTypes) {
super();
this.owner = owner;
this.kind = kind;
this.returnType = returnType;
this.name = name;
this.argumentTypes = argumentTypes;
cachedHash = Mappable.hashOf(toString());
}
public boolean isStatic() {
switch (kind) {
case Kind.STATIC_INIT:
case Kind.STATIC_METHOD:
return true;
default:
return false;
}
}
@Override
public String toString() {
return owner.toString() + (isStatic() ? "##" : "#") + name + "(" + TypeSignature.arrayToString(argumentTypes) + ")" + ":" + (returnType == null ? "void" : returnType.toString());
}
int cachedHash = 0;
@Override
public int mappableHash() {
return cachedHash; //Mappable.hashOf(toString());
}
@Override
public boolean mappableEquals(Mappable obj) {
if (obj instanceof MethodSignature) {
MethodSignature mso = (MethodSignature)obj;
return mso.name == this.name && this.toString().equals(mso.toString());
} else {
return false; //super.equals(obj);
}
}
}

View File

@@ -0,0 +1,50 @@
package slangc.bytecode;
public interface MinorOpcode {
public static enum CALL implements MinorOpcode {
STATIC,
INSTANCE,
NEW
}
public static enum LOADSTORE implements MinorOpcode {
LOAD_CONSTANT,
RESERVED,
LOAD_LOCAL,
STORE_LOCAL,
LOAD_INSTANCE,
STORE_INSTANCE,
LOAD_ARRAY,
STORE_ARRAY,
LOAD_STATIC,
STORE_STATIC,
}
public static enum CONVERT implements MinorOpcode {
TO_INT8,
TO_INT16,
TO_INT32,
TO_INT64,
TO_UINT8,
TO_UINT16,
TO_UINT32,
TO_UINT64,
TO_FLOAT32,
TO_FLOAT64,
TO_STRING,
TO_OBJECT,
TO_DUCK
}
public static enum ALU implements MinorOpcode {
ADD,
SUB,
SHL,
SHRX,
SHRZ,
MUL,
DIV,
MOD,
}
}

View File

@@ -0,0 +1,171 @@
package slangc.bytecode;
public class StackMachineAdapter implements StackMachineVisitor {
protected final StackMachineVisitor target;
protected final ReferenceFactory referenceFactory;
public StackMachineAdapter(StackMachineVisitor target, ReferenceFactory referenceFactory) {
this.target = target;
this.referenceFactory = referenceFactory;
}
//@Override
public StackMachineVisitor.FieldReference getFieldReference(StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference type, String name) {
return referenceFactory.getFieldReference(this, owner, isStatic, type, name);
}
//@Override
public StackMachineVisitor.MethodReference getMethodReference(StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference returnType,
String name, StackMachineVisitor.TypeReference[] argumentTypes) {
return referenceFactory.getMethodReference(this, owner, isStatic, returnType, name, argumentTypes);
}
//@Override
public StackMachineVisitor.TypeReference voidTypeReference() {
return referenceFactory.voidTypeReference(this);
}
//@Override
public StackMachineVisitor.TypeReference getTypeReference(String packageName, String typeName) {
return referenceFactory.getTypeReference(this, packageName, typeName);
}
//@Override
public void finish() {
target.finish();
}
/** Reference construction is separated out since it may be handled very differently in different scenarios.
* See DefaultReferenceFactory for a simple "pass-through" implementation. Other adapters may want to keep a
* description of the references instead and then reconstruct references as they're used, or may want to pass
* some of them through but also need to cache some information.
*/
public static interface ReferenceFactory {
public StackMachineVisitor.FieldReference toTarget(StackMachineVisitor.FieldReference localReference);
public StackMachineVisitor.MethodReference toTarget(StackMachineVisitor.MethodReference localReference);
public StackMachineVisitor.TypeReference toTarget(StackMachineVisitor.TypeReference localReference);
public StackMachineVisitor.TypeReference[] toTarget(StackMachineVisitor.TypeReference[] localReferences);
public StackMachineVisitor.FieldReference getFieldReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference type, String name);
public StackMachineVisitor.MethodReference getMethodReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference returnType,
String name, StackMachineVisitor.TypeReference[] argumentTypes);
public StackMachineVisitor.TypeReference voidTypeReference(StackMachineVisitor.StackMachineAdapter adapter);
public StackMachineVisitor.TypeReference getTypeReference(StackMachineVisitor.StackMachineAdapter adapter, String packageName, String typeName);
}
public static class DefaultReferenceFactory implements ReferenceFactory {
//@Override
public StackMachineVisitor.FieldReference toTarget(StackMachineVisitor.FieldReference localReference) {
return localReference;
}
//@Override
public StackMachineVisitor.MethodReference toTarget(StackMachineVisitor.MethodReference localReference) {
return localReference;
}
//@Override
public StackMachineVisitor.TypeReference toTarget(StackMachineVisitor.TypeReference localReference) {
return localReference;
}
//@Override
public StackMachineVisitor.TypeReference[] toTarget(StackMachineVisitor.TypeReference[] localReferences) {
return localReferences;
}
//@Override
public StackMachineVisitor.FieldReference getFieldReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic,
StackMachineVisitor.TypeReference type, String name) {
return adapter.target.getFieldReference(owner, isStatic, type, name);
}
//@Override
public StackMachineVisitor.MethodReference getMethodReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic,
StackMachineVisitor.TypeReference returnType, String name, StackMachineVisitor.TypeReference[] argumentTypes) {
return adapter.target.getMethodReference(owner, isStatic, returnType, name, argumentTypes);
}
//@Override
public StackMachineVisitor.TypeReference voidTypeReference(StackMachineAdapter adapter) {
return adapter.target.voidTypeReference();
}
//@Override
public StackMachineVisitor.TypeReference getTypeReference(StackMachineAdapter adapter, String packageName, String typeName) {
return adapter.target.getTypeReference(packageName, typeName);
}
}
public static class SystemAdapter extends StackMachineAdapter implements SystemVisitor {
public SystemAdapter(StackMachineVisitor target, ReferenceFactory referenceFactory) {
super(target, referenceFactory);
}
//@Override
public StackMachineVisitor.TypeVisitor beginType(StackMachineVisitor.TypeReference reference) {
((StackMachineVisitor.SystemVisitor)target).beginType(reference);
return null;
}
}
public static class AdapterWithAttributes extends StackMachineAdapter implements StackMachineVisitor.VisitorWithAttributes {
public AdapterWithAttributes(StackMachineVisitor.VisitorWithAttributes target, ReferenceFactory referenceFactory) {
super(target, referenceFactory);
}
//@Override
public void setProtectionType(StackMachineVisitor.ProtectionType t) {
((StackMachineVisitor.VisitorWithAttributes)target).setProtectionType(t);
}
}
public static class TypeAdapter extends AdapterWithAttributes implements StackMachineVisitor.TypeVisitor {
public TypeAdapter(StackMachineVisitor.TypeVisitor target, ReferenceFactory referenceFactory) {
super(target, referenceFactory);
}
//@Override
public void setSourceFile(String filename) {
((StackMachineVisitor.TypeVisitor)target).setSourceFile(filename);
}
//@Override
public void setBaseType(StackMachineVisitor.TypeReference reference) {
((StackMachineVisitor.TypeVisitor)target).setBaseType(referenceFactory.toTarget(reference));
}
//@Override
public void setInterfaceTypes(StackMachineVisitor.TypeReference[] interfaces) {
((StackMachineVisitor.TypeVisitor)target).setInterfaceTypes(referenceFactory.toTarget(interfaces));
}
//@Override
public void setOuterType(StackMachineVisitor.TypeReference reference) {
((StackMachineVisitor.TypeVisitor)target).setOuterType(referenceFactory.toTarget(reference));
}
//@Override
public StackMachineVisitor.FieldVisitor beginField(StackMachineVisitor.FieldReference reference) {
// TODO Auto-generated method stub
return null;
}
//@Override
public StackMachineVisitor.MethodVisitor beginMethod(StackMachineVisitor.MethodReference reference) {
// TODO Auto-generated method stub
return null;
}
}
}

View File

@@ -0,0 +1,67 @@
package slangc.bytecode;
public interface StackMachineVisitor {
public enum ProtectionType {
PRIVATE,
PROTECTED,
DEFAULT,
PUBLIC
}
public interface VisitorWithAttributes extends StackMachineVisitor {
void setProtectionType(ProtectionType t);
}
public interface SystemVisitor extends StackMachineVisitor {
TypeVisitor beginType(TypeReference reference);
}
public interface TypeVisitor extends VisitorWithAttributes {
void setSourceFile(String filename);
void setBaseType(TypeReference reference);
void setInterfaceTypes(TypeReference[] interfaces);
void setOuterType(TypeReference reference);
FieldVisitor beginField(FieldReference reference);
MethodVisitor beginMethod(MethodReference reference);
}
public interface FieldVisitor extends VisitorWithAttributes {
}
public interface MethodVisitor extends VisitorWithAttributes {
InstructionTarget beginInstructions();
}
public interface InstructionTarget extends StackMachineVisitor {
public void setSourceLine(int lineNumber);
public void labelHere(LabelReference reference);
public LabelReference newLabel(String debugName);
}
public interface Reference {
}
public interface TypeReference extends Reference {
}
public interface FieldReference extends Reference {
}
public interface MethodReference extends Reference {
}
public interface LocalReference extends Reference {
}
public interface LabelReference extends Reference {
}
public FieldReference getFieldReference(TypeReference owner, boolean isStatic, TypeReference type, String name);
public MethodReference getMethodReference(TypeReference owner, boolean isStatic, TypeReference returnType, String name, TypeReference[] argumentTypes);
public TypeReference voidTypeReference();
public TypeReference getTypeReference(String packageName, String typeName);
void finish();
}

View File

@@ -0,0 +1,16 @@
package slangc.bytecode;
/**
* This denotes the type of a typeName on the stack (a typeName which instructions work with).
* @author Zak
*
*/
public enum StackType {
DUCK,
OBJECT,
INT32,
INT64,
FLOAT32,
FLOAT64,
BOOLEAN
}

View File

@@ -0,0 +1,5 @@
package slangc.bytecode;
public interface SystemVisitor extends Visitor {
TypeVisitor visitType(TypeSignature signature);
}

View File

@@ -0,0 +1,53 @@
package slangc.bytecode;
import slang.data.Mappable;
public final class TypeSignature implements Mappable {
public final String packageName;
public final String typeName;
public static final TypeSignature VOID = new TypeSignature("kebab0.core", "void"); // TODO: Make translatable
public TypeSignature(String packageName, String typeName) {
/*if (packageName == null) {
throw new Error("Can't create type signature with null package");
}*/
if (typeName == null) {
throw new Error("Can't create type signature with null type");
}
this.packageName = packageName;
this.typeName = typeName;
}
@Override
public String toString() {
return packageName + "." + typeName;
}
public static String arrayToString(TypeSignature[] types) {
String result = "";
for (int i = 0; i < types.length; i++) {
if (i != 0) {
result += ",";
}
result += types[i].toString();
}
return result;
}
@Override
public int mappableHash() {
return Mappable.hashOf(toString());//toString().hashCode();
}
@Override
public boolean mappableEquals(Mappable obj) {
if (obj instanceof TypeSignature) {
return this.toString().equals(((TypeSignature)obj).toString());
} else {
return false; //super.equals(obj);
}
}
}

View File

@@ -0,0 +1,6 @@
package slangc.bytecode;
public interface TypeVisitor extends Visitor {
//FieldVisitor visitField(FieldSignature signature);
//MethodVisitor visitMethod(MethodSignature signature);
}

View File

@@ -0,0 +1,5 @@
package slangc.bytecode;
public interface Visitor {
void finish();
}