Skip to content
Browse files

定数テーブル出力/読み込み動作確認

  • Loading branch information...
1 parent bd3d11c commit 6c13b3a342bf4917b5fc691f04fde4d4172d066f @sile committed Apr 22, 2012
View
48 plvm/include/psil/vm/#ConstantTable.hh#
@@ -1,48 +0,0 @@
-#ifndef PSIL_VM_CONSTANT_TABLE_HH
-#define PSIL_VM_CONSTANT_TABLE_HH
-
-#include "aux.hh"
-#include "Object.hh"
-#include <cstddef>
-#include <vector>
-#include <string>
-#include <sstream>
-
-/*
- [format] // 暫定. 現状シンボルのみ
- len:2byte name:len-byte *
- */
-namespace psil {
- namespace vm {
- class ConstantTable {
- public:
- static ConstantTable* parse(const char* bytes, unsigned count) {
- ConstantTable* consts = new ConstantTable;
- for(unsigned i=0; i < count; i++) {
- short length = aux::Endian::toBigShort(*reinterpret_cast<const unsigned*>(bytes相互));
- std::string name(bytes+2, length);
- consts->table.push_back(type::Symbol::intern(name));
- bytes += length;
- }
- return consts;
- }
-
- std::string show() const {
- std::ostringstream out;
- out << "[ConstantTable]" << std::endl;
- for(std::size_t i=0; i < table.size(); i++) {
- out << " " << i << ": " << table[i]->show() << std::endl;
- }
- return out.str();
- }
-
- private:
- ConstantTable() {}
-
- private:
- std::vector<type::Symbol*> table;
- };
- }
-}
-
-#endif
View
1 plvm/include/psil/vm/.#ConstantTable.hh
View
9 plvm/include/psil/vm/ByteStream.hh
@@ -11,13 +11,12 @@ namespace psil {
class ByteStream {
public:
ByteStream(const char* bytes, unsigned size) : bytes(bytes), size(size), cur(0) {}
-
bool eos() const { return cur >= size; }
// TODO: endian
uint1 readUint1() { assert(! eos()); return (uint1)bytes[cur++]; }
- uint2 readUint2() { return (uint2)(readUint1() << 8 || readUint1()); }
- uint2 readUint4() { return (uint4)(readUint2() << 16 || readUint2()); }
+ uint2 readUint2() { return (uint2)(readUint1() << 8 | readUint1()); }
+ uint2 readUint4() { return (uint4)(readUint2() << 16 | readUint2()); }
void readBytes(char* buffer, unsigned size) {
assert(cur + size < this->size);
memcpy(buffer, bytes, size);
@@ -35,6 +34,10 @@ namespace psil {
cur = pos;
return *this;
}
+
+ ByteStream subStream(unsigned start, unsigned end) const {
+ return ByteStream(bytes + start, end - start);
+ }
private:
const char* bytes;
View
14 plvm/include/psil/vm/BytecodeObject.hh
@@ -5,6 +5,7 @@
#include "ByteStream.hh"
#include <cassert>
#include <cstring>
+#include <string>
/*
[format]
@@ -20,7 +21,7 @@
- code
*/
#define MAGIC_CODE "psil"
-
+#include <iostream>
namespace psil {
namespace vm {
class BytecodeObject {
@@ -30,7 +31,7 @@ namespace psil {
char magic[4];
in.readBytes(magic, 4);
assert(strcmp(MAGIC_CODE, magic) == 0);
-
+
Header h = {in.readUint4(),
in.readUint4(),
in.readUint4(),
@@ -49,8 +50,17 @@ namespace psil {
public:
BytecodeObject(ByteStream& in)
: header(Header::read(in)), const_table(in, header.constant_count), in(in) {}
+
operator bool () const { return true; } // XXX:
+ const ConstantTable& constantTable() const { return const_table; }
+
+ ByteStream getCodeStream() {
+ return in.subStream(header.code_start, header.code_start + header.code_size);
+ }
+
+ std::string show() const;
+
private:
const Header header;
const ConstantTable const_table;
View
2 plvm/include/psil/vm/ConstantTable.hh
@@ -17,6 +17,8 @@ namespace psil {
ConstantTable(ByteStream& in, unsigned count);
std::string show() const;
+ type::Object* get(unsigned index) const { return table[index]; }
+
private:
std::vector<type::Symbol*> table;
};
View
8 plvm/include/psil/vm/Object.hh
@@ -29,16 +29,12 @@ namespace psil {
class Symbol : public Object {
private:
- Symbol(const std::string& name) : name(name), value(NULL) {} // XXX: NilObjとかをデフォルト
+ Symbol(const std::string& name) : name(name), value(Undef::make()) {}
public:
static Symbol* make(const std::string& name);
-
OBJ_TYPE getType() const { return TYPE_SYMBOL; }
-
- std::string show() const {
- return std::string("<SYMBOL ") + name + ">";
- }
+ std::string show() const { return std::string("<SYMBOL ") + name + ">"; }
public:
const std::string name;
View
1 plvm/src/bin/plvm.cc
@@ -32,6 +32,7 @@ int main(int argc, char** argv) {
std::cerr << "ERROR: can't parse " << filepath << std::endl;
return 1;
}
+ std::cout << bo.show() << std::endl << std::endl;
if(exec.execute(bo)) {
std::cerr << "ERROR: can't execute " << filepath << std::endl;
View
15 plvm/src/psil/vm/BytecodeObject.cc
@@ -1,3 +1,18 @@
#include "psil/vm/BytecodeObject.hh"
+#include <sstream>
using namespace psil::vm;
+
+std::string BytecodeObject::show() const {
+ std::ostringstream out;
+ out << "[BytecodeObject]" << std::endl
+ << " header:" << std::endl
+ << " - version: " << header.version << std::endl
+ << " - constant_start: " << header.constant_start << std::endl
+ << " - constant_count: " << header.constant_count << std::endl
+ << " - code_start: " << header.code_start << std::endl
+ << " - code_size: " << header.code_size << std::endl
+ << " constant-table: " << std::endl
+ << const_table.show() << std::endl;
+ return out.str();
+}
View
2 plvm/src/psil/vm/ConstantTable.cc
@@ -9,7 +9,7 @@ ConstantTable::ConstantTable(ByteStream& in, unsigned count) {
uint2 length = in.readUint2();
std::string name;
in.readString(name, length);
- table.push_back(type::Symbol::intern(name));
+ table.push_back(type::Symbol::make(name));
}
}
View
17 vm/bytecode/writer.lisp
@@ -6,7 +6,9 @@
;; header
(write-int out (header-version header))
+ (write-int out 0) ; constant-start: dummy
(write-int out (header-symbol-count header))
+ (write-int out 0) ; code-start: dummy
(write-int out (header-code-size header))
t
@@ -20,17 +22,26 @@
(loop FOR (index . symbol) IN (sort acc #'< :key #'car)
FOR s = (sb-ext:string-to-octets (symbol-name symbol))
DO
- (write-byte 4 out) ; symbol
+ ;; (write-byte 4 out) ; symbol
(write-short out (length s))
(write-sequence s out))))
(defun write-bc (out codes &key (symbol-table (make-hash-table)))
(write-header out (header :symbol-count (hash-table-count symbol-table)
:code-size (length codes)))
- (write-symbol-table out symbol-table)
- (write-sequence codes out)
+ (let ((constant-start (file-position out)))
+ (write-symbol-table out symbol-table)
+ (let ((code-start (file-position out)))
+ (write-sequence codes out)
+
+ ;; constant-start, code-start
+ (file-position out (+ 4 4))
+ (write-int out constant-start)
+ (file-position out (+ 4 4 4 4))
+ (write-int out code-start)))
t)
+
(defun write-bc-to-file (filepath codes &key (symbol-table (make-hash-table)))
(with-open-file (out filepath
:direction :output

0 comments on commit 6c13b3a

Please sign in to comment.
Something went wrong with that request. Please try again.