Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
image-segments
.DS_Store
release/*
21 changes: 21 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"~/.conan2/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "macos-clang-arm64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
16 changes: 12 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/cpp/egg",
"args": ["${workspaceFolder}/image-segments/Kernel.ems"],
"program": "${workspaceFolder}/runtime/cpp/build/egg",
"args": ["TinyBenchmarks"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"cwd": "${workspaceFolder}/image-segments/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"linux" : {
"MIMode": "gdb",
},
"osx" : {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
},
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cmake.sourceDirectory": "${workspaceFolder}/runtime/cpp",
"cmake.configureArgs": [
"-DCMAKE_TOOLCHAIN_FILE=${workspaceFolder}/runtime/cpp/build/conan_toolchain.cmake",
"-DCMAKE_PREFIX_PATH=${workspaceFolder}/runtime/cpp/build"
]
}
8 changes: 6 additions & 2 deletions image-segments/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
IMAGE_SEGMENTS_DIR=$(ROOT_DIR)/image-segments
PHARO_DIR=$(GIT_PATH)/runtime/pharo

.PHONY: always_rebuild

%.json:
always_rebuild:
@true # Always succeed, forces rebuild

%.json: always_rebuild
export IMAGE_SEGMENTS_DIR
cd $(PHARO_DIR) && ./pharo eggjs.image eval "EggBuilder forJSPlatform generateSegmentFromModuleNamed: '$*'"

%.ems:
%.ems: always_rebuild
export IMAGE_SEGMENTS_DIR
cd $(PHARO_DIR) && ./pharo eggjs.image eval "EggBuilder forNativePlatform generateSegmentFromModuleNamed: '$*'"

9 changes: 2 additions & 7 deletions modules/Examples/HelloWorld/HelloWorldModule.st
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
Class {
#name : #HelloWorldModule,
#superclass : #Module,
#instVars : [
''
],
#category : #'Examples-HelloWorld'
}

{ #category : #spec }
HelloWorldModule >> imports [
^{
#Kernel -> #(Transcript)
}
^{}
]

{ #category : #services }
HelloWorldModule >> main: arguments [
Transcript show: 'Hello, World!'.
Kernel log: 'Hello, World!', String cr.
^0
]
32 changes: 31 additions & 1 deletion modules/Kernel/HostSystem.st
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,41 @@ HostSystem >> load: aSymbol [
^module justLoaded
]

{ #category : #logging }
HostSystem >> log: aString level: anInteger [
<primitive: HostLog>
]

{ #category : #logging }
HostSystem >> logTrace: aString [
self log: aString level: 0
]

{ #category : #logging }
HostSystem >> logDebug: aString [
self log: aString level: 1
]

{ #category : #logging }
HostSystem >> logInfo: aString [
self log: aString level: 2
]

{ #category : #logging }
HostSystem >> logWarning: aString [
self log: aString level: 3
]

{ #category : #logging }
HostSystem >> logError: aString [
<primitive: HostLogError>
self log: aString level: 4
]

{ #category : #logging }
HostSystem >> logFatal: aString [
self log: aString level: 5

]
{ #category : #loading }
HostSystem >> platformName [
<primitive: HostPlatformName>
Expand Down
5 changes: 5 additions & 0 deletions modules/Kernel/KernelModule.st
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ KernelModule >> loadedModuleNamed: aSymbol [
^loadedModules at: aSymbol ifAbsent: []
]

{ #category : #logging }
KernelModule >> log: aString [
^host log: aString level: 2
]

{ #category : #accessing }
KernelModule >> memory [
^memory
Expand Down
3 changes: 3 additions & 0 deletions runtime/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/*
.idea/*
CMakeUserPresets.json
12 changes: 5 additions & 7 deletions runtime/cpp/Bootstrapper.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@

#include "Evaluator/Runtime.h"
#include "ImageSegment.h"
#include "HeapObject.h"

#include <map>
#include <string>
#include <vector>
#include <filesystem>
#include <fstream>
#include <Allocator/GCHeap.h>
#include <Allocator/GCSpace.h>
#include <Evaluator/Evaluator.h>
#include <Evaluator/SAssociationBinding.h>

#include "Allocator/GCHeap.h"
#include "Allocator/GCSpace.h"
#include "Evaluator/Evaluator.h"
#include "Evaluator/SAssociationBinding.h"

namespace Egg {

Expand Down
2 changes: 1 addition & 1 deletion runtime/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ find_package(cxxopts REQUIRED)

# *************************************************

file(GLOB ALL_SRC "*.cpp" "Evaluator/*.cpp" "Allocator/*.cpp" "${PLATFORM}/*.cpp")
file(GLOB ALL_SRC "*.cpp" "Evaluator/*.cpp" "Allocator/*.cpp" "Utils/*.cpp" "${PLATFORM}/*.cpp")

add_executable(${EXE} ${ALL_SRC})
target_link_libraries(${EXE} libffi::libffi cxxopts::cxxopts) # link agains libffi
Expand Down
23 changes: 23 additions & 0 deletions runtime/cpp/Evaluator/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ void Evaluator::initializePrimitives()
this->addPrimitive("HostInitializeFFI", &Evaluator::primitiveHostInitializeFFI);
this->addPrimitive("HostPlatformName", &Evaluator::primitiveHostPlatformName);
this->addPrimitive("HostCurrentMilliseconds", &Evaluator::primitiveHostCurrentMilliseconds);
this->addPrimitive("HostLog", &Evaluator::primitiveHostLog);

/*this->addPrimitive("PrepareForExecution", &Evaluator::primitivePrepareForExecution);
this->addPrimitive("ProcessVMStackInitialize", &Evaluator::primitiveProcessVMStackInitialize);
this->addPrimitive("ProcessVMStackAt", &Evaluator::primitiveProcessVMStackAt);
Expand Down Expand Up @@ -675,6 +677,27 @@ Object* Evaluator::primitiveHostPlatformName() {
return (Object*)this->_runtime->newString_(PlatformName());
}

Object* Evaluator::primitiveHostLog() {
auto arg = this->_context->firstArgument();
auto code = this->_context->secondArgument()->asSmallInteger()->asNative();

std::string message;
if (arg->isSmallInteger())
message = arg->printString();
else
{
auto harg = arg->asHeapObject();
auto species = _runtime->behaviorClass_(harg->behavior());
if (species == _runtime->_stringClass)
message = harg->asLocalString();
else
message = harg->printString();
}

_runtime->log_code_(message, code);
return this->_regR;
}

Object* Evaluator::primitiveHostInitializeFFI() {
auto library = this->_context->firstArgument()->asHeapObject();
auto handle = library->slotAt_(1);
Expand Down
1 change: 1 addition & 0 deletions runtime/cpp/Evaluator/Evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class Evaluator : public SExpressionVisitor {
Object* primitiveHostFixOverrides();
Object* primitiveHostInitializeFFI();
Object* primitiveHostLoadModule();
Object* primitiveHostLog();
Object* primitiveHostPlatformName();
Object* primitiveNew();
Object* primitiveNewBytes();
Expand Down
52 changes: 48 additions & 4 deletions runtime/cpp/Evaluator/Runtime.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

#include "Runtime.h"

#include <Bootstrapper.h>
#include <map>
#include <sstream>

#include "Runtime.h"
#include "Bootstrapper.h"
#include "Evaluator.h"
#include "Allocator/GCHeap.h"
#include "SAbstractMessage.h"
#include "KnownConstants.h"
#include "GCedRef.h"
#include "StackGCedRef.h"
#include "Utils/CRLFStream.h"

using namespace Egg;

Expand Down Expand Up @@ -421,3 +420,48 @@ std::string Egg::Runtime::print_(HeapObject *obj) {

return "a " + name + "";
}


// Color codes for different log levels
#define RESET "\033[0m"
#define RED "\033[31m"
#define YELLOW "\033[33m"
#define GREEN "\033[32m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"

void Runtime::log_code_(std::string &message, uintptr_t level)
{
std::ofstream logFile("error-log.txt", std::ios_base::app); // Open log file in append mode

CRLFStream lfout(std::cout);

switch(level) {
case 0: // TRACE
lfout << BLUE << "[trace] " + message; break;
case 1: // DEBUG
lfout << GREEN << message; break;
case 2: // INFO
lfout << message; break;
case 3: // WARN
lfout << YELLOW << "[warn] " + message;
break;
case 4: // ERROR
lfout.setStream(std::cerr);
lfout << RED << "[error] " + message;
break;
case 5: // FATAL
lfout.setStream(std::cerr);
lfout << MAGENTA << "[FATAL] " + message;
break;
default:
return; // Invalid level
}

// Also write to file for all error levels
if (level > 4) {
logFile << "[LEVEL " << level << "] " << message;
}
}

1 change: 1 addition & 0 deletions runtime/cpp/Evaluator/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Runtime {
Runtime(Bootstrapper *bootstrapper, ImageSegment *kernel);

std::string print_(HeapObject* obj);
void log_code_(std::string &string, uintptr_t code);

void initializeEvaluator();

Expand Down
10 changes: 4 additions & 6 deletions runtime/cpp/ImageSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,11 @@ ImageSegment::load(std::istream *data)
this->readImportDescriptors(data);
this->readExports(data);
}

#include <inttypes.h>
void ImageSegment::fixPointerSlots(const std::vector<Object*>& imports)
{
intptr_t delta = this->_currentBase - this->header.baseAddress;

constexpr auto mask = static_cast<uintptr_t>(-1LL << 32); // discards lower 32 bits
uintptr_t oldBehaviorBase = this->header.baseAddress & mask;

uintptr_t oldBehaviorBase = this->header.baseAddress & (((intptr_t)-1) << 32); // discards lower 32 bits
auto spaceStart = this->spaceStart();
auto current = ((HeapObject::ObjectHeader*)spaceStart)->object();
auto end = (HeapObject*)this->spaceEnd();
Expand All @@ -66,7 +63,8 @@ void ImageSegment::fixPointerSlots(const std::vector<Object*>& imports)
auto behavior = current->basicBehavior();
if (((uintptr_t)behavior & 0x3) == 0x0) // if an oop
{
current->behavior((HeapObject*)(oldBehaviorBase + behavior + delta));
auto newBehavior = oldBehaviorBase + (intptr_t)behavior + delta;
current->behavior((HeapObject*)newBehavior);
}
else if (((uintptr_t)behavior & 0x3) == 0x2) // if an import
{
Expand Down
6 changes: 2 additions & 4 deletions runtime/cpp/Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
#include <iostream>
#include <vector>
#include <cstring>

#include "Launcher.h"

#include <algorithm>
#include <Evaluator/Evaluator.h>

#include "Launcher.h"
#include "ImageSegment.h"
#include "Util.h"
#include "Bootstrapper.h"

#include "Evaluator/Evaluator.h"
#include "Evaluator/Runtime.h"

using namespace Egg;
Expand Down
Loading