Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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 | |
| 28 | #include <stdarg.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "platform.h" |
| 33 | |
| 34 | #include "sys/stat.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | |
| 40 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 41 | // figure 3-3, page 48, where the function is called clp2. |
| 42 | uint32_t RoundUpToPowerOf2(uint32_t x) { |
| 43 | x = x - 1; |
| 44 | x = x | (x >> 1); |
| 45 | x = x | (x >> 2); |
| 46 | x = x | (x >> 4); |
| 47 | x = x | (x >> 8); |
| 48 | x = x | (x >> 16); |
| 49 | return x + 1; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | byte* EncodeInt(byte* p, int x) { |
| 54 | while (x < -64 || x >= 64) { |
| 55 | *p++ = static_cast<byte>(x & 127); |
| 56 | x = ArithmeticShiftRight(x, 7); |
| 57 | } |
| 58 | // -64 <= x && x < 64 |
| 59 | *p++ = static_cast<byte>(x + 192); |
| 60 | return p; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | byte* DecodeInt(byte* p, int* x) { |
| 65 | int r = 0; |
| 66 | unsigned int s = 0; |
| 67 | byte b = *p++; |
| 68 | while (b < 128) { |
| 69 | r |= static_cast<int>(b) << s; |
| 70 | s += 7; |
| 71 | b = *p++; |
| 72 | } |
| 73 | // b >= 128 |
| 74 | *x = r | ((static_cast<int>(b) - 192) << s); |
| 75 | return p; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | byte* EncodeUnsignedIntBackward(byte* p, unsigned int x) { |
| 80 | while (x >= 128) { |
| 81 | *--p = static_cast<byte>(x & 127); |
| 82 | x = x >> 7; |
| 83 | } |
| 84 | // x < 128 |
| 85 | *--p = static_cast<byte>(x + 128); |
| 86 | return p; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | // Thomas Wang, Integer Hash Functions. |
| 91 | // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 92 | uint32_t ComputeIntegerHash(uint32_t key) { |
| 93 | uint32_t hash = key; |
| 94 | hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
| 95 | hash = hash ^ (hash >> 12); |
| 96 | hash = hash + (hash << 2); |
| 97 | hash = hash ^ (hash >> 4); |
| 98 | hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11); |
| 99 | hash = hash ^ (hash >> 16); |
| 100 | return hash; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void PrintF(const char* format, ...) { |
| 105 | va_list arguments; |
| 106 | va_start(arguments, format); |
| 107 | OS::VPrint(format, arguments); |
| 108 | va_end(arguments); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void Flush() { |
| 113 | fflush(stdout); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | char* ReadLine(const char* prompt) { |
| 118 | char* result = NULL; |
| 119 | char line_buf[256]; |
| 120 | int offset = 0; |
| 121 | bool keep_going = true; |
| 122 | fprintf(stdout, "%s", prompt); |
| 123 | fflush(stdout); |
| 124 | while (keep_going) { |
| 125 | if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { |
| 126 | // fgets got an error. Just give up. |
| 127 | if (result != NULL) { |
| 128 | DeleteArray(result); |
| 129 | } |
| 130 | return NULL; |
| 131 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 132 | int len = StrLength(line_buf); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | if (len > 1 && |
| 134 | line_buf[len - 2] == '\\' && |
| 135 | line_buf[len - 1] == '\n') { |
| 136 | // When we read a line that ends with a "\" we remove the escape and |
| 137 | // append the remainder. |
| 138 | line_buf[len - 2] = '\n'; |
| 139 | line_buf[len - 1] = 0; |
| 140 | len -= 1; |
| 141 | } else if ((len > 0) && (line_buf[len - 1] == '\n')) { |
| 142 | // Since we read a new line we are done reading the line. This |
| 143 | // will exit the loop after copying this buffer into the result. |
| 144 | keep_going = false; |
| 145 | } |
| 146 | if (result == NULL) { |
| 147 | // Allocate the initial result and make room for the terminating '\0' |
| 148 | result = NewArray<char>(len + 1); |
| 149 | } else { |
| 150 | // Allocate a new result with enough room for the new addition. |
| 151 | int new_len = offset + len + 1; |
| 152 | char* new_result = NewArray<char>(new_len); |
| 153 | // Copy the existing input into the new array and set the new |
| 154 | // array as the result. |
| 155 | memcpy(new_result, result, offset * kCharSize); |
| 156 | DeleteArray(result); |
| 157 | result = new_result; |
| 158 | } |
| 159 | // Copy the newly read line into the result. |
| 160 | memcpy(result + offset, line_buf, len * kCharSize); |
| 161 | offset += len; |
| 162 | } |
| 163 | ASSERT(result != NULL); |
| 164 | result[offset] = '\0'; |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | char* ReadCharsFromFile(const char* filename, |
| 170 | int* size, |
| 171 | int extra_space, |
| 172 | bool verbose) { |
| 173 | FILE* file = OS::FOpen(filename, "rb"); |
| 174 | if (file == NULL || fseek(file, 0, SEEK_END) != 0) { |
| 175 | if (verbose) { |
| 176 | OS::PrintError("Cannot read from file %s.\n", filename); |
| 177 | } |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | // Get the size of the file and rewind it. |
| 182 | *size = ftell(file); |
| 183 | rewind(file); |
| 184 | |
| 185 | char* result = NewArray<char>(*size + extra_space); |
| 186 | for (int i = 0; i < *size;) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 187 | int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | if (read <= 0) { |
| 189 | fclose(file); |
| 190 | DeleteArray(result); |
| 191 | return NULL; |
| 192 | } |
| 193 | i += read; |
| 194 | } |
| 195 | fclose(file); |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | byte* ReadBytes(const char* filename, int* size, bool verbose) { |
| 201 | char* chars = ReadCharsFromFile(filename, size, 0, verbose); |
| 202 | return reinterpret_cast<byte*>(chars); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | Vector<const char> ReadFile(const char* filename, |
| 207 | bool* exists, |
| 208 | bool verbose) { |
| 209 | int size; |
| 210 | char* result = ReadCharsFromFile(filename, &size, 1, verbose); |
| 211 | if (!result) { |
| 212 | *exists = false; |
| 213 | return Vector<const char>::empty(); |
| 214 | } |
| 215 | result[size] = '\0'; |
| 216 | *exists = true; |
| 217 | return Vector<const char>(result, size); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | int WriteCharsToFile(const char* str, int size, FILE* f) { |
| 222 | int total = 0; |
| 223 | while (total < size) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 224 | int write = static_cast<int>(fwrite(str, 1, size - total, f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | if (write == 0) { |
| 226 | return total; |
| 227 | } |
| 228 | total += write; |
| 229 | str += write; |
| 230 | } |
| 231 | return total; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int WriteChars(const char* filename, |
| 236 | const char* str, |
| 237 | int size, |
| 238 | bool verbose) { |
| 239 | FILE* f = OS::FOpen(filename, "wb"); |
| 240 | if (f == NULL) { |
| 241 | if (verbose) { |
| 242 | OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | int written = WriteCharsToFile(str, size, f); |
| 247 | fclose(f); |
| 248 | return written; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | int WriteBytes(const char* filename, |
| 253 | const byte* bytes, |
| 254 | int size, |
| 255 | bool verbose) { |
| 256 | const char* str = reinterpret_cast<const char*>(bytes); |
| 257 | return WriteChars(filename, str, size, verbose); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | StringBuilder::StringBuilder(int size) { |
| 262 | buffer_ = Vector<char>::New(size); |
| 263 | position_ = 0; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | void StringBuilder::AddString(const char* s) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 268 | AddSubstring(s, StrLength(s)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | void StringBuilder::AddSubstring(const char* s, int n) { |
| 273 | ASSERT(!is_finalized() && position_ + n < buffer_.length()); |
| 274 | ASSERT(static_cast<size_t>(n) <= strlen(s)); |
| 275 | memcpy(&buffer_[position_], s, n * kCharSize); |
| 276 | position_ += n; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | void StringBuilder::AddFormatted(const char* format, ...) { |
| 281 | ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 282 | va_list args; |
| 283 | va_start(args, format); |
| 284 | int n = OS::VSNPrintF(buffer_ + position_, format, args); |
| 285 | va_end(args); |
| 286 | if (n < 0 || n >= (buffer_.length() - position_)) { |
| 287 | position_ = buffer_.length(); |
| 288 | } else { |
| 289 | position_ += n; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void StringBuilder::AddPadding(char c, int count) { |
| 295 | for (int i = 0; i < count; i++) { |
| 296 | AddCharacter(c); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | char* StringBuilder::Finalize() { |
| 302 | ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 303 | buffer_[position_] = '\0'; |
| 304 | // Make sure nobody managed to add a 0-character to the |
| 305 | // buffer while building the string. |
| 306 | ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
| 307 | position_ = -1; |
| 308 | ASSERT(is_finalized()); |
| 309 | return buffer_.start(); |
| 310 | } |
| 311 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 312 | |
| 313 | int TenToThe(int exponent) { |
| 314 | ASSERT(exponent <= 9); |
| 315 | ASSERT(exponent >= 1); |
| 316 | int answer = 10; |
| 317 | for (int i = 1; i < exponent; i++) answer *= 10; |
| 318 | return answer; |
| 319 | } |
| 320 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | } } // namespace v8::internal |