| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
# This script generates jit/CacheIROpsGenerated.h from CacheIROps.yaml |
| 6 |
|
| 7 |
import io |
| 8 |
import os |
| 9 |
import os.path |
| 10 |
|
| 11 |
import buildconfig |
| 12 |
import yaml |
| 13 |
from mozbuild.preprocessor import Preprocessor |
| 14 |
|
| 15 |
HEADER_TEMPLATE = """\ |
| 16 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 17 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 18 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 19 |
|
| 20 |
#ifndef %(includeguard)s |
| 21 |
#define %(includeguard)s |
| 22 |
|
| 23 |
/* This file is generated by jit/GenerateCacheIRFiles.py. Do not edit! */ |
| 24 |
|
| 25 |
%(contents)s |
| 26 |
|
| 27 |
#endif // %(includeguard)s |
| 28 |
""" |
| 29 |
|
| 30 |
|
| 31 |
def generate_header(c_out, includeguard, contents): |
| 32 |
c_out.write( |
| 33 |
HEADER_TEMPLATE |
| 34 |
% { |
| 35 |
"includeguard": includeguard, |
| 36 |
"contents": contents, |
| 37 |
} |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
def load_yaml(yaml_path): |
| 42 |
# First invoke preprocessor.py so that we can use #ifdef JS_SIMULATOR in |
| 43 |
# the YAML file. |
| 44 |
pp = Preprocessor() |
| 45 |
pp.context.update(buildconfig.defines["ALLDEFINES"]) |
| 46 |
pp.out = io.StringIO() |
| 47 |
pp.do_filter("substitution") |
| 48 |
pp.do_include(yaml_path) |
| 49 |
contents = pp.out.getvalue() |
| 50 |
return yaml.safe_load(contents) |
| 51 |
|
| 52 |
|
| 53 |
# Information for generating CacheIRWriter code for a single argument. Tuple |
| 54 |
# stores the C++ argument type and the CacheIRWriter method to call. |
| 55 |
arg_writer_info = { |
| 56 |
"ValId": ("ValOperandId", "writeOperandId"), |
| 57 |
"ObjId": ("ObjOperandId", "writeOperandId"), |
| 58 |
"StringId": ("StringOperandId", "writeOperandId"), |
| 59 |
"SymbolId": ("SymbolOperandId", "writeOperandId"), |
| 60 |
"BooleanId": ("BooleanOperandId", "writeOperandId"), |
| 61 |
"Int32Id": ("Int32OperandId", "writeOperandId"), |
| 62 |
"NumberId": ("NumberOperandId", "writeOperandId"), |
| 63 |
"BigIntId": ("BigIntOperandId", "writeOperandId"), |
| 64 |
"ValueTagId": ("ValueTagOperandId", "writeOperandId"), |
| 65 |
"IntPtrId": ("IntPtrOperandId", "writeOperandId"), |
| 66 |
"RawId": ("OperandId", "writeOperandId"), |
| 67 |
"ShapeField": ("Shape*", "writeShapeField"), |
| 68 |
"WeakShapeField": ("Shape*", "writeWeakShapeField"), |
| 69 |
"WeakGetterSetterField": ("GetterSetter*", "writeWeakGetterSetterField"), |
| 70 |
"ObjectField": ("JSObject*", "writeObjectField"), |
| 71 |
"WeakObjectField": ("JSObject*", "writeWeakObjectField"), |
| 72 |
"StringField": ("JSString*", "writeStringField"), |
| 73 |
"AtomField": ("JSAtom*", "writeStringField"), |
| 74 |
"SymbolField": ("JS::Symbol*", "writeSymbolField"), |
| 75 |
"WeakBaseScriptField": ("BaseScript*", "writeWeakBaseScriptField"), |
| 76 |
"JitCodeField": ("JitCode*", "writeJitCodeField"), |
| 77 |
"RawInt32Field": ("uint32_t", "writeRawInt32Field"), |
| 78 |
"RawPointerField": ("const void*", "writeRawPointerField"), |
| 79 |
"ICScriptField": ("const ICScript*", "writeICScriptField"), |
| 80 |
"IdField": ("jsid", "writeIdField"), |
| 81 |
"ValueField": ("const Value&", "writeValueField"), |
| 82 |
"RawInt64Field": ("uint64_t", "writeRawInt64Field"), |
| 83 |
"DoubleField": ("double", "writeDoubleField"), |
| 84 |
"AllocSiteField": ("gc::AllocSite*", "writeAllocSiteField"), |
| 85 |
"JSOpImm": ("JSOp", "writeJSOpImm"), |
| 86 |
"JSTypeImm": ("JSType", "writeJSTypeImm"), |
| 87 |
"TypeofEqOperandImm": ("TypeofEqOperand", "writeTypeofEqOperandImm"), |
| 88 |
"BoolImm": ("bool", "writeBoolImm"), |
| 89 |
"ByteImm": ("uint32_t", "writeByteImm"), # uint32_t to enable fits-in-byte asserts. |
| 90 |
"GuardClassKindImm": ("GuardClassKind", "writeGuardClassKindImm"), |
| 91 |
"ArrayBufferViewKindImm": ("ArrayBufferViewKind", "writeArrayBufferViewKindImm"), |
| 92 |
"ValueTypeImm": ("ValueType", "writeValueTypeImm"), |
| 93 |
"JSWhyMagicImm": ("JSWhyMagic", "writeJSWhyMagicImm"), |
| 94 |
"CallFlagsImm": ("CallFlags", "writeCallFlagsImm"), |
| 95 |
"ScalarTypeImm": ("Scalar::Type", "writeScalarTypeImm"), |
| 96 |
"UnaryMathFunctionImm": ("UnaryMathFunction", "writeUnaryMathFunctionImm"), |
| 97 |
"WasmValTypeImm": ("wasm::ValType::Kind", "writeWasmValTypeImm"), |
| 98 |
"Int32Imm": ("int32_t", "writeInt32Imm"), |
| 99 |
"UInt32Imm": ("uint32_t", "writeUInt32Imm"), |
| 100 |
"JSNativeImm": ("JSNative", "writeJSNativeImm"), |
| 101 |
"StaticStringImm": ("const char*", "writeStaticStringImm"), |
| 102 |
"AllocKindImm": ("gc::AllocKind", "writeAllocKindImm"), |
| 103 |
"CompletionKindImm": ("CompletionKind", "writeCompletionKindImm"), |
| 104 |
"RealmFuseIndexImm": ("RealmFuses::FuseIndex", "writeRealmFuseIndexImm"), |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
def gen_writer_method(name, args, custom_writer): |
| 109 |
"""Generates a CacheIRWRiter method for a single opcode.""" |
| 110 |
|
| 111 |
# Generate a single method that writes the opcode and each argument. |
| 112 |
# For example: |
| 113 |
# |
| 114 |
# void guardShape(ObjOperandId obj, Shape* shape) { |
| 115 |
# writeOp(CacheOp::GuardShape); |
| 116 |
# writeOperandId(obj); |
| 117 |
# writeShapeField(shape); |
| 118 |
# assertLengthMatches(); |
| 119 |
# } |
| 120 |
# |
| 121 |
# The assertLengthMatches() call is to assert the information in the |
| 122 |
# arg_length dictionary below matches what's written. |
| 123 |
|
| 124 |
# Method names start with a lowercase letter. |
| 125 |
method_name = name[0].lower() + name[1:] |
| 126 |
if custom_writer: |
| 127 |
method_name += "_" |
| 128 |
|
| 129 |
method_args = [] |
| 130 |
ret_type = "void" |
| 131 |
args_code = "" |
| 132 |
if args: |
| 133 |
for arg_name, arg_type in args.items(): |
| 134 |
cpp_type, write_method = arg_writer_info[arg_type] |
| 135 |
if arg_name == "result": |
| 136 |
ret_type = cpp_type |
| 137 |
args_code += f" {cpp_type} result(newOperandId());\\\n" |
| 138 |
args_code += " writeOperandId(result);\\\n" |
| 139 |
else: |
| 140 |
method_args.append(f"{cpp_type} {arg_name}") |
| 141 |
args_code += f" {write_method}({arg_name});\\\n" |
| 142 |
|
| 143 |
code = "" |
| 144 |
if custom_writer: |
| 145 |
code += "private:\\\n" |
| 146 |
code += "{} {}({}) {{\\\n".format(ret_type, method_name, ", ".join(method_args)) |
| 147 |
code += f" writeOp(CacheOp::{name});\\\n" |
| 148 |
code += args_code |
| 149 |
code += " assertLengthMatches();\\\n" |
| 150 |
if ret_type != "void": |
| 151 |
code += " return result;\\\n" |
| 152 |
code += "}" |
| 153 |
if custom_writer: |
| 154 |
code += "\\\npublic:" |
| 155 |
return code |
| 156 |
|
| 157 |
|
| 158 |
# Information for generating code using CacheIRReader for a single argument. |
| 159 |
# Tuple stores the C++ type, the suffix used for arguments/variables of this |
| 160 |
# type, and the expression to read this type from CacheIRReader. |
| 161 |
arg_reader_info = { |
| 162 |
"ValId": ("ValOperandId", "Id", "reader.valOperandId()"), |
| 163 |
"ObjId": ("ObjOperandId", "Id", "reader.objOperandId()"), |
| 164 |
"StringId": ("StringOperandId", "Id", "reader.stringOperandId()"), |
| 165 |
"SymbolId": ("SymbolOperandId", "Id", "reader.symbolOperandId()"), |
| 166 |
"BooleanId": ("BooleanOperandId", "Id", "reader.booleanOperandId()"), |
| 167 |
"Int32Id": ("Int32OperandId", "Id", "reader.int32OperandId()"), |
| 168 |
"NumberId": ("NumberOperandId", "Id", "reader.numberOperandId()"), |
| 169 |
"BigIntId": ("BigIntOperandId", "Id", "reader.bigIntOperandId()"), |
| 170 |
"ValueTagId": ("ValueTagOperandId", "Id", "reader.valueTagOperandId()"), |
| 171 |
"IntPtrId": ("IntPtrOperandId", "Id", "reader.intPtrOperandId()"), |
| 172 |
"RawId": ("uint32_t", "Id", "reader.rawOperandId()"), |
| 173 |
"ShapeField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 174 |
"WeakShapeField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 175 |
"WeakGetterSetterField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 176 |
"ObjectField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 177 |
"WeakObjectField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 178 |
"StringField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 179 |
"AtomField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 180 |
"SymbolField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 181 |
"WeakBaseScriptField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 182 |
"JitCodeField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 183 |
"RawInt32Field": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 184 |
"RawPointerField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 185 |
"ICScriptField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 186 |
"IdField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 187 |
"ValueField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 188 |
"RawInt64Field": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 189 |
"DoubleField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 190 |
"AllocSiteField": ("uint32_t", "Offset", "reader.stubOffset()"), |
| 191 |
"JSOpImm": ("JSOp", "", "reader.jsop()"), |
| 192 |
"JSTypeImm": ("JSType", "", "reader.jstype()"), |
| 193 |
"TypeofEqOperandImm": ("TypeofEqOperand", "", "reader.typeofEqOperand()"), |
| 194 |
"BoolImm": ("bool", "", "reader.readBool()"), |
| 195 |
"ByteImm": ("uint8_t", "", "reader.readByte()"), |
| 196 |
"GuardClassKindImm": ("GuardClassKind", "", "reader.guardClassKind()"), |
| 197 |
"ArrayBufferViewKindImm": ( |
| 198 |
"ArrayBufferViewKind", |
| 199 |
"", |
| 200 |
"reader.arrayBufferViewKind()", |
| 201 |
), |
| 202 |
"ValueTypeImm": ("ValueType", "", "reader.valueType()"), |
| 203 |
"JSWhyMagicImm": ("JSWhyMagic", "", "reader.whyMagic()"), |
| 204 |
"CallFlagsImm": ("CallFlags", "", "reader.callFlags()"), |
| 205 |
"ScalarTypeImm": ("Scalar::Type", "", "reader.scalarType()"), |
| 206 |
"UnaryMathFunctionImm": ("UnaryMathFunction", "", "reader.unaryMathFunction()"), |
| 207 |
"WasmValTypeImm": ("wasm::ValType::Kind", "", "reader.wasmValType()"), |
| 208 |
"Int32Imm": ("int32_t", "", "reader.int32Immediate()"), |
| 209 |
"UInt32Imm": ("uint32_t", "", "reader.uint32Immediate()"), |
| 210 |
"JSNativeImm": ("JSNative", "", "reinterpret_cast<JSNative>(reader.pointer())"), |
| 211 |
"StaticStringImm": ("const char*", "", "reinterpret_cast<char*>(reader.pointer())"), |
| 212 |
"AllocKindImm": ("gc::AllocKind", "", "reader.allocKind()"), |
| 213 |
"CompletionKindImm": ("CompletionKind", "", "reader.completionKind()"), |
| 214 |
"RealmFuseIndexImm": ("RealmFuses::FuseIndex", "", "reader.realmFuseIndex()"), |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
def gen_compiler_method(name, args): |
| 219 |
"""Generates CacheIRCompiler or WarpCacheIRTranspiler header code for a |
| 220 |
single opcode.""" |
| 221 |
|
| 222 |
method_name = "emit" + name |
| 223 |
|
| 224 |
# We generate the signature of the method that needs to be implemented and a |
| 225 |
# separate function forwarding to it. For example: |
| 226 |
# |
| 227 |
# [[nodiscard]] bool emitGuardShape(ObjOperandId objId, uint32_t shapeOffset); |
| 228 |
# [[nodiscard]] bool emitGuardShape(CacheIRReader& reader) { |
| 229 |
# ObjOperandId objId = reader.objOperandId(); |
| 230 |
# uint32_t shapeOffset = reader.stubOffset(); |
| 231 |
# return emitGuardShape(objId, shapeOffset); |
| 232 |
# } |
| 233 |
cpp_args = [] |
| 234 |
method_args = [] |
| 235 |
args_code = "" |
| 236 |
if args: |
| 237 |
for arg_name, arg_type in args.items(): |
| 238 |
cpp_type, suffix, readexpr = arg_reader_info[arg_type] |
| 239 |
cpp_name = arg_name + suffix |
| 240 |
cpp_args.append(cpp_name) |
| 241 |
method_args.append(f"{cpp_type} {cpp_name}") |
| 242 |
args_code += f" {cpp_type} {cpp_name} = {readexpr};\\\n" |
| 243 |
|
| 244 |
# Generate signature. |
| 245 |
code = "[[nodiscard]] bool {}({});\\\n".format(method_name, ", ".join(method_args)) |
| 246 |
|
| 247 |
# Generate the method forwarding to it. |
| 248 |
code += f"[[nodiscard]] bool {method_name}(CacheIRReader& reader) {{\\\n" |
| 249 |
code += args_code |
| 250 |
code += " return {}({});\\\n".format(method_name, ", ".join(cpp_args)) |
| 251 |
code += "}\\\n" |
| 252 |
|
| 253 |
return code |
| 254 |
|
| 255 |
|
| 256 |
# For each argument type, the method name for printing it. |
| 257 |
arg_spewer_method = { |
| 258 |
"ValId": "spewOperandId", |
| 259 |
"ObjId": "spewOperandId", |
| 260 |
"StringId": "spewOperandId", |
| 261 |
"SymbolId": "spewOperandId", |
| 262 |
"BooleanId": "spewOperandId", |
| 263 |
"Int32Id": "spewOperandId", |
| 264 |
"NumberId": "spewOperandId", |
| 265 |
"BigIntId": "spewOperandId", |
| 266 |
"ValueTagId": "spewOperandId", |
| 267 |
"IntPtrId": "spewOperandId", |
| 268 |
"RawId": "spewRawOperandId", |
| 269 |
"ShapeField": "spewField", |
| 270 |
"WeakShapeField": "spewField", |
| 271 |
"WeakGetterSetterField": "spewField", |
| 272 |
"ObjectField": "spewField", |
| 273 |
"WeakObjectField": "spewField", |
| 274 |
"StringField": "spewField", |
| 275 |
"AtomField": "spewField", |
| 276 |
"SymbolField": "spewField", |
| 277 |
"WeakBaseScriptField": "spewField", |
| 278 |
"JitCodeField": "spewField", |
| 279 |
"RawInt32Field": "spewField", |
| 280 |
"RawPointerField": "spewField", |
| 281 |
"ICScriptField": "spewField", |
| 282 |
"IdField": "spewField", |
| 283 |
"ValueField": "spewField", |
| 284 |
"RawInt64Field": "spewField", |
| 285 |
"DoubleField": "spewField", |
| 286 |
"AllocSiteField": "spewField", |
| 287 |
"JSOpImm": "spewJSOpImm", |
| 288 |
"JSTypeImm": "spewJSTypeImm", |
| 289 |
"TypeofEqOperandImm": "spewTypeofEqOperandImm", |
| 290 |
"BoolImm": "spewBoolImm", |
| 291 |
"ByteImm": "spewByteImm", |
| 292 |
"GuardClassKindImm": "spewGuardClassKindImm", |
| 293 |
"ArrayBufferViewKindImm": "spewArrayBufferViewKindImm", |
| 294 |
"ValueTypeImm": "spewValueTypeImm", |
| 295 |
"JSWhyMagicImm": "spewJSWhyMagicImm", |
| 296 |
"CallFlagsImm": "spewCallFlagsImm", |
| 297 |
"ScalarTypeImm": "spewScalarTypeImm", |
| 298 |
"UnaryMathFunctionImm": "spewUnaryMathFunctionImm", |
| 299 |
"WasmValTypeImm": "spewWasmValTypeImm", |
| 300 |
"Int32Imm": "spewInt32Imm", |
| 301 |
"UInt32Imm": "spewUInt32Imm", |
| 302 |
"JSNativeImm": "spewJSNativeImm", |
| 303 |
"StaticStringImm": "spewStaticStringImm", |
| 304 |
"AllocKindImm": "spewAllocKindImm", |
| 305 |
"CompletionKindImm": "spewCompletionKindImm", |
| 306 |
"RealmFuseIndexImm": "spewRealmFuseIndexImm", |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
def gen_spewer_method(name, args): |
| 311 |
"""Generates spewer code for a single opcode.""" |
| 312 |
|
| 313 |
method_name = "spew" + name |
| 314 |
|
| 315 |
# Generate code like this: |
| 316 |
# |
| 317 |
# void spewGuardShape(CacheIRReader& reader) { |
| 318 |
# spewOp(CacheOp::GuardShape); |
| 319 |
# spewOperandId("objId", reader.objOperandId()); |
| 320 |
# spewOperandSeparator(); |
| 321 |
# spewField("shapeOffset", reader.stubOffset()); |
| 322 |
# spewOpEnd(); |
| 323 |
# } |
| 324 |
args_code = "" |
| 325 |
if args: |
| 326 |
is_first = True |
| 327 |
for arg_name, arg_type in args.items(): |
| 328 |
_, suffix, readexpr = arg_reader_info[arg_type] |
| 329 |
arg_name += suffix |
| 330 |
spew_method = arg_spewer_method[arg_type] |
| 331 |
if not is_first: |
| 332 |
args_code += " spewArgSeparator();\\\n" |
| 333 |
args_code += f' {spew_method}("{arg_name}", {readexpr});\\\n' |
| 334 |
is_first = False |
| 335 |
|
| 336 |
code = f"void {method_name}(CacheIRReader& reader) {{\\\n" |
| 337 |
code += f" spewOp(CacheOp::{name});\\\n" |
| 338 |
code += args_code |
| 339 |
code += " spewOpEnd();\\\n" |
| 340 |
code += "}\\\n" |
| 341 |
|
| 342 |
return code |
| 343 |
|
| 344 |
|
| 345 |
def gen_clone_method(name, args): |
| 346 |
"""Generates code for cloning a single opcode.""" |
| 347 |
|
| 348 |
method_name = "clone" + name |
| 349 |
|
| 350 |
# Generate code like this: |
| 351 |
# |
| 352 |
# void cloneGuardShape(CacheIRReader& reader, CacheIRWriter& writer) { |
| 353 |
# writer.writeOp(CacheOp::GuardShape); |
| 354 |
# ObjOperandId objId = reader.objOperandId(); |
| 355 |
# writer.writeOperandId(objId); |
| 356 |
# uint32_t shapeOffset = reader.stubOffset(); |
| 357 |
# Shape* shape = getShapeField(shapeOffset); |
| 358 |
# writer.writeShapeField(shape); |
| 359 |
# writer.assertLengthMatches(); |
| 360 |
# } |
| 361 |
|
| 362 |
args_code = "" |
| 363 |
if args: |
| 364 |
for arg_name, arg_type in args.items(): |
| 365 |
if arg_type == "RawId": |
| 366 |
arg_type = "ValId" |
| 367 |
|
| 368 |
read_type, suffix, readexpr = arg_reader_info[arg_type] |
| 369 |
read_name = arg_name + suffix |
| 370 |
value_name = read_name |
| 371 |
args_code += f" {read_type} {read_name} = {readexpr};\\\n" |
| 372 |
|
| 373 |
write_type, write_method = arg_writer_info[arg_type] |
| 374 |
if arg_name == "result": |
| 375 |
args_code += " writer.newOperandId();\\\n" |
| 376 |
if suffix == "Offset": |
| 377 |
# If the write function takes T&, the intermediate variable |
| 378 |
# should be of type T. |
| 379 |
if write_type.endswith("&"): |
| 380 |
write_type = write_type[:-1] |
| 381 |
value_name = arg_name |
| 382 |
args_code += ( |
| 383 |
f" {write_type} {value_name} = get{arg_type}({read_name});\\\n" |
| 384 |
) |
| 385 |
args_code += f" writer.{write_method}({value_name});\\\n" |
| 386 |
|
| 387 |
code = f"void {method_name}" |
| 388 |
code += "(CacheIRReader& reader, CacheIRWriter& writer) {{\\\n" |
| 389 |
code += f" writer.writeOp(CacheOp::{name});\\\n" |
| 390 |
code += args_code |
| 391 |
code += " writer.assertLengthMatches();\\\n" |
| 392 |
code += "}}\\\n" |
| 393 |
|
| 394 |
return code |
| 395 |
|
| 396 |
|
| 397 |
# Length in bytes for each argument type, either an integer or a C++ expression. |
| 398 |
# This is used to generate the CacheIROpArgLengths array. CacheIRWriter asserts |
| 399 |
# the number of bytes written matches the value in that array. |
| 400 |
arg_length = { |
| 401 |
"ValId": 1, |
| 402 |
"ObjId": 1, |
| 403 |
"StringId": 1, |
| 404 |
"SymbolId": 1, |
| 405 |
"BooleanId": 1, |
| 406 |
"Int32Id": 1, |
| 407 |
"NumberId": 1, |
| 408 |
"BigIntId": 1, |
| 409 |
"ValueTagId": 1, |
| 410 |
"IntPtrId": 1, |
| 411 |
"RawId": 1, |
| 412 |
"ShapeField": 1, |
| 413 |
"WeakShapeField": 1, |
| 414 |
"WeakGetterSetterField": 1, |
| 415 |
"ObjectField": 1, |
| 416 |
"WeakObjectField": 1, |
| 417 |
"StringField": 1, |
| 418 |
"AtomField": 1, |
| 419 |
"SymbolField": 1, |
| 420 |
"WeakBaseScriptField": 1, |
| 421 |
"JitCodeField": 1, |
| 422 |
"RawInt32Field": 1, |
| 423 |
"RawPointerField": 1, |
| 424 |
"ICScriptField": 1, |
| 425 |
"RawInt64Field": 1, |
| 426 |
"DoubleField": 1, |
| 427 |
"IdField": 1, |
| 428 |
"ValueField": 1, |
| 429 |
"AllocSiteField": 1, |
| 430 |
"ByteImm": 1, |
| 431 |
"BoolImm": 1, |
| 432 |
"CallFlagsImm": 1, |
| 433 |
"ScalarTypeImm": 1, |
| 434 |
"UnaryMathFunctionImm": 1, |
| 435 |
"JSOpImm": 1, |
| 436 |
"JSTypeImm": 1, |
| 437 |
"TypeofEqOperandImm": 1, |
| 438 |
"ValueTypeImm": 1, |
| 439 |
"GuardClassKindImm": 1, |
| 440 |
"ArrayBufferViewKindImm": 1, |
| 441 |
"JSWhyMagicImm": 1, |
| 442 |
"WasmValTypeImm": 1, |
| 443 |
"Int32Imm": 4, |
| 444 |
"UInt32Imm": 4, |
| 445 |
"JSNativeImm": "sizeof(uintptr_t)", |
| 446 |
"StaticStringImm": "sizeof(uintptr_t)", |
| 447 |
"AllocKindImm": 1, |
| 448 |
"CompletionKindImm": 1, |
| 449 |
"RealmFuseIndexImm": 1, |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
def generate_cacheirops_header(c_out, yaml_path): |
| 454 |
"""Generate CacheIROpsGenerated.h from CacheIROps.yaml. The generated file |
| 455 |
contains a list of all CacheIR ops and generated source code for |
| 456 |
CacheIRWriter and CacheIRCompiler.""" |
| 457 |
|
| 458 |
data = load_yaml(yaml_path) |
| 459 |
|
| 460 |
# CACHE_IR_OPS items. Each item stores an opcode name and arguments length |
| 461 |
# expression. For example: _(GuardShape, 1 + 1) |
| 462 |
ops_items = [] |
| 463 |
|
| 464 |
# Generated CacheIRWriter methods. |
| 465 |
writer_methods = [] |
| 466 |
|
| 467 |
# Generated CacheIRCompiler methods. |
| 468 |
compiler_shared_methods = [] |
| 469 |
compiler_unshared_methods = [] |
| 470 |
|
| 471 |
# Generated WarpCacheIRTranspiler methods. |
| 472 |
transpiler_methods = [] |
| 473 |
|
| 474 |
# List of ops supported by WarpCacheIRTranspiler. |
| 475 |
transpiler_ops = [] |
| 476 |
|
| 477 |
# Generated methods for spewers. |
| 478 |
spewer_methods = [] |
| 479 |
|
| 480 |
# Generated methods for cloning IC stubs |
| 481 |
clone_methods = [] |
| 482 |
|
| 483 |
for op in data: |
| 484 |
name = op["name"] |
| 485 |
|
| 486 |
args = op["args"] |
| 487 |
assert args is None or isinstance(args, dict) |
| 488 |
|
| 489 |
shared = op["shared"] |
| 490 |
assert isinstance(shared, bool) |
| 491 |
|
| 492 |
transpile = op["transpile"] |
| 493 |
assert isinstance(transpile, bool) |
| 494 |
|
| 495 |
# Unscored Ops default to UINT32_MAX |
| 496 |
cost_estimate = op.get("cost_estimate", 0xFFFFFFFF) |
| 497 |
assert isinstance(cost_estimate, int) |
| 498 |
|
| 499 |
custom_writer = op.get("custom_writer", False) |
| 500 |
assert isinstance(custom_writer, bool) |
| 501 |
|
| 502 |
if args: |
| 503 |
args_length = " + ".join([str(arg_length[v]) for v in args.values()]) |
| 504 |
else: |
| 505 |
args_length = "0" |
| 506 |
|
| 507 |
transpile_str = "true" if transpile else "false" |
| 508 |
ops_items.append(f"_({name}, {args_length}, {transpile_str}, {cost_estimate})") |
| 509 |
|
| 510 |
writer_methods.append(gen_writer_method(name, args, custom_writer)) |
| 511 |
|
| 512 |
if shared: |
| 513 |
compiler_shared_methods.append(gen_compiler_method(name, args)) |
| 514 |
else: |
| 515 |
compiler_unshared_methods.append(gen_compiler_method(name, args)) |
| 516 |
|
| 517 |
if transpile: |
| 518 |
transpiler_methods.append(gen_compiler_method(name, args)) |
| 519 |
transpiler_ops.append(f"_({name})") |
| 520 |
|
| 521 |
spewer_methods.append(gen_spewer_method(name, args)) |
| 522 |
|
| 523 |
clone_methods.append(gen_clone_method(name, args)) |
| 524 |
|
| 525 |
contents = "#define CACHE_IR_OPS(_)\\\n" |
| 526 |
contents += "\\\n".join(ops_items) |
| 527 |
contents += "\n\n" |
| 528 |
|
| 529 |
contents += "#define CACHE_IR_WRITER_GENERATED \\\n" |
| 530 |
contents += "\\\n".join(writer_methods) |
| 531 |
contents += "\n\n" |
| 532 |
|
| 533 |
contents += "#define CACHE_IR_COMPILER_SHARED_GENERATED \\\n" |
| 534 |
contents += "\\\n".join(compiler_shared_methods) |
| 535 |
contents += "\n\n" |
| 536 |
|
| 537 |
contents += "#define CACHE_IR_COMPILER_UNSHARED_GENERATED \\\n" |
| 538 |
contents += "\\\n".join(compiler_unshared_methods) |
| 539 |
contents += "\n\n" |
| 540 |
|
| 541 |
contents += "#define CACHE_IR_TRANSPILER_GENERATED \\\n" |
| 542 |
contents += "\\\n".join(transpiler_methods) |
| 543 |
contents += "\n\n" |
| 544 |
|
| 545 |
contents += "#define CACHE_IR_TRANSPILER_OPS(_)\\\n" |
| 546 |
contents += "\\\n".join(transpiler_ops) |
| 547 |
contents += "\n\n" |
| 548 |
|
| 549 |
contents += "#define CACHE_IR_SPEWER_GENERATED \\\n" |
| 550 |
contents += "\\\n".join(spewer_methods) |
| 551 |
contents += "\n\n" |
| 552 |
|
| 553 |
contents += "#define CACHE_IR_CLONE_GENERATED \\\n" |
| 554 |
contents += "\\\n".join(clone_methods) |
| 555 |
contents += "\n\n" |
| 556 |
|
| 557 |
generate_header(c_out, "jit_CacheIROpsGenerated_h", contents) |
| 558 |
|
| 559 |
|
| 560 |
def read_aot_ics(ic_path): |
| 561 |
ics = "" |
| 562 |
idx = 0 |
| 563 |
for entry in os.scandir(ic_path): |
| 564 |
if entry.is_file() and os.path.basename(entry.path).startswith("IC-"): |
| 565 |
with open(entry.path) as f: |
| 566 |
content = f.read().strip() |
| 567 |
ics += " _(%d, %s) \\\n" % (idx, content) |
| 568 |
idx += 1 |
| 569 |
return ics |
| 570 |
|
| 571 |
|
| 572 |
def generate_aot_ics_header(c_out, ic_path): |
| 573 |
"""Generate CacheIROpsGenerated.h from AOT IC corpus.""" |
| 574 |
|
| 575 |
# Read in all ICs from js/src/ics/IC-*. |
| 576 |
ics = read_aot_ics(ic_path) |
| 577 |
|
| 578 |
contents = "#define JS_AOT_IC_DATA(_) \\\n" |
| 579 |
contents += ics |
| 580 |
contents += "\n" |
| 581 |
|
| 582 |
generate_header(c_out, "jit_CacheIRAOTGenerated_h", contents) |