1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/API.cpp Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,483 @@
1.4 +/* vim: set ts=4 sw=4 tw=99 et:
1.5 + *
1.6 + * Copyright (C) 2012 David Anderson
1.7 + *
1.8 + * This file is part of JITCraft.
1.9 + *
1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
1.11 + * the terms of the GNU General Public License as published by the Free
1.12 + * Software Foundation, either version 3 of the License, or (at your option)
1.13 + * any later version.
1.14 + *
1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License along with
1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#include "../include/keima.h"
1.23 +#include "Zone.h"
1.24 +#include "RootScopes.h"
1.25 +#include "CompileContext.h"
1.26 +#include "Parser.h"
1.27 +#include "Scopes.h"
1.28 +#include "SemanticAnalysis.h"
1.29 +#include "BytecodeCompiler.h"
1.30 +#include "Modules.h"
1.31 +#include "GlobalRoots.h"
1.32 +#include "Reference.h"
1.33 +#include "Array.h"
1.34 +#include "Interpreter.h"
1.35 +#include "Stack.h"
1.36 +#include "Strings.h"
1.37 +
1.38 +using namespace ke;
1.39 +
1.40 +bool
1.41 +Ke::Keima::Initialize()
1.42 +{
1.43 + SingleThreadedZone = new Zone();
1.44 + if (!SingleThreadedZone || !SingleThreadedZone->initialize())
1.45 + return false;
1.46 + return true;
1.47 +}
1.48 +
1.49 +void
1.50 +Ke::Keima::Shutdown()
1.51 +{
1.52 + delete SingleThreadedZone;
1.53 + SingleThreadedZone = NULL;
1.54 +}
1.55 +
1.56 +Ke::HandleScope::HandleScope()
1.57 + : zone_(ke::ZONE()),
1.58 + position_(zone_->rootScopes().position()),
1.59 + prev_(*zone_->rootScopes().scope())
1.60 +{
1.61 + *zone_->rootScopes().scope() = this;
1.62 +}
1.63 +
1.64 +Ke::HandleScope::~HandleScope()
1.65 +{
1.66 + zone_->rootScopes().unwind(position_);
1.67 + *zone_->rootScopes().scope() = prev_;
1.68 +}
1.69 +
1.70 +ke::Object **
1.71 +Ke::HandleScope::CreateHandle(ke::Object *obj)
1.72 +{
1.73 + if (!obj)
1.74 + return NULL;
1.75 +
1.76 + return ZONE()->rootScopes().make(obj);
1.77 +}
1.78 +
1.79 +template <typename From, typename To> static inline To *
1.80 +Open(Ke::Handle<From> from)
1.81 +{
1.82 + return *reinterpret_cast<To **>(*from);
1.83 +}
1.84 +
1.85 +template <typename From, typename To> static inline To *
1.86 +Open(From *from)
1.87 +{
1.88 + return *reinterpret_cast<To **>(from);
1.89 +}
1.90 +
1.91 +template <typename From, typename To> static inline Ke::Local<To>
1.92 +ToLocal(From *from)
1.93 +{
1.94 + return Ke::Local<To>(reinterpret_cast<To *>(Ke::HandleScope::CreateHandle(from)));
1.95 +}
1.96 +
1.97 +static ke::Module *
1.98 +CompileFromSource(CompileContext &cc, const char *path, const char *stream, size_t length)
1.99 +{
1.100 + Parser parser(ZONE(), cc, stream, length);
1.101 + ParseTree *tree = parser.parse();
1.102 + if (!tree)
1.103 + return NULL;
1.104 + tree->dump(stdout);
1.105 +
1.106 + if (!Scope::Bind(cc, tree))
1.107 + return NULL;
1.108 +
1.109 + SemanticAnalysis semantics(ZONE(), cc, tree);
1.110 + if (!semantics.analyze())
1.111 + return NULL;
1.112 +
1.113 + Scope::Allocate(tree);
1.114 +
1.115 + BytecodeCompiler compiler(ZONE(), tree, NULL);
1.116 + ke::Local<Module> module(ke::ZONE(), compiler.compile());
1.117 + if (!module)
1.118 + return NULL;
1.119 +
1.120 + ke::InvokeArgs args;
1.121 + if (!ke::ZONE()->stack().pushModuleFrame(module, args))
1.122 + return NULL;
1.123 + if (!ke::Interpret(ke::ZONE(), NULL, NULL))
1.124 + return NULL;
1.125 +
1.126 + return module;
1.127 +}
1.128 +
1.129 +Ke::Local<Ke::Module>
1.130 +Ke::Compile(const char *path)
1.131 +{
1.132 + RootScope roots(ZONE());
1.133 + CompileContext cc(ZONE(), path);
1.134 +
1.135 + char *bytes = NULL;
1.136 + size_t length;
1.137 + {
1.138 + FILE *fp = fopen(path, "rb");
1.139 + if (!fp) {
1.140 + cc.reportError(SourcePosition(), Message_CantOpenFile);
1.141 + return Ke::Local<Ke::Module>();
1.142 + }
1.143 +
1.144 + if (fseek(fp, 0, SEEK_END)) {
1.145 + cc.reportError(SourcePosition(), Message_ErrorSeekingFile);
1.146 + fclose(fp);
1.147 + return Ke::Local<Ke::Module>();;
1.148 + }
1.149 + long size = ftell(fp);
1.150 + if (size < 0) {
1.151 + cc.reportError(SourcePosition(), Message_ErrorSeekingFile);
1.152 + fclose(fp);
1.153 + return Ke::Local<Ke::Module>();;
1.154 + }
1.155 + rewind(fp);
1.156 + length = size_t(size);
1.157 + bytes = new char[length + 1];
1.158 + if (fread(bytes, 1, length, fp) != length) {
1.159 + cc.reportError(SourcePosition(), Message_ErrorReadingFile);
1.160 + fclose(fp);
1.161 + delete [] bytes;
1.162 + return Ke::Local<Ke::Module>();;
1.163 + }
1.164 + fclose(fp);
1.165 + }
1.166 +
1.167 + ke::Local<ke::Module> module(ZONE(), CompileFromSource(cc, path, bytes, length));
1.168 + delete [] bytes;
1.169 + if (!module)
1.170 + return Ke::Local<Ke::Module>();
1.171 +
1.172 + ke::Local<ke::String> pathstr(ZONE(), ke::String::New(ZONE(), path, Heap::Tenure_Old));
1.173 + if (!pathstr)
1.174 + return Ke::Local<Ke::Module>();
1.175 +
1.176 + module->setPath(pathstr);
1.177 + return ToLocal<ke::Module, Ke::Module>(module);
1.178 +}
1.179 +
1.180 +void
1.181 +Ke::Keima::InstallMessageReporter(Ke::IMessageReporter *reporter)
1.182 +{
1.183 + ZONE()->installErrorReporter(reporter);
1.184 +}
1.185 +
1.186 +class StackFrameIterator : public Ke::IStackFrameIterator
1.187 +{
1.188 + StackIter iter_;
1.189 +
1.190 + public:
1.191 + StackFrameIterator()
1.192 + : iter_(ZONE())
1.193 + {
1.194 + }
1.195 +
1.196 + virtual bool done() {
1.197 + return iter_.done();
1.198 + }
1.199 +
1.200 + const char *function() {
1.201 + return iter_.function();
1.202 + }
1.203 +
1.204 + const char *source() {
1.205 + return iter_.source();
1.206 + }
1.207 +
1.208 + unsigned lineNumber() {
1.209 + return iter_.lineNumber();
1.210 + }
1.211 +
1.212 + void prev() {
1.213 + iter_.prev();
1.214 + }
1.215 +};
1.216 +
1.217 +Ke::IStackFrameIterator *
1.218 +Ke::Keima::NewStackFrameIterator()
1.219 +{
1.220 + return new StackFrameIterator();
1.221 +}
1.222 +
1.223 +static inline ke::HeapRoot *
1.224 +FromAPI(ke::HeapRoot *from)
1.225 +{
1.226 + ke::Address address = reinterpret_cast<ke::Address>(from);
1.227 + address -= offsetof(ke::HeapRoot, obj);
1.228 + return reinterpret_cast<ke::HeapRoot *>(address);
1.229 +}
1.230 +
1.231 +ke::HeapRoot *
1.232 +Ke::Internals::NewHeapRoot(ke::Object **ptr)
1.233 +{
1.234 + ke::Local<ke::Object> obj(ZONE(), *ptr);
1.235 + ke::HeapRoot *root = ke::ZONE()->globalRoots()->create(obj);
1.236 + if (!root)
1.237 + return NULL;
1.238 +
1.239 + // Compute an interior pointer, so it looks like a Handle to an object.
1.240 + return reinterpret_cast<ke::HeapRoot *>(&root->obj);
1.241 +}
1.242 +
1.243 +void
1.244 +Ke::Internals::IncRef(ke::HeapRoot *root)
1.245 +{
1.246 + ZONE()->globalRoots()->incref(FromAPI(root));
1.247 +}
1.248 +
1.249 +void
1.250 +Ke::Internals::DecRef(ke::HeapRoot *root)
1.251 +{
1.252 + ZONE()->globalRoots()->decref(FromAPI(root));
1.253 +}
1.254 +
1.255 +bool
1.256 +Ke::Keima::IsInCallStack(Handle<Module> argModule)
1.257 +{
1.258 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(argModule));
1.259 + return ZONE()->stack().isInCallStack(module);
1.260 +}
1.261 +
1.262 +void
1.263 +Ke::Keima::CollectGarbage()
1.264 +{
1.265 + ke::ZONE()->heap().reclaim();
1.266 +}
1.267 +
1.268 +unsigned
1.269 +Ke::Module::numPublics()
1.270 +{
1.271 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
1.272 + if (!module->publics())
1.273 + return 0;
1.274 + return module->publics()->length();
1.275 +}
1.276 +
1.277 +Ke::Local<Ke::Function>
1.278 +Ke::Module::getPublic(unsigned index)
1.279 +{
1.280 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
1.281 + ke::Local<ke::Function> fun(ke::ZONE(), ke::Function::cast(module->publics()->at(index)));
1.282 + return ToLocal<ke::Function, Ke::Function>(fun);
1.283 +}
1.284 +
1.285 +unsigned
1.286 +Ke::Module::numNatives()
1.287 +{
1.288 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
1.289 + if (!module->natives())
1.290 + return 0;
1.291 + return module->natives()->length();
1.292 +}
1.293 +
1.294 +Ke::Local<Ke::Native>
1.295 +Ke::Module::getNative(unsigned index)
1.296 +{
1.297 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
1.298 + ke::Local<ke::Native> fun(ke::ZONE(), ke::Native::cast(module->natives()->at(index)));
1.299 + return ToLocal<ke::Native, Ke::Native>(fun);
1.300 +}
1.301 +
1.302 +void
1.303 +Ke::Module::setNativeIdentity(void *identity)
1.304 +{
1.305 + ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
1.306 + module->setNativeIdentity(identity);
1.307 +}
1.308 +
1.309 +unsigned
1.310 +Ke::String::length()
1.311 +{
1.312 + ke::Local<ke::String> obj(ke::ZONE(), Open<Ke::String, ke::String>(this));
1.313 + return obj->length();
1.314 +}
1.315 +
1.316 +const char *
1.317 +Ke::String::chars()
1.318 +{
1.319 + ke::Local<ke::String> obj(ke::ZONE(), Open<Ke::String, ke::String>(this));
1.320 + return obj->chars();
1.321 +}
1.322 +
1.323 +Ke::Local<Ke::String>
1.324 +Ke::Function::name()
1.325 +{
1.326 + ke::Local<ke::Function> fun(ke::ZONE(), Open<Ke::Function, ke::Function>(this));
1.327 + return ToLocal<ke::String, Ke::String>(fun->name());
1.328 +}
1.329 +
1.330 +Ke::Local<Ke::String>
1.331 +Ke::Native::name()
1.332 +{
1.333 + ke::Local<ke::Native> native(ke::ZONE(), Open<Ke::Native, ke::Native>(this));
1.334 + return ToLocal<ke::String, Ke::String>(native->name());
1.335 +}
1.336 +
1.337 +void
1.338 +Ke::Native::bind(void *function)
1.339 +{
1.340 + ke::Local<ke::Native> native(ke::ZONE(), Open<Ke::Native, ke::Native>(this));
1.341 + native->bind(function);
1.342 +}
1.343 +
1.344 +static inline bool
1.345 +CheckInvokeType(const Ke::Parameter ¶m, Type *type)
1.346 +{
1.347 + switch (param.type()) {
1.348 + case Ke::Parameter::Array:
1.349 + return type->isArray() &&
1.350 + type->levels() == 1 &&
1.351 + type->contained()->isPrimitive() &&
1.352 + (type->contained()->primitive() != PrimitiveType_Char);
1.353 + case Ke::Parameter::CharArray:
1.354 + return type->isCharArray();
1.355 + case Ke::Parameter::Float:
1.356 + return type->isFloat();
1.357 + case Ke::Parameter::FloatRef:
1.358 + return type->isReference() && type->contained()->isFloat();
1.359 + case Ke::Parameter::Int:
1.360 + return type->isInt32();
1.361 + case Ke::Parameter::IntRef:
1.362 + return type->isReference() && type->contained()->isInt32();
1.363 + default:
1.364 + return false;
1.365 + }
1.366 +}
1.367 +
1.368 +bool
1.369 +Ke::Keima::CheckInvokeTypes(Ke::Handle<Ke::Function> argFun, const Ke::Parameter *params, unsigned nparams)
1.370 +{
1.371 + ke::Local<ke::Function> fun(ke::ZONE(), Open<Ke::Function, ke::Function>(argFun));
1.372 +
1.373 + if (fun->nargs() != nparams)
1.374 + return false;
1.375 +
1.376 + for (unsigned i = 0; i < nparams; i++) {
1.377 + if (!CheckInvokeType(params[i], fun->argType(i)))
1.378 + return false;
1.379 + }
1.380 +
1.381 + // Functions can only return tagged cells, for ISPE1-compatible Invoke().
1.382 + ke::Local<ke::Type> returnType(ke::ZONE(), fun->type()->returnType());
1.383 + if ((!returnType->isPrimitive() && !returnType->isVoid()) || returnType->isTraceable())
1.384 + return false;
1.385 +
1.386 + return true;
1.387 +}
1.388 +
1.389 +bool
1.390 +Ke::Keima::Invoke(Handle<Function> argFun, const Parameter *params, unsigned nparams, int *retval)
1.391 +{
1.392 + assert(CheckInvokeTypes(argFun, params, nparams));
1.393 +
1.394 + ke::Local<ke::Function> fun(ke::ZONE(), Open<Ke::Function, ke::Function>(argFun));
1.395 +
1.396 + if (fun->nargs() != nparams) {
1.397 + // :TODO: report error
1.398 + assert(false);
1.399 + return false;
1.400 + }
1.401 +
1.402 + ke::InvokeArgs args;
1.403 + if (!ke::ZONE()->stack().pushInvokeFrame(fun, args))
1.404 + return false;
1.405 +
1.406 + for (unsigned i = 0; i < nparams; i++) {
1.407 + ke::LocalSlot &slot = args.arg(i);
1.408 + switch (params[i].type()) {
1.409 + case Ke::Parameter::Int:
1.410 + slot.i32 = params[i].i32();
1.411 + break;
1.412 +
1.413 + case Ke::Parameter::Float:
1.414 + slot.f = params[i].f();
1.415 + break;
1.416 +
1.417 + case Ke::Parameter::IntRef:
1.418 + if (params[i].writeback()) {
1.419 + slot.ref = Reference::NewStack(ke::ZONE(), params[i].i32p(), Heap::Tenure_Stack);
1.420 + } else {
1.421 + LocalSlot other;
1.422 + other.i32 = *params[i].i32p();
1.423 + slot.ref = Reference::NewStack(ke::ZONE(), other, Heap::Tenure_Stack);
1.424 + }
1.425 + if (!slot.ref)
1.426 + return false;
1.427 + break;
1.428 +
1.429 + case Ke::Parameter::FloatRef:
1.430 + if (params[i].writeback()) {
1.431 + slot.ref = Reference::NewStack(ke::ZONE(), params[i].fp(), Heap::Tenure_Stack);
1.432 + } else {
1.433 + LocalSlot other;
1.434 + other.f = *params[i].fp();
1.435 + slot.ref = Reference::NewStack(ke::ZONE(), other, Heap::Tenure_Stack);
1.436 + }
1.437 + if (!slot.ref)
1.438 + return false;
1.439 + break;
1.440 +
1.441 + case Ke::Parameter::Array:
1.442 + {
1.443 + ke::Local<ke::ArrayMap> map(ke::ZONE(), ke::ZONE()->genericArrayMap());
1.444 + if (params[i].writeback()) {
1.445 + slot.obj = Array::NewExternal(ke::ZONE(), map, params[i].length(),
1.446 + params[i].buffer(), Heap::Tenure_Stack);
1.447 + if (!slot.obj)
1.448 + return false;
1.449 + } else {
1.450 + Array *array = Array::New(ke::ZONE(), map, params[i].length(), Heap::Tenure_Stack);
1.451 + if (!slot.obj)
1.452 + return false;
1.453 + memcpy(array->vec(), params[i].buffer(), params[i].length() * sizeof(int));
1.454 + }
1.455 + break;
1.456 + }
1.457 +
1.458 + case Ke::Parameter::CharArray:
1.459 + {
1.460 + ke::Local<ke::ArrayMap> map(ke::ZONE(), ke::ZONE()->charArrayMap());
1.461 + if (params[i].writeback()) {
1.462 + slot.obj = Array::NewExternal(ke::ZONE(), map, params[i].length(),
1.463 + params[i].buffer(), Heap::Tenure_Stack);
1.464 + if (!slot.obj)
1.465 + return false;
1.466 + } else {
1.467 + Array *array = Array::New(ke::ZONE(), map, params[i].length(), Heap::Tenure_Stack);
1.468 + if (!slot.obj)
1.469 + return false;
1.470 + memcpy(array->vec(), params[i].buffer(), params[i].length() * sizeof(char));
1.471 + }
1.472 + break;
1.473 + }
1.474 + }
1.475 + }
1.476 +
1.477 + LocalSlot result;
1.478 + if (!Interpret(ke::ZONE(), NULL, &result))
1.479 + return false;
1.480 +
1.481 + if (fun->type()->returnType()->isVoid())
1.482 + result.i32 = 0;
1.483 +
1.484 + *retval = result.i32;
1.485 + return true;
1.486 +}