blob: 26dfc42b53a89256759343e84fcb81bc586bdad3 [file] [log] [blame]
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdochb0fe1622011-05-05 13:52:32 +010028#include <stdlib.h>
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080029#include <stdarg.h>
30#include "../include/v8stdint.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010031#include "../include/v8-preparser.h"
32#include "unicode-inl.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080033
34enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 };
35
36namespace v8 {
37namespace internal {
38
39// THIS FILE IS PROOF-OF-CONCEPT ONLY.
40// The final goal is a stand-alone preparser library.
41
Ben Murdochb0fe1622011-05-05 13:52:32 +010042
43class UTF8InputStream : public v8::UnicodeInputStream {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080044 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +010045 UTF8InputStream(uint8_t* buffer, size_t length)
46 : buffer_(buffer),
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080047 offset_(0),
Ben Murdochb0fe1622011-05-05 13:52:32 +010048 pos_(0),
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080049 end_offset_(static_cast<int>(length)) { }
50
Ben Murdochb0fe1622011-05-05 13:52:32 +010051 virtual ~UTF8InputStream() { }
52
53 virtual void PushBack(int32_t ch) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080054 // Pushback assumes that the character pushed back is the
55 // one that was most recently read, and jumps back in the
56 // UTF-8 stream by the length of that character's encoding.
57 offset_ -= unibrow::Utf8::Length(ch);
58 pos_--;
59#ifdef DEBUG
Ben Murdochb0fe1622011-05-05 13:52:32 +010060 if (static_cast<unsigned>(ch) <= unibrow::Utf8::kMaxOneByteChar) {
61 if (ch != buffer_[offset_]) {
62 fprintf(stderr, "Invalid pushback: '%c'.", ch);
63 exit(1);
64 }
65 } else {
66 unsigned tmp = 0;
67 if (static_cast<unibrow::uchar>(ch) !=
68 unibrow::Utf8::CalculateValue(buffer_ + offset_,
69 end_offset_ - offset_,
70 &tmp)) {
71 fprintf(stderr, "Invalid pushback: 0x%x.", ch);
72 exit(1);
73 }
74 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080075#endif
76 }
77
Ben Murdochb0fe1622011-05-05 13:52:32 +010078 virtual int32_t Next() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080079 if (offset_ == end_offset_) return -1;
80 uint8_t first_char = buffer_[offset_];
81 if (first_char <= unibrow::Utf8::kMaxOneByteChar) {
82 pos_++;
83 offset_++;
Ben Murdochb0fe1622011-05-05 13:52:32 +010084 return static_cast<int32_t>(first_char);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080085 }
86 unibrow::uchar codepoint =
87 unibrow::Utf8::CalculateValue(buffer_ + offset_,
88 end_offset_ - offset_,
89 &offset_);
90 pos_++;
Ben Murdochb0fe1622011-05-05 13:52:32 +010091 return static_cast<int32_t>(codepoint);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080092 }
93
94 private:
95 const uint8_t* buffer_;
96 unsigned offset_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010097 unsigned pos_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080098 unsigned end_offset_;
99};
100
101
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800102// Write a number to dest in network byte order.
103void WriteUInt32(FILE* dest, uint32_t value, bool* ok) {
104 for (int i = 3; i >= 0; i--) {
105 uint8_t byte = static_cast<uint8_t>(value >> (i << 3));
106 int result = fputc(byte, dest);
107 if (result == EOF) {
108 *ok = false;
109 return;
110 }
111 }
112}
113
114// Read number from FILE* in network byte order.
115uint32_t ReadUInt32(FILE* source, bool* ok) {
116 uint32_t n = 0;
117 for (int i = 0; i < 4; i++) {
118 int c = fgetc(source);
119 if (c == EOF) {
120 *ok = false;
121 return 0;
122 }
123 n = (n << 8) + static_cast<uint32_t>(c);
124 }
125 return n;
126}
127
128
129bool ReadBuffer(FILE* source, void* buffer, size_t length) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100130 size_t actually_read = fread(buffer, 1, length, source);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800131 return (actually_read == length);
132}
133
134
Ben Murdochb0fe1622011-05-05 13:52:32 +0100135bool WriteBuffer(FILE* dest, const void* buffer, size_t length) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800136 size_t actually_written = fwrite(buffer, 1, length, dest);
137 return (actually_written == length);
138}
139
Ben Murdochb0fe1622011-05-05 13:52:32 +0100140
141template <typename T>
142class ScopedPointer {
143 public:
144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {}
145 ~ScopedPointer() { delete[] pointer_; }
146 T& operator[](int index) { return pointer_[index]; }
147 T* operator*() { return pointer_ ;}
148 private:
149 T* pointer_;
150};
151
152
153// Preparse input and output result on stdout.
154int PreParseIO(FILE* input) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800155 fprintf(stderr, "LOG: Enter parsing loop\n");
156 bool ok = true;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100157 uint32_t length = ReadUInt32(input, &ok);
158 fprintf(stderr, "LOG: Input length: %d\n", length);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800159 if (!ok) return kErrorReading;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100160 ScopedPointer<uint8_t> buffer(new uint8_t[length]);
161
162 if (!ReadBuffer(input, *buffer, length)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800163 return kErrorReading;
164 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800166
Ben Murdochb0fe1622011-05-05 13:52:32 +0100167 v8::PreParserData data =
168 v8::Preparse(&input_buffer, 64 * 1024 * sizeof(void*)); // NOLINT
169 if (data.stack_overflow()) {
170 fprintf(stderr, "LOG: Stack overflow\n");
171 fflush(stderr);
172 // Report stack overflow error/no-preparser-data.
173 WriteUInt32(stdout, 0, &ok);
174 if (!ok) return kErrorWriting;
175 return 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800176 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800177
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178 uint32_t size = data.size();
179 fprintf(stderr, "LOG: Success, data size: %u\n", size);
180 fflush(stderr);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800181 WriteUInt32(stdout, size, &ok);
182 if (!ok) return kErrorWriting;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183 if (!WriteBuffer(stdout, data.data(), size)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800184 return kErrorWriting;
185 }
186 return 0;
187}
188
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800189} } // namespace v8::internal
190
191
192int main(int argc, char* argv[]) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100193 FILE* input = stdin;
194 if (argc > 1) {
195 char* arg = argv[1];
196 input = fopen(arg, "rb");
197 if (input == NULL) return EXIT_FAILURE;
198 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800199 int status = 0;
200 do {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100201 status = v8::internal::PreParseIO(input);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800202 } while (status == 0);
203 fprintf(stderr, "EXIT: Failure %d\n", status);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800204 fflush(stderr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205 return EXIT_FAILURE;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800206}