[vmkit-commits] [vmkit] r121631 - in /vmkit/branches/py-vm/lib/p3/VMCore: P3.cpp P3.h P3Error.h P3Extractor.cpp P3Extractor.h P3Reader.cpp P3Reader.h

Gael Thomas gael.thomas at lip6.fr
Sun Dec 12 01:20:46 PST 2010


Author: gthomas
Date: Sun Dec 12 03:20:46 2010
New Revision: 121631

URL: http://llvm.org/viewvc/llvm-project?rev=121631&view=rev
Log:
read a pyc file

Added:
    vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp
    vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h
    vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.cpp
    vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.h
Modified:
    vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp
    vmkit/branches/py-vm/lib/p3/VMCore/P3.h
    vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h

Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp?rev=121631&r1=121630&r2=121631&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp Sun Dec 12 03:20:46 2010
@@ -1,5 +1,7 @@
 #include "P3.h"
 #include "P3Error.h"
+#include "P3Reader.h"
+#include "P3Extractor.h"
 
 using namespace p3;
 
@@ -13,6 +15,26 @@
 size_t P3::getObjectSize(mvm::gc* object) { NI(); }
 const char* P3::getObjectTypeName(mvm::gc* object) { NI(); }
 
+void P3::runFile(const char* fileName) {
+	mvm::BumpPtrAllocator allocator;
+	P3ByteCode* bc = P3Reader::openFile(allocator, fileName); 
+	if(!bc)
+		fatal("unable to open: %s", fileName);
+	P3Reader reader(bc);
+
+	uint16 ver = reader.readU2();
+	printf("ver: 0x%x\n", ver);
+	uint16 magic = reader.readU2();
+	printf("magic: 0x%x\n", magic);
+	reader.readU4(); // last modification
+
+	P3Extractor extractor(&reader); 
+	extractor.readObject();
+}
+
 void P3::runApplicationImpl(int argc, char** argv) {
-	NI();
+	if(argc < 2)
+		fatal("usage: %s filename", argv[0]);
+
+	runFile(argv[1]);
 }

Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3.h?rev=121631&r1=121630&r2=121631&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3.h (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3.h Sun Dec 12 03:20:46 2010
@@ -5,6 +5,8 @@
 
 namespace p3 {
 
+class P3Reader;
+
 class P3 : public mvm::VirtualMachine {
 public:
   /// P3 - default constructor
@@ -45,6 +47,11 @@
 	/// runApplicationImpl - code executed after a runApplication in a vmkit thread
 	///
 	virtual void runApplicationImpl(int argc, char** argv);
+
+public:
+	/// runFile - execute the file fileName
+	///
+	void runFile(const char* fileName);
 };
 
 } // namespace p3

Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h?rev=121631&r1=121630&r2=121631&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h Sun Dec 12 03:20:46 2010
@@ -4,4 +4,7 @@
 #define NI() \
 	do { fprintf(stderr, "%s is not implemented at %s::%d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__); abort(); } while(0)
 
+#define fatal(...) \
+	do { fprintf(stderr, "FATAL: "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); abort(); } while(0)
+
 #endif

Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp?rev=121631&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp Sun Dec 12 03:20:46 2010
@@ -0,0 +1,87 @@
+#include "P3Extractor.h"
+#include "P3Reader.h"
+#include "P3Error.h"
+
+using namespace p3;
+
+#define TYPE_NULL               '0'
+#define TYPE_NONE               'N'
+#define TYPE_FALSE              'F'
+#define TYPE_TRUE               'T'
+#define TYPE_STOPITER           'S'
+#define TYPE_ELLIPSIS           '.'
+#define TYPE_INT                'i'
+#define TYPE_INT64              'I'
+#define TYPE_FLOAT              'f'
+#define TYPE_BINARY_FLOAT       'g'
+#define TYPE_COMPLEX            'x'
+#define TYPE_BINARY_COMPLEX     'y'
+#define TYPE_LONG               'l'
+#define TYPE_STRING             's'
+#define TYPE_INTERNED           't'
+#define TYPE_STRINGREF          'R'
+#define TYPE_TUPLE              '('
+#define TYPE_LIST               '['
+#define TYPE_DICT               '{'
+#define TYPE_CODE               'c'
+#define TYPE_UNICODE            'u'
+#define TYPE_UNKNOWN            '?'
+#define TYPE_SET                '<'
+#define TYPE_FROZENSET          '>'
+
+void P3Extractor::readObject() {
+	uint8 type = reader->readU1();
+
+	printf("reading object with type: %d (%c)\n", type, type);
+
+	switch(type) {
+		case TYPE_NONE:
+			break;
+
+		case TYPE_TUPLE:
+			{
+				uint32 length = reader->readU4();
+				printf("  tuple length: %d\n", length);
+				for(uint32 i=0; i<length; i++)
+					readObject();
+			}
+			break;
+
+		case TYPE_INTERNED:
+		case TYPE_STRING:
+			{
+				uint32 length = reader->readU4();
+				printf("  string length: %d\n", length);
+				if(length > INT_MAX)
+					fatal("wrong length for string");
+				for(uint32 i=0; i<length; i++) {
+					uint8 c = reader->readU1();
+					printf("    %-3d (%c) ", c, c);
+				}
+				printf("\n");
+			}
+			break;
+
+		case TYPE_CODE:
+			{
+				uint32 nargs   = reader->readU4();
+				uint32 nlocals = reader->readU4();
+				uint32 ssize   = reader->readU4();
+				uint32 flags   = reader->readU4();
+				readObject();      // code
+				readObject();      // const
+				readObject();      // names
+				readObject();      // varnames
+				readObject();      // freevars
+				readObject();      // cellvars
+				readObject();      // filename
+				readObject();      // name
+				reader->readU4();  // line num
+				readObject();      // lnotab
+			}
+			break;
+
+		default:
+			fatal("wrong type info: %d ('%c')", type, type);
+	}
+}

Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h?rev=121631&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h Sun Dec 12 03:20:46 2010
@@ -0,0 +1,22 @@
+#ifndef _P3_EXTRACTOR_H_
+#define _P3_EXTRACTOR_H_
+
+namespace p3 {
+
+class P3Reader;
+
+class P3Extractor {
+	/// reader - the reader
+	P3Reader* reader;
+
+public:
+	P3Extractor(P3Reader* r) { this->reader = r; }
+
+	/// readObject - unmarshal an object
+	///
+	void readObject();
+};
+
+} // namespace p3
+
+#endif

Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.cpp?rev=121631&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.cpp (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.cpp Sun Dec 12 03:20:46 2010
@@ -0,0 +1,55 @@
+//===--------------- P3Reader.cpp - Open and read files ---------------------===//
+//
+//                            The VMKit project
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <cstdio>
+#include <cstring>
+
+#include "types.h"
+
+#include "P3Reader.h"
+
+using namespace p3;
+
+const int P3Reader::SeekSet = SEEK_SET;
+const int P3Reader::SeekCur = SEEK_CUR;
+const int P3Reader::SeekEnd = SEEK_END;
+
+P3ByteCode* P3Reader::openFile(mvm::BumpPtrAllocator& allocator, const char* path) {
+  P3ByteCode* res = NULL;
+  FILE* fp = fopen(path, "r");
+  if (fp != 0) {
+    fseek(fp, 0, SeekEnd);
+    size_t nbb = ftell(fp);
+    fseek(fp, 0, SeekSet);
+    res = new(allocator, nbb) P3ByteCode(nbb);
+    if (fread(res->elements, nbb, 1, fp) == 0) {
+      fprintf(stderr, "fread error\n");
+      abort();  
+    }
+    fclose(fp);
+  }
+  return res;
+}
+
+void P3Reader::seek(uint32 pos, int from) {
+  uint32 n = 0;
+  uint32 start = min;
+  uint32 end = max;
+  
+  if (from == SeekCur) n = cursor + pos;
+  else if (from == SeekSet) n = start + pos;
+  else if (from == SeekEnd) n = end + pos;
+  
+
+  assert(n >= start && n <= end && "out of range");
+
+  cursor = n;
+}
+
+

Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.h?rev=121631&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.h (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Reader.h Sun Dec 12 03:20:46 2010
@@ -0,0 +1,130 @@
+#ifndef _P3_READER_H_
+#define _P3_READER_H_
+
+#include "mvm/Allocator.h"
+
+namespace p3 {
+
+class P3ByteCode {
+ public:
+  P3ByteCode(int l) {
+    size = l;
+  }
+
+  void* operator new(size_t sz, mvm::BumpPtrAllocator& allocator, int n) {
+    return allocator.Allocate(sizeof(uint32_t) + n * sizeof(uint8_t),
+                              "Class bytes");
+  }
+
+  uint32_t size;
+  uint8_t elements[1];
+};
+
+class P3Reader {
+public:
+  // bytes - Pointer to a reference array. The array is not manipulated directly
+  // in order to support copying GC.
+  P3ByteCode* bytes;
+  uint32 min;
+  uint32 cursor;
+  uint32 max;
+
+  P3Reader(P3ByteCode* array, uint32 start = 0, uint32 end = 0) {
+    if (!end) end = array->size;
+    this->bytes = array;
+    this->cursor = start;
+    this->min = start;
+    this->max = start + end;
+  }
+
+  P3Reader(P3Reader& r, uint32 nbb) {
+    bytes = r.bytes;
+    cursor = r.cursor;
+    min = r.min;
+    max = min + nbb;
+  }
+
+  static const int SeekSet;
+  static const int SeekCur;
+  static const int SeekEnd;
+
+  static P3ByteCode* openFile(mvm::BumpPtrAllocator& allocator, const char* path);
+  
+  uint8 readU1() {
+    ++cursor;
+    return bytes->elements[cursor - 1];
+  }
+  
+  sint8 readS1() {
+    ++cursor;
+    return bytes->elements[cursor - 1];
+  }
+  
+  uint16 readU2() {
+    uint16 tmp = ((uint16)(readU1()));
+    return tmp | ((uint16)(readU1())) << 8;
+  }
+  
+  sint16 readS2() {
+    sint16 tmp = ((sint16)(readS1()));
+    return tmp | ((sint16)(readU1())) << 8;
+  }
+  
+  uint32 readU4() {
+    uint32 tmp = ((uint32)(readU2()));
+    return tmp | ((uint32)(readU2())) << 16;
+  }
+  
+  sint32 readS4() {
+    sint32 tmp = ((sint32)(readS2()));
+    return tmp | ((sint32)(readU2())) << 16;
+  }
+
+  uint64 readU8() {
+    uint64 tmp = ((uint64)(readU4()));
+    return tmp | ((uint64)(readU4())) << 32;
+  }
+  
+  sint64 readS8() {
+    sint64 tmp = ((sint64)(readS4()));
+    return tmp | ((sint64)(readU4())) << 32;
+  }
+
+  static double readF8(int first, int second) {
+    int values[2];
+    double res[1];
+#if defined(__PPC__)
+    values[0] = second;
+    values[1] = first;
+#else
+    values[0] = first;
+    values[1] = second;
+#endif
+    memcpy(res, values, 8); 
+    return res[0];
+  }
+
+//   static sint64 readS8(int first, int second) {
+//     int values[2];
+//     sint64 res[1];
+// #if defined(__PPC__)
+//     values[0] = second;
+//     values[1] = first;
+// #else
+//     values[0] = first;
+//     values[1] = second;
+// #endif
+//     memcpy(res, values, 8); 
+//     return res[0];
+//   }
+  
+  unsigned int tell() {
+    return cursor - min;
+  }
+  
+  void seek(uint32 pos, int from);
+};
+
+} // namespace p3
+
+#endif





More information about the vmkit-commits mailing list