| Index: src/assembler-re2k.cc
|
| ===================================================================
|
| --- src/assembler-re2k.cc (revision 0)
|
| +++ src/assembler-re2k.cc (revision 0)
|
| @@ -0,0 +1,258 @@
|
| +// Copyright 2008 the V8 project authors. All rights reserved.
|
| +// Redistribution and use in source and binary forms, with or without
|
| +// modification, are permitted provided that the following conditions are
|
| +// met:
|
| +//
|
| +// * Redistributions of source code must retain the above copyright
|
| +// notice, this list of conditions and the following disclaimer.
|
| +// * Redistributions in binary form must reproduce the above
|
| +// copyright notice, this list of conditions and the following
|
| +// disclaimer in the documentation and/or other materials provided
|
| +// with the distribution.
|
| +// * Neither the name of Google Inc. nor the names of its
|
| +// contributors may be used to endorse or promote products derived
|
| +// from this software without specific prior written permission.
|
| +//
|
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| +
|
| +// A light-weight assembler for the Regexp2000 byte code.
|
| +
|
| +
|
| +#include "v8.h"
|
| +#include "ast.h"
|
| +#include "bytecodes-re2k.h"
|
| +#include "regexp-codegen.h"
|
| +#include "assembler-re2k.h"
|
| +
|
| +#include "assembler-re2k-inl.h"
|
| +
|
| +
|
| +namespace v8 { namespace internal {
|
| +
|
| +
|
| +static inline int Load32(const byte* pc) {
|
| +#ifdef CAN_READ_UNALIGNED
|
| + return *reinterpret_cast<const int*>(pc);
|
| +#else
|
| + union {
|
| + int word;
|
| + byte bytes[4];
|
| + } u;
|
| + u.bytes[0] = pc[0];
|
| + u.bytes[1] = pc[1];
|
| + u.bytes[2] = pc[2];
|
| + u.bytes[3] = pc[3];
|
| + return u.word;
|
| +#endif
|
| +}
|
| +
|
| +
|
| +Re2kAssembler::Re2kAssembler(byte* buffer, int buffer_size)
|
| + : buffer_(buffer),
|
| + buffer_size_(buffer_size),
|
| + limit_(buffer + buffer_size - 3),
|
| + pc_(buffer),
|
| + own_buffer_(false) {
|
| +}
|
| +
|
| +
|
| +Re2kAssembler::~Re2kAssembler() {
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PushCurrentPosition(int cp_offset) {
|
| + ASSERT(cp_offset >= 0);
|
| + if (cp_offset < 256) {
|
| + Emit(BC_PUSH_CP);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_PUSH_CP_WIDE);
|
| + Emit32(cp_offset);
|
| + }
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PushBacktrack(Label* l) {
|
| + Emit(BC_PUSH_BT);
|
| + EmitOrLink(l);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PushCapture(int index) {
|
| + ASSERT(index >= 0);
|
| + Emit(BC_PUSH_CAPTURE);
|
| + Emit(index);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::SetCapture(int index, int cp_offset) {
|
| + ASSERT(cp_offset >= 0);
|
| + ASSERT(index >= 0);
|
| + if (cp_offset < 256) {
|
| + Emit(BC_SET_CAPTURE);
|
| + Emit(index);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_SET_CAPTURE_WIDE);
|
| + Emit(index);
|
| + Emit32(cp_offset);
|
| + }
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PopCurrentPosition() {
|
| + Emit(BC_POP_CP);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PopBacktrack() {
|
| + Emit(BC_POP_BT);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::PopCapture(int index) {
|
| + Emit(BC_POP_CAPTURE);
|
| + Emit(index);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::Fail() {
|
| + Emit(BC_FAIL);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::FailIfWithin(int distance_from_end) {
|
| + if (distance_from_end < 256) {
|
| + Emit(BC_FAIL_IF_WITHIN);
|
| + Emit(distance_from_end);
|
| + } else {
|
| + Emit(BC_FAIL_IF_WITHIN_WIDE);
|
| + Emit32(distance_from_end);
|
| + }
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::Succeed() {
|
| + Emit(BC_SUCCEED);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::Bind(Label* l) {
|
| + int bind_to = pc_ - buffer_;
|
| + ASSERT(!l->is_bound());
|
| + if (l->is_linked()) {
|
| + int pos = l->pos();
|
| + while (pos != 0) {
|
| + int fixup = pos;
|
| + pos = Load32(buffer_ + fixup);
|
| + Store32(bind_to, buffer_ + fixup);
|
| + }
|
| + }
|
| + l->bind_to(bind_to);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::AdvanceCP(int cp_offset) {
|
| + if (cp_offset < 256) {
|
| + Emit(BC_ADVANCE_CP);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_ADVANCE_CP_WIDE);
|
| + Emit32(cp_offset);
|
| + }
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::GoTo(Label* l) {
|
| + Emit(BC_GOTO);
|
| + EmitOrLink(l);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::LoadCurrentChar(int cp_offset) {
|
| + if (cp_offset < 256) {
|
| + Emit(BC_LOAD_CURRENT_CHAR);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_LOAD_CURRENT_CHAR_WIDE);
|
| + Emit32(cp_offset);
|
| + }
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckChar(uc16 c, Label* on_mismatch) {
|
| + Emit(BC_CHECK_CHAR);
|
| + Emit16(c);
|
| + EmitOrLink(on_mismatch);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckNotChar(uc16 c, Label* on_match) {
|
| + Emit(BC_CHECK_NOT_CHAR);
|
| + Emit16(c);
|
| + EmitOrLink(on_match);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckRange(uc16 start, uc16 end, Label* on_mismatch) {
|
| + Emit(BC_CHECK_RANGE);
|
| + Emit16(start);
|
| + Emit16(end);
|
| + EmitOrLink(on_mismatch);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckNotRange(uc16 start, uc16 end, Label* on_match) {
|
| + Emit(BC_CHECK_NOT_RANGE);
|
| + Emit16(start);
|
| + Emit16(end);
|
| + EmitOrLink(on_match);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckBackref(int capture_index, Label* on_mismatch, int cp_offset) {
|
| + if (cp_offset < 256) {
|
| + Emit(BC_CHECK_BACKREF);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_CHECK_BACKREF_WIDE);
|
| + Emit32(cp_offset);
|
| + }
|
| + Emit(capture_index);
|
| + EmitOrLink(on_mismatch);
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::CheckNotBackref(int capture_index, Label* on_match, int cp_offset) {
|
| + if (cp_offset < 256) {
|
| + Emit(BC_CHECK_NOT_BACKREF);
|
| + Emit(cp_offset);
|
| + } else {
|
| + Emit(BC_CHECK_NOT_BACKREF_WIDE);
|
| + Emit32(cp_offset);
|
| + }
|
| + Emit(capture_index);
|
| + EmitOrLink(on_match);
|
| +}
|
| +
|
| +
|
| +int Re2kAssembler::length() {
|
| + return pc_ - buffer_;
|
| +}
|
| +
|
| +
|
| +void Re2kAssembler::Copy(Address a) {
|
| + memcpy(a, buffer_, length());
|
| +}
|
| +
|
| +} } // namespace v8::internal
|
|
|