1// Copyright 2010 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "include/v8stdint.h"
6#include "src/base/logging.h"
7#include "src/compiler.h"
8#include "src/globals.h"
9#include "src/hashmap.h"
10#include "src/preparse-data.h"
11#include "src/preparse-data-format.h"
12
13namespace v8 {
14namespace internal {
15
16
17CompleteParserRecorder::CompleteParserRecorder()
18    : function_store_(0) {
19  preamble_[PreparseDataConstants::kMagicOffset] =
20      PreparseDataConstants::kMagicNumber;
21  preamble_[PreparseDataConstants::kVersionOffset] =
22      PreparseDataConstants::kCurrentVersion;
23  preamble_[PreparseDataConstants::kHasErrorOffset] = false;
24  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
25  preamble_[PreparseDataConstants::kSizeOffset] = 0;
26  DCHECK_EQ(5, PreparseDataConstants::kHeaderSize);
27#ifdef DEBUG
28  prev_start_ = -1;
29#endif
30}
31
32
33void CompleteParserRecorder::LogMessage(int start_pos,
34                                        int end_pos,
35                                        const char* message,
36                                        const char* arg_opt,
37                                        bool is_reference_error) {
38  if (HasError()) return;
39  preamble_[PreparseDataConstants::kHasErrorOffset] = true;
40  function_store_.Reset();
41  STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
42  function_store_.Add(start_pos);
43  STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
44  function_store_.Add(end_pos);
45  STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
46  function_store_.Add((arg_opt == NULL) ? 0 : 1);
47  STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3);
48  function_store_.Add(is_reference_error ? 1 : 0);
49  STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4);
50  WriteString(CStrVector(message));
51  if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
52}
53
54
55void CompleteParserRecorder::WriteString(Vector<const char> str) {
56  function_store_.Add(str.length());
57  for (int i = 0; i < str.length(); i++) {
58    function_store_.Add(str[i]);
59  }
60}
61
62
63ScriptData* CompleteParserRecorder::GetScriptData() {
64  int function_size = function_store_.size();
65  int total_size = PreparseDataConstants::kHeaderSize + function_size;
66  unsigned* data = NewArray<unsigned>(total_size);
67  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
68  MemCopy(data, preamble_, sizeof(preamble_));
69  if (function_size > 0) {
70    function_store_.WriteTo(Vector<unsigned>(
71        data + PreparseDataConstants::kHeaderSize, function_size));
72  }
73  DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
74  ScriptData* result = new ScriptData(reinterpret_cast<byte*>(data),
75                                      total_size * sizeof(unsigned));
76  result->AcquireDataOwnership();
77  return result;
78}
79
80
81} }  // namespace v8::internal.
82