Skip to content

Commit

Permalink
Implement new global constructors and destructors logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MatwayBurkow committed Jul 2, 2020
1 parent 54ebc7d commit 109f0ca
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 445 deletions.
6 changes: 3 additions & 3 deletions Block.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

"Mref" use
"Var" use
"astNodeType" use

ArgVirtual: [0n8 dynamic];
ArgGlobal: [1n8 dynamic];
Expand Down Expand Up @@ -137,8 +138,7 @@ NameWithOverloadAndRefToVar: [{
}];

TokenRef: [{
file: ["MplFile.FileSchema" use FileSchema] Mref;
token: Int32;
astNode: AstNode Cref;
}];

UsedModuleInfo: [{
Expand Down Expand Up @@ -230,7 +230,7 @@ Block: [{

refToVar: RefToVar; #refToVar of function with compiled node
varNameInfo: -1 dynamic; #variable name of imported function
indexArrayAddress: 0nx dynamic;
astArrayIndex: -1 dynamic;
matchingInfoIndex: -1 dynamic;
exportDepth: 0 dynamic;
namedFunctions: String Int32 HashTable; # name -> node ID
Expand Down
20 changes: 11 additions & 9 deletions astNodeType.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ AstNodeType: {
};

#AST Nodes
IndexArray: [Int32 Array];
IndexOfArray: [Int32];

NamedRecursiveBranch:[{
children: IndexArray; # Array of Index
children: IndexOfArray; # Array of Index
name: String; # dynamic String
nameInfo: 0 dynamic; #index in NameInfo pool
}];
Expand All @@ -50,10 +50,11 @@ AstNode: [{
token: String;
column: -1 dynamic;
line: -1 dynamic;
fileId: -1 dynamic;
data: (
IndexArray #CodeNode:
IndexOfArray #CodeNode:
NamedRecursiveBranch #LabelNode:
IndexArray #ListNode:
IndexOfArray #ListNode:
NamedBranch #NameNode:
NamedBranch #NameMemberNode:
NamedBranch #NameReadNode:
Expand All @@ -70,7 +71,7 @@ AstNode: [{
Nat64 #Numbern64Node:
Nat64 #Numbern8Node:
Nat64 #NumbernxNode:
IndexArray #ObjectNode:
IndexOfArray #ObjectNode:
Real64 #Real32Node:
Real64 #Real64Node:
String #StringNode:
Expand All @@ -94,17 +95,18 @@ ParserResult: [{
message: String;
position: PositionInfo;
};
memory: AstNode Array;
nodes: IndexArray;

memory: AstNode Array Array;
root: IndexOfArray;

INIT: []; DIE: []; # default life control, and ban uneffective copy, because object is too big
}];

ParserResults: [ParserResult Array];

MultiParserResult: [{
memory: AstNode Owner Array;
nodes: IndexArray Owner Array; # order of going is not defined before compiling
memory: AstNode Array 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
}];
171 changes: 44 additions & 127 deletions astOptimizers.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,26 @@

"astNodeType" use

optimizeLabelsInCurrentNode: [
node:;

addDataToProcess: [
copy isLabel:; recData:;
isLabel @needToSwap.pushBack
IndexArray @processedNodes.pushBack
@recData AsRef @unfinishedNodes.pushBack
0 @unfinishedIndexes.pushBack
];

node.data.getTag AstNodeType.Label = [
AstNodeType.Label @node.@data.get.@children TRUE addDataToProcess
] [
node.data.getTag AstNodeType.Code = [
AstNodeType.Code @node.@data.get FALSE addDataToProcess
] [
node.data.getTag AstNodeType.Object = [
AstNodeType.Object @node.@data.get FALSE addDataToProcess
] [
node.data.getTag AstNodeType.List = [
AstNodeType.List @node.@data.get FALSE addDataToProcess
] [
# do nothing
] if
] if
] if
] if
];

optimizeLabels: [
parserResult:;
unfinishedNodes: IndexArray AsRef Array;
processedNodes: IndexArray Array;
unfinishedIndexes: Int32 Array;
needToSwap: Cond Array;

FALSE @needToSwap.pushBack
IndexArray @processedNodes.pushBack
@parserResult.@nodes AsRef @unfinishedNodes.pushBack
0 dynamic @unfinishedIndexes.pushBack

[
uSize: unfinishedIndexes.dataSize 1 -;
index: uSize unfinishedIndexes.at copy;
nodes: @unfinishedNodes.last.data;

index nodes.dataSize < [
nodeIndex: index @nodes.at copy;
nodeIndex @processedNodes.last.pushBack
nodeIndex @parserResult.@memory.at optimizeLabelsInCurrentNode

indexAfter: uSize @unfinishedIndexes.at;
indexAfter 1 + @indexAfter set
TRUE
] [
needToSwap.last [
sz: processedNodes.dataSize copy;
src: sz 1 - @processedNodes.at;
dst: sz 2 - @processedNodes.at;
ll: dst.last copy;
@dst.popBack
src.dataSize [
i src.at @dst.pushBack
] times
ll @dst.pushBack
@src.clear
] when

lastUnfinished: @unfinishedNodes.last.@data;
processedNodes.last @lastUnfinished set
@parserResult.@memory [
currentIndexArray:;

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

@needToSwap.popBack
@processedNodes.popBack
@unfinishedIndexes.popBack
@unfinishedNodes.popBack
@current move @newIndexArray.pushBack
] each

unfinishedNodes.getSize 0 >
] if
] loop
@newIndexArray move @currentIndexArray set
] each
];

optimizeNamesInCurrentNode: [
Expand All @@ -96,17 +36,8 @@ optimizeNamesInCurrentNode: [
nameIndex @nameWithInfo.@nameInfo set
];

addToProcess: [
data:;
@data AsRef @unfinishedNodes.pushBack
0 @unfinishedIndexes.pushBack
];

(
AstNodeType.Label [optimizeName] # must not have children, it must be after label optimizing
AstNodeType.Code [addToProcess] # must not have children, it must be after label optimizing
AstNodeType.Object [addToProcess] # must not have children, it must be after label optimizing
AstNodeType.List [addToProcess] # must not have children, it must be after label optimizing
AstNodeType.Name [optimizeName]
AstNodeType.NameRead [optimizeName]
AstNodeType.NameWrite [optimizeName]
Expand All @@ -117,63 +48,49 @@ optimizeNamesInCurrentNode: [
];

optimizeNames: [
current: nameManager: ;;

unfinishedNodes: IndexArray AsRef Array;
unfinishedIndexes: Int32 Array;
@current.@nodes AsRef @unfinishedNodes.pushBack
0 dynamic @unfinishedIndexes.pushBack

[
uSize: unfinishedIndexes.dataSize 1 -;
index: uSize unfinishedIndexes.at copy;
nodes: unfinishedNodes.last.data;

index nodes.dataSize < [
index nodes.at @current.@memory.at optimizeNamesInCurrentNode # reallocation here

indexAfter: uSize @unfinishedIndexes.at;
indexAfter 1 + @indexAfter set
TRUE
] [
@unfinishedIndexes.popBack
@unfinishedNodes.popBack

unfinishedNodes.getSize 0 >
] if
] loop
parserResult: nameManager: ;;

@parserResult.@memory [
[
optimizeNamesInCurrentNode
] each
] each
];

concatParserResult: [
mresult:;
current:;
shift: mresult.memory.getSize;
fileId: mresult.roots.getSize;

adjustArray: [
indexArray:;
@indexArray [
cur:;
cur shift + @cur set
] each
astArrayIndex:;
astArrayIndex shift + @astArrayIndex set
];

@current.@nodes adjustArray
@current.@root adjustArray

@current.@memory [
currentNode:;
currentNode.data.getTag AstNodeType.Code = [
AstNodeType.Code @currentNode.@data.get adjustArray
] [
currentNode.data.getTag AstNodeType.List = [
AstNodeType.List @currentNode.@data.get adjustArray
currentArray:;
@currentArray [
currentNode:;
fileId @currentNode.@fileId set

currentNode.data.getTag AstNodeType.Code = [
AstNodeType.Code @currentNode.@data.get adjustArray
] [
currentNode.data.getTag AstNodeType.Object = [
AstNodeType.Object @currentNode.@data.get adjustArray
] when
currentNode.data.getTag AstNodeType.List = [
AstNodeType.List @currentNode.@data.get adjustArray
] [
currentNode.data.getTag AstNodeType.Object = [
AstNodeType.Object @currentNode.@data.get adjustArray
] when
] if
] if
] if
] each

@currentNode move owner @mresult.@memory.pushBack
@currentArray move @mresult.@memory.pushBack
] each

@current.@nodes move owner @mresult.@nodes.pushBack
current.root @mresult.@roots.pushBack
];
34 changes: 14 additions & 20 deletions builtinImpl.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ staticityOfBinResult: [
]
[signature: parseSignature;]
[
astNode: VarCode varBody.data.get.index processor.multiParserResult.memory.at.get;
index: signature astNode VarCode varBody.data.get.file VarString varName.data.get makeStringView FALSE dynamic @processor @block processExportFunction;
astArrayIndex: VarCode varBody.data.get.index;
index: signature astArrayIndex VarString varName.data.get makeStringView FALSE dynamic @processor @block processExportFunction;
]
) sequence
] "mplBuiltinExportFunction" @declareBuiltin ucall
Expand Down Expand Up @@ -1229,18 +1229,16 @@ staticityOfBinResult: [
condition staticityOfVar Weak > [
value: VarCond varCond.data.get.end copy;
value [
VarCode varThen.data.get.index VarCode varThen.data.get.file "staticIfThen" makeStringView @processor @block processCall
VarCode varThen.data.get.index "staticIfThen" makeStringView @processor @block processCall
] [
VarCode varElse.data.get.index VarCode varElse.data.get.file "staticIfElse" makeStringView @processor @block processCall
VarCode varElse.data.get.index "staticIfElse" makeStringView @processor @block processCall
] if
] [
condition @processor @block makeVarRealCaptured

condition
VarCode varThen.data.get.index processor.multiParserResult.memory.at.get
VarCode varThen.data.get.file
VarCode varElse.data.get.index processor.multiParserResult.memory.at.get
VarCode varElse.data.get.file
VarCode varElse.data.get.index
VarCode varThen.data.get.index
@processor @block processIf
] if
] when
Expand Down Expand Up @@ -1386,8 +1384,8 @@ staticityOfBinResult: [
varBody: body getVar;
varBody.data.getTag VarCode = ~ ["body must be [CODE]" @processor block compilerError] when
] [
astNode: VarCode varBody.data.get.index processor.multiParserResult.memory.at.get;
astNode @processor @block VarCode varBody.data.get.file processLoop
astArrayIndex: VarCode varBody.data.get.index;
astArrayIndex @processor @block processLoop
]
) sequence
] "mplBuiltinLoop" @declareBuiltin ucall
Expand Down Expand Up @@ -1704,14 +1702,11 @@ staticityOfBinResult: [
varCode.data.getTag VarCode = ~ ["branch else must be a [CODE]" @processor block compilerError] when

processor compilable [
codeIndex: VarCode varCode.data.get.index copy;
codeFile: VarCode varCode.data.get.file;
astNode: codeIndex processor.multiParserResult.memory.at.get;
[astNode.data.getTag AstNodeType.Code =] "Not a code!" assert
astArrayIndex: VarCode varCode.data.get.index copy;
block.countOfUCall 1 + @block.@countOfUCall set
block.countOfUCall 65535 > ["ucall limit exceeded" @processor block compilerError] when
indexArray: AstNodeType.Code astNode.data.get;
indexArray @block codeFile addIndexArrayToProcess
indexArray: astArrayIndex processor.multiParserResult.memory.at;
indexArray @block addIndexArrayToProcess
] when
] when
] "mplBuiltinUcall" @declareBuiltin ucall
Expand All @@ -1734,12 +1729,11 @@ staticityOfBinResult: [
condition staticityOfVar Weak > [
value: VarCond varCond.data.get.end copy;
code: value [VarCode varThen.data.get] [VarCode varElse.data.get] if;
astNode: code.index processor.multiParserResult.memory.at.get;
[astNode.data.getTag AstNodeType.Code =] "Not a code!" assert
astArrayIndex: code.index;
block.countOfUCall 1 + @block.@countOfUCall set
block.countOfUCall 65535 > ["ucall limit exceeded" @processor block compilerError] when
indexArray: AstNodeType.Code astNode.data.get;
indexArray @block code.file addIndexArrayToProcess
indexArray: astArrayIndex processor.multiParserResult.memory.at;
indexArray @block addIndexArrayToProcess
] [
"condition must be static" @processor block compilerError
] if
Expand Down
Loading

0 comments on commit 109f0ca

Please sign in to comment.