Skip to content

Commit

Permalink
Introduce LexicalScope, Variable, and SourceCoordinate
Browse files Browse the repository at this point in the history
This is in preparation of adding inlining.

Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jul 22, 2024
1 parent 5e07e91 commit 9071664
Show file tree
Hide file tree
Showing 23 changed files with 368 additions and 161 deletions.
18 changes: 18 additions & 0 deletions SOM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
0A1887451832C62100A2CBCA /* Smalltalk in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0A18873E1832C62100A2CBCA /* Smalltalk */; };
0A1887471832C62100A2CBCA /* TestSuite in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0A1887401832C62100A2CBCA /* TestSuite */; };
0A1C98582C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */; };
0A1C985C2C43470F00735850 /* LexicalScope.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C985B2C43470F00735850 /* LexicalScope.cpp */; };
0A1C985D2C43470F00735850 /* LexicalScope.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C985B2C43470F00735850 /* LexicalScope.cpp */; };
0A1C98602C43D6E200735850 /* Variable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C985F2C43D6E200735850 /* Variable.cpp */; };
0A1C98612C43D6E200735850 /* Variable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C985F2C43D6E200735850 /* Variable.cpp */; };
0A1C98672C4D340300735850 /* Symbols.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C98652C4D340300735850 /* Symbols.cpp */; };
0A1C98682C4D340300735850 /* Symbols.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C98652C4D340300735850 /* Symbols.cpp */; };
0A1C986B2C4D363A00735850 /* LogAllocation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C986A2C4D363A00735850 /* LogAllocation.cpp */; };
Expand Down Expand Up @@ -190,6 +194,11 @@
0A1C98562C3DD87300735850 /* unitTests/BytecodeGenerationTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = unitTests/BytecodeGenerationTest.h; sourceTree = "<group>"; };
0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = unitTests/BytecodeGenerationTest.cpp; sourceTree = "<group>"; };
0A1C98592C432E0E00735850 /* VectorUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VectorUtil.h; sourceTree = "<group>"; };
0A1C985A2C4346F900735850 /* LexicalScope.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LexicalScope.h; sourceTree = "<group>"; };
0A1C985B2C43470F00735850 /* LexicalScope.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LexicalScope.cpp; sourceTree = "<group>"; };
0A1C985E2C43D6D000735850 /* Variable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Variable.h; sourceTree = "<group>"; };
0A1C985F2C43D6E200735850 /* Variable.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Variable.cpp; sourceTree = "<group>"; };
0A1C98622C499F9400735850 /* SourceCoordinate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SourceCoordinate.h; sourceTree = "<group>"; };
0A1C98642C4D33F300735850 /* Symbols.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Symbols.h; sourceTree = "<group>"; };
0A1C98652C4D340300735850 /* Symbols.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Symbols.cpp; sourceTree = "<group>"; };
0A1C98692C4D35BB00735850 /* LogAllocation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LogAllocation.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -482,6 +491,11 @@
3F5202FC0FA6624C00E75857 /* Parser.h */,
3F5202FD0FA6624C00E75857 /* SourcecodeCompiler.cpp */,
3F5202FE0FA6624C00E75857 /* SourcecodeCompiler.h */,
0A1C985A2C4346F900735850 /* LexicalScope.h */,
0A1C985B2C43470F00735850 /* LexicalScope.cpp */,
0A1C985E2C43D6D000735850 /* Variable.h */,
0A1C985F2C43D6E200735850 /* Variable.cpp */,
0A1C98622C499F9400735850 /* SourceCoordinate.h */,
);
path = compiler;
sourceTree = "<group>";
Expand Down Expand Up @@ -851,6 +865,7 @@
0A1886FF1832BCF500A2CBCA /* VMFrame.cpp in Sources */,
0A1886E41832BCD300A2CBCA /* Interpreter.cpp in Sources */,
0A1886E71832BCDC00A2CBCA /* Heap.cpp in Sources */,
0A1C985C2C43470F00735850 /* LexicalScope.cpp in Sources */,
0A1886F71832BCF500A2CBCA /* Signature.cpp in Sources */,
0A1887351832C10E00A2CBCA /* MarkSweepCollector.cpp in Sources */,
0A1886FB1832BCF500A2CBCA /* VMBlock.cpp in Sources */,
Expand All @@ -860,6 +875,7 @@
0A3A3CB11A5D5475004CB03B /* PrimitiveLoader.cpp in Sources */,
0A1887001832BCFA00A2CBCA /* VMInteger.cpp in Sources */,
0A3A3C931A5D546D004CB03B /* Block.cpp in Sources */,
0A1C98602C43D6E200735850 /* Variable.cpp in Sources */,
0A70752A297DF9FE00EB9F59 /* ParseInteger.cpp in Sources */,
0A3A3CB01A5D5475004CB03B /* PrimitiveContainer.cpp in Sources */,
0A3A3C981A5D546D004CB03B /* Object.cpp in Sources */,
Expand Down Expand Up @@ -919,6 +935,7 @@
0A67EA7519ACD43A00830E3B /* CloneObjectsTest.cpp in Sources */,
0A67EA7919ACD43A00830E3B /* WriteBarrierTest.cpp in Sources */,
0A67EA9819ACD85300830E3B /* GenerationalCollector.cpp in Sources */,
0A1C985D2C43470F00735850 /* LexicalScope.cpp in Sources */,
0A67EA7B19ACD72C00830E3B /* VMInvokable.cpp in Sources */,
0A67EA9119ACD83200830E3B /* MethodGenerationContext.cpp in Sources */,
0A3A3CA61A5D546D004CB03B /* Method.cpp in Sources */,
Expand All @@ -945,6 +962,7 @@
0A3A3CA81A5D546D004CB03B /* Primitive.cpp in Sources */,
0A67EA9619ACD84300830E3B /* Timer.cpp in Sources */,
0A67EA8D19ACD83200830E3B /* BytecodeGenerator.cpp in Sources */,
0A1C98612C43D6E200735850 /* Variable.cpp in Sources */,
0A67EA7619ACD43A00830E3B /* main.cpp in Sources */,
0A67EA8819ACD74800830E3B /* VMPrimitive.cpp in Sources */,
0A3A3CA21A5D546D004CB03B /* Block.cpp in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,21 @@ class Lexer {

StdString GetCurrentLine();

size_t getCurrentColumn() {
size_t GetCurrentColumn() const {
return state.startBufp + 1 - state.text.length();
}

size_t GetCurrentLineNumber() {
size_t GetCurrentLineNumber() const {
return state.lineNumber;
}

bool GetPeekDone() const {
return peekDone;
}

SourceCoordinate GetCurrentSource() const {
return SourceCoordinate(GetCurrentLineNumber(), GetCurrentColumn());
}

private:
size_t fillBuffer();
Expand Down
14 changes: 14 additions & 0 deletions src/compiler/LexicalScope.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../memory/Heap.h"
#include "../vmobjects/ObjectFormats.h"
#include "LexicalScope.h"


void LexicalScope::WalkObjects(walk_heap_fn walk) {
for (auto& var : arguments) {
var.WalkObjects(walk);
}

for (auto& var : locals) {
var.WalkObjects(walk);
}
}
52 changes: 52 additions & 0 deletions src/compiler/LexicalScope.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "../vmobjects/ObjectFormats.h"
#include "Variable.h"

class LexicalScope {
friend class MethodGenerationContext;

public:
LexicalScope(LexicalScope* outer, vector<Variable> arguments, vector<Variable> locals) : outer(outer), arguments(arguments), locals(locals) {}

inline size_t GetNumberOfArguments() const {
return arguments.size();
}

inline size_t GetNumberOfLocals() const {
return locals.size();
}

void AddInlinedLocal(Variable& var) {
assert(var.GetIndex() == locals.size());
locals.push_back(var);
}

void WalkObjects(walk_heap_fn walk);

const Variable* GetLocal(size_t index, uint8_t ctxLevel) {
if (ctxLevel > 0) {
return outer->GetLocal(index, ctxLevel - 1);
}
return &locals.at(index);
}

/**
* This removes the inlined scope from the chain.
* Removal is done exactly once, after all embedded blocks
* were adapted.
*/
void DropInlinedScope() {
assert(outer != nullptr);
assert(outer->outer != nullptr);

LexicalScope* newOuter = outer->outer;
delete outer;
outer = newOuter;
}

private:
LexicalScope* outer;
vector<Variable> arguments;
vector<Variable> locals;
};
117 changes: 55 additions & 62 deletions src/compiler/MethodGenerationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,32 @@
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

#include "../interpreter/bytecodes.h"
#include "../misc/VectorUtil.h"
#include "../vm/Symbols.h"
#include "../vm/Universe.h"
#include "../vmobjects/ObjectFormats.h"
#include "../vmobjects/VMMethod.h"
#include "../vmobjects/VMPrimitive.h"
#include "../vmobjects/VMSymbol.h"
#include "BytecodeGenerator.h"
#include "MethodGenerationContext.h"
#include "Variable.h"

MethodGenerationContext::MethodGenerationContext() :
signature(nullptr), holderGenc(nullptr), outerGenc(nullptr),
primitive(false), blockMethod(false), finished(false),
currentStackDepth(0), maxStackDepth(0) { }
MethodGenerationContext::MethodGenerationContext(ClassGenerationContext& holder, MethodGenerationContext* outer) :
signature(nullptr), holderGenc(holder), outerGenc(outer),
primitive(false), blockMethod(outer != nullptr), finished(false),
currentStackDepth(0), maxStackDepth(0), maxContextLevel(outer == nullptr ? 0 : outer->GetMaxContextLevel() + 1) {
}

VMMethod* MethodGenerationContext::Assemble() {
// create a method instance with the given number of bytecodes and literals
size_t numLiterals = literals.size();

VMMethod* meth = GetUniverse()->NewMethod(signature, bytecode.size(),
numLiterals);
numLiterals, lexicalScope);

// populate the fields that are immediately available
size_t numLocals = locals.size();
Expand Down Expand Up @@ -83,11 +87,34 @@ int8_t MethodGenerationContext::FindLiteralIndex(vm_oop_t lit) {
}

uint8_t MethodGenerationContext::GetFieldIndex(VMSymbol* field) {
int16_t idx = holderGenc->GetFieldIndex(field);
int16_t idx = holderGenc.GetFieldIndex(field);
assert(idx >= 0);
return idx;
}

bool Contains(std::vector<Variable>& vec, VMSymbol* name) {
for (Variable& v : vec) {
if (v.GetName() == name) {
return true;
}
}

return false;
}

size_t IndexOf(std::vector<Variable>& vec, VMSymbol* name) {
int32_t i = 0;
for (Variable& v : vec) {
if (v.GetName() == name) {
return 0;
}
i += 1;
}

return -1;
}


bool MethodGenerationContext::FindVar(VMSymbol* var, size_t* index,
int* context, bool* isArgument) {
if ((*index = IndexOf(locals, var)) == -1) {
Expand All @@ -106,25 +133,13 @@ bool MethodGenerationContext::FindVar(VMSymbol* var, size_t* index,
}

bool MethodGenerationContext::HasField(VMSymbol* field) {
return holderGenc->HasField(field);
return holderGenc.HasField(field);
}

size_t MethodGenerationContext::GetNumberOfArguments() {
return arguments.size();
}

void MethodGenerationContext::SetHolder(ClassGenerationContext* holder) {
holderGenc = holder;
}

void MethodGenerationContext::SetOuter(MethodGenerationContext* outer) {
outerGenc = outer;
}

void MethodGenerationContext::SetIsBlockMethod(bool isBlock) {
blockMethod = isBlock;
}

void MethodGenerationContext::SetSignature(VMSymbol* sig) {
signature = sig;
}
Expand All @@ -133,14 +148,14 @@ void MethodGenerationContext::SetPrimitive(bool prim) {
primitive = prim;
}

void MethodGenerationContext::AddArgument(const std::string& arg) {
VMSymbol* argSym = SymbolFor(arg);
arguments.push_back(argSym);
void MethodGenerationContext::AddArgument(VMSymbol* arg, const SourceCoordinate& coord) {
size_t index = arguments.size();
arguments.push_back({arg, index, true, coord});
}

void MethodGenerationContext::AddLocal(const std::string& local) {
VMSymbol* localSym =SymbolFor(local);
locals.push_back(localSym);
void MethodGenerationContext::AddLocal(VMSymbol* local, const SourceCoordinate& coord) {
size_t index = locals.size();
locals.push_back({local, index, false, coord});
}

uint8_t MethodGenerationContext::AddLiteral(vm_oop_t lit) {
Expand All @@ -154,21 +169,20 @@ void MethodGenerationContext::UpdateLiteral(vm_oop_t oldValue, uint8_t index, vm
literals[index] = newValue;
}

bool MethodGenerationContext::AddArgumentIfAbsent(const std::string& arg) {
VMSymbol* argSym = SymbolFor(arg);
if (Contains(locals, argSym)) {
bool MethodGenerationContext::AddArgumentIfAbsent(VMSymbol* arg, const SourceCoordinate& coord) {
if (Contains(locals, arg)) {
return false;
}
arguments.push_back(argSym);
AddArgument(arg, coord);
return true;
}

bool MethodGenerationContext::AddLocalIfAbsent(const std::string& local) {
VMSymbol* localSym = SymbolFor(local);
if (Contains(locals, localSym)) {
bool MethodGenerationContext::AddLocalIfAbsent(VMSymbol* local, const SourceCoordinate& coord) {
if (Contains(locals, local)) {
return false;
}
locals.push_back(localSym);

AddLocal(local, coord);
return true;
}

Expand All @@ -185,43 +199,22 @@ void MethodGenerationContext::SetFinished(bool finished) {
this->finished = finished;
}

ClassGenerationContext* MethodGenerationContext::GetHolder() {
return holderGenc;
}

MethodGenerationContext* MethodGenerationContext::GetOuter() {
return outerGenc;
}

VMSymbol* MethodGenerationContext::GetSignature() {
return signature;
}

bool MethodGenerationContext::IsPrimitive() {
return primitive;
}

bool MethodGenerationContext::IsBlockMethod() {
return blockMethod;
}

bool MethodGenerationContext::IsFinished() {
return finished;
}

bool MethodGenerationContext::HasBytecodes() {
return !bytecode.empty();
}

size_t MethodGenerationContext::AddBytecode(uint8_t bc, size_t stackEffect) {
void MethodGenerationContext::AddBytecode(uint8_t bc, size_t stackEffect) {
currentStackDepth += stackEffect;
maxStackDepth = max(maxStackDepth, currentStackDepth);

bytecode.push_back(bc);
return bytecode.size();
}

size_t MethodGenerationContext::AddBytecodeArgument(uint8_t bc) {
void MethodGenerationContext::AddBytecodeArgument(uint8_t bc) {
bytecode.push_back(bc);
return bytecode.size();
}

void MethodGenerationContext::CompleteLexicalScope() {
lexicalScope = new LexicalScope(outerGenc == nullptr ? nullptr : outerGenc->lexicalScope,
arguments, locals);
}
Loading

0 comments on commit 9071664

Please sign in to comment.