Skip to content

Commit

Permalink
Refactor matching to use comparison trees instead of linear lookup
Browse files Browse the repository at this point in the history
Improve compilation speed
Do not overwrite .ll files if the result did not change, write the result to .ll.temp instead, to facilitate build systems which depend on file timestamps (for example, MSBuild)
Make [dynamic] built-in dynamize values instead of marking them dirty
Fix aggressive caching that leads to variables having old values after being updated by reference
Fix that variables created with newVarOfTheSameType do not destruct properly
Fix that debug information provides the wrong location before function entry
Fix that predicates do not compile properly inside of a loop
  • Loading branch information
MatwayBurkow committed Jul 15, 2020
1 parent 109f0ca commit dad5182
Show file tree
Hide file tree
Showing 17 changed files with 1,391 additions and 912 deletions.
87 changes: 38 additions & 49 deletions Block.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ ArgRef: [2n8 dynamic];
ArgCopy: [3n8 dynamic];
ArgDerefCopy: [4n8 dynamic];
ArgMeta: [5n8 dynamic];
ArgReturn: [6n8 dynamic];
ArgRefDeref: [7n8 dynamic];
ArgReturnDeref: [8n8 dynamic];
ArgAlreadyUsed: [6n8 dynamic];
ArgReturn: [7n8 dynamic];
ArgRefDeref: [8n8 dynamic];
ArgReturnDeref: [9n8 dynamic];

NameCaseInvalid: [ 0n8 dynamic];
NameCaseBuiltin: [ 1n8 dynamic];
Expand Down Expand Up @@ -63,7 +64,8 @@ Argument: [{
Capture: [{
refToVar: RefToVar;
argCase: ArgRef;
captureCase: NameCaseInvalid;
stable: Cond;
mplFieldIndex: -1 dynamic;
nameInfo: -1 dynamic;
nameOverloadDepth: -1 dynamic;
file: ["MplFile.FileSchema" use FileSchema] Mref;
Expand All @@ -80,16 +82,7 @@ CompilerPositionInfo: [{
file: ["MplFile.FileSchema" use FileSchema] Mref;
line: -1 dynamic;
column: -1 dynamic;
token: String;
}];

FieldCapture: [{
object: RefToVar;
captureCase: NameCaseInvalid;
nameInfo: -1 dynamic;
nameOverloadDepth: -1 dynamic;
fieldIndex: -1 dynamic;
file: ["MplFile.FileSchema" use FileSchema] Mref;
token: StringView;
}];

makeInstruction: [{
Expand All @@ -106,21 +99,13 @@ makeInstruction: [{

Instruction: [0 0 makeInstruction];

ShadowEvent: [(
Cond #ShadowReasonNo
ShadowEventCapture #ShadowReasonCapture
ShadowEventStableName #ShadowReasonStableName
ShadowEventFieldCapture #ShadowReasonFieldCapture
ShadowEventInput #ShadowReasonInput
ShadowEventField #ShadowReasonField
ShadowEventPointee #ShadowReasonPointee
) Variant];

MatchingInfo: [{
inputs: Argument Array; #for generating signatures
captures: Capture Array; #for generating signatures, for finding 'self'
shadowEvents: ShadowEvent Array;
lastTopologyIndex: 0 dynamic;
currentInputCount: 0 dynamic;
maxInputCount: 0 dynamic;
}];

NameWithOverload: [{
Expand Down Expand Up @@ -150,8 +135,6 @@ ShadowEventInput: [Argument];

ShadowEventCapture: [Capture];

ShadowEventFieldCapture: [FieldCapture];

ShadowEventField: [{
object: RefToVar;
field: RefToVar;
Expand All @@ -163,28 +146,35 @@ ShadowEventPointee: [{
pointee: RefToVar;
}];

ShadowEventStableName: [{
nameInfo: Int32;
}];
ShadowEvent: [(
Cond #ShadowReasonNo
ShadowEventCapture #ShadowReasonCapture
ShadowEventInput #ShadowReasonInput
ShadowEventField #ShadowReasonField
ShadowEventPointee #ShadowReasonPointee
Cond #ShadowReasonLambda
) Variant];

Block: [{
id: Int32;
root: FALSE dynamic;
file: ["MplFile.FileSchema" use FileSchema] Mref;
topNode: [@BlockSchema] Mref;
capturedFiles: Cond Array;
beginPosition: CompilerPositionInfo;
parent: 0 dynamic;
nodeCase: NodeCaseCode;
stack: RefToVar Array; # we must compile node without touching parent
minStackDepth: 0 dynamic;
programTemplate: String;
program: Instruction Array;
aliases: String Array;
lastLambdaName: Int32;
nextRecLambdaId: -1 dynamic;
globalPriority: Int32;

id: Int32;
root: FALSE dynamic;
file: ["MplFile.FileSchema" use FileSchema] Mref;
topNode: [@BlockSchema] Mref;
capturedFiles: Cond Array;
beginPosition: CompilerPositionInfo;
parent: 0 dynamic;
nodeCase: NodeCaseCode;
stack: RefToVar Array; # we must compile node without touching parent
minStackDepth: 0 dynamic;
programTemplate: String;
program: Instruction Array;
aliases: String Array;
lastLambdaName: Int32;
nextRecLambdaId: -1 dynamic;
globalPriority: Int32;

instructionCountBeforeRet: Int32;
hasCallImport: FALSE dynamic;
hasEmptyLambdas: FALSE dynamic;
nodeIsRecursive: FALSE dynamic;
nextLabelIsVirtual: FALSE dynamic;
Expand Down Expand Up @@ -220,8 +210,7 @@ Block: [{

fromModuleNames: NameWithOverloadAndRefToVar Array;
labelNames: NameWithOverloadAndRefToVar Array;
captureNames: NameWithOverloadAndRefToVar Array;
fieldCaptureNames: NameWithOverloadAndRefToVar Array;
captureNames: NameWithOverload Array;
stableNames: Int32 Array;

candidatesToDie: RefToVar Array;
Expand All @@ -231,7 +220,7 @@ Block: [{
refToVar: RefToVar; #refToVar of function with compiled node
varNameInfo: -1 dynamic; #variable name of imported function
astArrayIndex: -1 dynamic;
matchingInfoIndex: -1 dynamic;
matchingChindIndex: -1 dynamic;
exportDepth: 0 dynamic;
namedFunctions: String Int32 HashTable; # name -> node ID
capturedVars: RefToVar Array;
Expand Down
16 changes: 16 additions & 0 deletions NameManager.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ NameManager: [{
index
];

findItemStrong: [
index: file: nameId:;; copy;
items: nameId names.at.items;

index -1 = [items.size !index] when
[
index 1 - !index
index -1 = [
itemFile: index items.at.file;
file isNil [itemFile file is] ||
] || ~
] loop

index
];

hasOverload: [
nameId: copy;
nameId @names.at.overloadCount 0 >
Expand Down
19 changes: 12 additions & 7 deletions Var.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ Weak: [2n8 dynamic];
Static: [3n8 dynamic];
Virtual: [4n8 dynamic];

ShadowReasonNo: [0];
ShadowReasonCapture: [1];
ShadowReasonStableName: [2];
ShadowReasonFieldCapture: [3];
ShadowReasonInput: [4];
ShadowReasonField: [5];
ShadowReasonPointee: [6];
ShadowReasonNo: [0];
ShadowReasonCapture: [1];
ShadowReasonInput: [2];
ShadowReasonField: [3];
ShadowReasonPointee: [4];
ShadowReasonTreeSplitterLambda: [5];

VarInvalid: [ 0 static];
VarCond: [ 1 static];
Expand Down Expand Up @@ -137,6 +136,7 @@ Variable: [{
storageStaticity: Static;
global: FALSE dynamic;
usedInHeader: FALSE dynamic;
usedInParams: FALSE dynamic;
capturedByPtr: FALSE dynamic;
capturedAsRealValue: FALSE dynamic;
capturedForDeref: FALSE dynamic;
Expand Down Expand Up @@ -202,6 +202,11 @@ isVirtual: [
[refToVar isVirtualType] ||
];

noMatterToCopy: [
refToVar:;
refToVar isVirtual [refToVar isAutoStruct ~] &&
];

isVirtualType: [
refToVar:;

Expand Down
28 changes: 16 additions & 12 deletions astNodeType.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ NamedBranch: [{
nameInfo: 0 dynamic; #index in NameInfo pool
}];

makePositionInfo: [{
token: String;
column: copy;
line: copy;
offset: copy;
fileId: copy;
}];

PositionInfo: [-1 dynamic -1 dynamic 1 dynamic 0 dynamic makePositionInfo];

AstNode: [{
virtual AST_NODE: ();
token: String;
column: -1 dynamic;
line: -1 dynamic;
fileId: -1 dynamic;
positionInfo: PositionInfo;
data: (
IndexOfArray #CodeNode:
NamedRecursiveBranch #LabelNode:
Expand Down Expand Up @@ -80,14 +87,11 @@ AstNode: [{
INIT: []; DIE: []; # default life control, and ban uneffective copy, because object is too big
}];

makePositionInfo: [{
column: copy;
line: copy;
offset: copy;
AstNodeArray: [{
positionInfo: PositionInfo;
nodes: AstNode Array;
}];

PositionInfo: [-1 dynamic 1 dynamic 0 dynamic makePositionInfo];

ParserResult: [{
virtual PARSER_RESULT: ();
success: TRUE dynamic;
Expand All @@ -96,7 +100,7 @@ ParserResult: [{
position: PositionInfo;
};

memory: AstNode Array Array;
memory: AstNodeArray Array;
root: IndexOfArray;

INIT: []; DIE: []; # default life control, and ban uneffective copy, because object is too big
Expand All @@ -105,7 +109,7 @@ ParserResult: [{
ParserResults: [ParserResult Array];

MultiParserResult: [{
memory: AstNode Array Array;
memory: AstNodeArray Array;
roots: IndexOfArray Array; # order of going is not defined before compiling

INIT: []; DIE: []; # default life control, and ban uneffective copy, because object is too big
Expand Down
18 changes: 9 additions & 9 deletions astOptimizers.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ optimizeLabels: [
@parserResult.@memory [
currentIndexArray:;

newIndexArray: AstNode Array;
@currentIndexArray [
newIndexArray: AstNodeArray;
@currentIndexArray.@positionInfo move @newIndexArray.@positionInfo set

@currentIndexArray.@nodes [
current:;
current.data.getTag AstNodeType.Label = [
AstNodeType.Label current.data.get.children @parserResult.@memory.at [
move @newIndexArray.pushBack
AstNodeType.Label current.data.get.children @parserResult.@memory.at.@nodes [
move @newIndexArray.@nodes.pushBack
] each
] when

@current move @newIndexArray.pushBack
@current move @newIndexArray.@nodes.pushBack
] each

@newIndexArray move @currentIndexArray set
Expand Down Expand Up @@ -51,7 +53,7 @@ optimizeNames: [
parserResult: nameManager: ;;

@parserResult.@memory [
[
.@nodes [
optimizeNamesInCurrentNode
] each
] each
Expand All @@ -61,7 +63,6 @@ concatParserResult: [
mresult:;
current:;
shift: mresult.memory.getSize;
fileId: mresult.roots.getSize;

adjustArray: [
astArrayIndex:;
Expand All @@ -72,9 +73,8 @@ concatParserResult: [

@current.@memory [
currentArray:;
@currentArray [
@currentArray.@nodes [
currentNode:;
fileId @currentNode.@fileId set

currentNode.data.getTag AstNodeType.Code = [
AstNodeType.Code @currentNode.@data.get adjustArray
Expand Down
Loading

0 comments on commit dad5182

Please sign in to comment.