DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (27a812186ff4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef builtin_TypeRepresentation_h
#define builtin_TypeRepresentation_h

/*
 *  Type Representations are canonical versions of user-defined type
 *  descriptors. Using the typed objects API, users are free to define
 *  new type descriptors representing C-like type definitions. Each such
 *  type descriptor has a distinct prototype and identity, even if it
 *  describes the same logical type as another type
 *  descriptor. Generally speaking, though, the only thing the compiler
 *  cares about is the binary data layout implied by a type descriptor.
 *
 *  Therefore, we link each such type descriptor to one canonical* type
 *  representation*. The type representation is itself an object, but
 *  this object is never exposed to user code (it is used by self-hosted
 *  code). Type representations are interned into a hashset in the
 *  compartment (typeReprs), meaning that you can compare two type
 *  representations for equality just using `==`. If they are equal,
 *  it means that they represent the same binary layout.
 *
 *  # Creation and canonicalization:
 *
 *  Each kind of `TypeRepresentation` object includes a `New` method
 *  that will create a canonical instance of it. So, for example, you
 *  can do `ScalarTypeRepresentation::Create(Uint8)` to get the
 *  canonical representation of uint8. The object that is returned is
 *  designed to be immutable, and the API permits only read access.
 *
 *  # Integration with TI:
 *
 *  Each TypeRepresentation has an associated Type Object. This Type
 *  Object is used as the type object for all type descriptors with this
 *  representation. The type object has an associated addendum linking
 *  to the type representation, thus allowing the jit to deduce
 *  information about type descriptors that appear in expressions.
 *
 *  # Memory management:
 *
 *  Each TypeRepresentations has an associated JSObject, called its
 *  owned object. When this object is finalized, the TypeRepresentation*
 *  will be freed (and removed from the interning table, see
 *  below). Therefore, if you reference a TypeRepresentation*, you must
 *  ensure this owner object is traced. In type objects, this is done by
 *  invoking TypeRepresentation::mark(); in binary data type
 *  descriptors, the owner object for the type is stored in
 *  `SLOT_TYPE_REPR`.
 *
 *  The canonicalization table maintains* weak references* to the
 *  TypeRepresentation* pointers. That is, the table is not traced.
 *  Instead, whenever an object is created, it is paired with its owner
 *  object, and the finalizer of the owner object removes the pointer
 *  from the table and then frees the pointer.
 *
 *  # Opacity
 *
 *  A type representation is considered "opaque" if it contains
 *  references (strings, objects, any). In those cases we have to be
 *  more limited with respect to aliasing etc to preserve portability
 *  across engines (for example, we don't want to expose sizeof(Any))
 *  and memory safety.
 */

#include "jsalloc.h"
#include "jscntxt.h"
#include "jspubtd.h"

#include "builtin/TypedObject.h"
#include "builtin/TypedObjectConstants.h"
#include "gc/Barrier.h"
#include "js/HashTable.h"

namespace js {

class TypeRepresentation;
class SizedTypeRepresentation;
class ScalarTypeRepresentation;
class ReferenceTypeRepresentation;
class X4TypeRepresentation;
class SizedArrayTypeRepresentation;
class UnsizedArrayTypeRepresentation;
class StructTypeRepresentation;

struct Class;
class StringBuffer;

struct TypeRepresentationHasher
{
    typedef TypeRepresentation* Lookup;
    static HashNumber hash(TypeRepresentation* key);
    static bool match(TypeRepresentation* key1, TypeRepresentation* key2);

  private:
    static HashNumber hashScalar(ScalarTypeRepresentation* key);
    static HashNumber hashReference(ReferenceTypeRepresentation* key);
    static HashNumber hashX4(X4TypeRepresentation* key);
    static HashNumber hashStruct(StructTypeRepresentation* key);
    static HashNumber hashUnsizedArray(UnsizedArrayTypeRepresentation* key);
    static HashNumber hashSizedArray(SizedArrayTypeRepresentation* key);

    static bool matchScalars(ScalarTypeRepresentation* key1,
                             ScalarTypeRepresentation* key2);
    static bool matchReferences(ReferenceTypeRepresentation* key1,
                                ReferenceTypeRepresentation* key2);
    static bool matchX4s(X4TypeRepresentation* key1,
                         X4TypeRepresentation* key2);
    static bool matchStructs(StructTypeRepresentation* key1,
                             StructTypeRepresentation* key2);
    static bool matchSizedArrays(SizedArrayTypeRepresentation* key1,
                                 SizedArrayTypeRepresentation* key2);
    static bool matchUnsizedArrays(UnsizedArrayTypeRepresentation* key1,
                                   UnsizedArrayTypeRepresentation* key2);
};

typedef js::HashSet<TypeRepresentation*,
                    TypeRepresentationHasher,
                    RuntimeAllocPolicy> TypeRepresentationHash;

class TypeRepresentationHelper;

class TypeRepresentation {
  protected:
    TypeRepresentation(TypeDescr::Kind kind, size_t alignment, bool opaque);

    // in order to call addToTableOrFree()
    friend class TypeRepresentationHelper;

    TypeDescr::Kind kind_;
    bool opaque_;
    size_t alignment_;

    JSObject* addToTableOrFree(JSContext* cx, TypeRepresentationHash::AddPtr& p);

  private:
    static const Class class_;
    static void obj_trace(JSTracer* trace, JSObject* object);
    static void obj_finalize(js::FreeOp* fop, JSObject* object);

    HeapPtrObject ownerObject_;
    void traceFields(JSTracer* tracer);

  public:
    TypeDescr::Kind kind() const { return kind_; }
    bool opaque() const { return opaque_; }
    bool transparent() const { return !opaque_; }
    JSObject* ownerObject() const { return ownerObject_.get(); }
    size_t alignment() const { return alignment_; }

    static bool isOwnerObject(JSObject& obj);
    static TypeRepresentation* fromOwnerObject(JSObject& obj);

    bool isSized() const {
        return TypeDescr::isSized(kind());
    }

    inline SizedTypeRepresentation* asSized();

    bool isScalar() const {
        return kind() == TypeDescr::Scalar;
    }

    inline ScalarTypeRepresentation* asScalar();

    bool isReference() const {
        return kind() == TypeDescr::Reference;
    }

    inline ReferenceTypeRepresentation* asReference();

    bool isX4() const {
        return kind() == TypeDescr::X4;
    }

    inline X4TypeRepresentation* asX4();

    bool isSizedArray() const {
        return kind() == TypeDescr::SizedArray;
    }

    inline SizedArrayTypeRepresentation* asSizedArray();

    bool isUnsizedArray() const {
        return kind() == TypeDescr::UnsizedArray;
    }

    inline UnsizedArrayTypeRepresentation* asUnsizedArray();

    bool isAnyArray() const {
        return isSizedArray() || isUnsizedArray();
    }

    bool isStruct() const {
        return kind() == TypeDescr::Struct;
    }

    inline StructTypeRepresentation* asStruct();

    void mark(JSTracer* tracer);
};

class SizedTypeRepresentation : public TypeRepresentation {
  protected:
    SizedTypeRepresentation(TypeDescr::Kind kind, bool opaque, size_t size, size_t align);

    size_t size_;

  public:
    size_t size() const { return size_; }

    // Initializes memory that contains `count` instances of this type.
    // `count` must be at least 1.
    void initInstance(const JSRuntime* rt, uint8_t* mem, size_t count);

    // Traces memory that contains `count` instances of this type.
    void traceInstance(JSTracer* trace, uint8_t* mem, size_t count);
};

class ScalarTypeRepresentation : public SizedTypeRepresentation {
  private:
    // in order to call constructor
    friend class TypeRepresentationHelper;

    const ScalarTypeDescr::Type type_;

    explicit ScalarTypeRepresentation(ScalarTypeDescr::Type type);

  public:
    ScalarTypeDescr::Type type() const {
        return type_;
    }

    const char* typeName() const {
        return ScalarTypeDescr::typeName(type());
    }

    static JSObject* Create(JSContext* cx, ScalarTypeDescr::Type type);
};

class ReferenceTypeRepresentation : public SizedTypeRepresentation {
  private:
    ReferenceTypeDescr::Type type_;

    explicit ReferenceTypeRepresentation(ReferenceTypeDescr::Type type);

  public:
    ReferenceTypeDescr::Type type() const {
        return type_;
    }

    const char* typeName() const {
        return ReferenceTypeDescr::typeName(type());
    }

    static JSObject* Create(JSContext* cx, ReferenceTypeDescr::Type type);
};

class X4TypeRepresentation : public SizedTypeRepresentation {
  private:
    // in order to call constructor
    friend class TypeRepresentationHelper;

    const X4TypeDescr::Type type_;

    explicit X4TypeRepresentation(X4TypeDescr::Type type);

  public:
    X4TypeDescr::Type type() const {
        return type_;
    }

    static JSObject* Create(JSContext* cx, X4TypeDescr::Type type);
};

class UnsizedArrayTypeRepresentation : public TypeRepresentation {
  private:
    // so TypeRepresentation can call tracing routines
    friend class TypeRepresentation;

    SizedTypeRepresentation* element_;

    UnsizedArrayTypeRepresentation(SizedTypeRepresentation* element);

    // See TypeRepresentation::traceFields()
    void traceUnsizedArrayFields(JSTracer* trace);

  public:
    SizedTypeRepresentation* element() {
        return element_;
    }

    static JSObject* Create(JSContext* cx,
                            SizedTypeRepresentation* elementTypeRepr);
};

class SizedArrayTypeRepresentation : public SizedTypeRepresentation {
  private:
    // so TypeRepresentation can call traceSizedArrayFields()
    friend class TypeRepresentation;

    SizedTypeRepresentation* element_;
    size_t length_;

    SizedArrayTypeRepresentation(SizedTypeRepresentation* element,
                                 size_t length);

    // See TypeRepresentation::traceFields()
    void traceSizedArrayFields(JSTracer* trace);

  public:
    SizedTypeRepresentation* element() {
        return element_;
    }

    size_t length() {
        return length_;
    }

    static JSObject* Create(JSContext* cx,
                            SizedTypeRepresentation* elementTypeRepr,
                            size_t length);
};

struct StructField {
    size_t index;
    HeapPtrPropertyName propertyName;
    SizedTypeRepresentation* typeRepr;
    size_t offset;

    explicit StructField(size_t index,
                         PropertyName* propertyName,
                         SizedTypeRepresentation* typeRepr,
                         size_t offset);
};

class StructTypeRepresentation : public SizedTypeRepresentation {
  private:
    // so TypeRepresentation can call traceStructFields() etc
    friend class TypeRepresentation;

    size_t fieldCount_;

    // StructTypeRepresentations are allocated with extra space to
    // store the contents of the fields array.
    StructField* fields() {
        return (StructField*) (this+1);
    }
    const StructField* fields() const {
        return (StructField*) (this+1);
    }

    StructTypeRepresentation();
    bool init(JSContext* cx,
              AutoPropertyNameVector& names,
              AutoObjectVector& typeReprOwners);

    // See TypeRepresentation::traceFields()
    void traceStructFields(JSTracer* trace);

  public:
    size_t fieldCount() const {
        return fieldCount_;
    }

    const StructField& field(size_t i) const {
        JS_ASSERT(i < fieldCount());
        return fields()[i];
    }

    const StructField* fieldNamed(jsid id) const;

    // Creates a struct type from two parallel arrays:
    // - `names`: the names of the struct type fields.
    // - `typeReprOwners`: the types of each field, which are assumed
    //   to be the owner objects for sized type representations.
    static JSObject* Create(JSContext* cx,
                            AutoPropertyNameVector& names,
                            AutoObjectVector& typeReprOwners);
};

// Definitions of the casting methods. These are pulled out of the
// main class definition because both the super- and subtypes must be
// defined for C++ to permit the static_cast.

SizedTypeRepresentation*
TypeRepresentation::asSized() {
    JS_ASSERT(isSized());
    return static_cast<SizedTypeRepresentation*>(this);
}

ScalarTypeRepresentation*
TypeRepresentation::asScalar() {
    JS_ASSERT(isScalar());
    return static_cast<ScalarTypeRepresentation*>(this);
}

ReferenceTypeRepresentation*
TypeRepresentation::asReference() {
    JS_ASSERT(isReference());
    return static_cast<ReferenceTypeRepresentation*>(this);
}

X4TypeRepresentation*
TypeRepresentation::asX4() {
    JS_ASSERT(isX4());
    return static_cast<X4TypeRepresentation*>(this);
}

SizedArrayTypeRepresentation*
TypeRepresentation::asSizedArray() {
    JS_ASSERT(isSizedArray());
    return static_cast<SizedArrayTypeRepresentation*>(this);
}

UnsizedArrayTypeRepresentation*
TypeRepresentation::asUnsizedArray() {
    JS_ASSERT(isUnsizedArray());
    return static_cast<UnsizedArrayTypeRepresentation*>(this);
}

StructTypeRepresentation*
TypeRepresentation::asStruct() {
    JS_ASSERT(isStruct());
    return static_cast<StructTypeRepresentation*>(this);
}

} // namespace js

#endif