pytecode 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,495 @@
1
+ """Data types representing JVM bytecode instruction operands.
2
+
3
+ Provides dataclasses and enums that model the operand formats for each JVM
4
+ instruction as defined in the JVM specification (JVMS §6.5). Each opcode is
5
+ mapped to an ``InsnInfoType`` member whose associated ``InsnInfo`` subclass
6
+ describes the shape of its operands.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from dataclasses import dataclass
12
+ from enum import IntEnum
13
+
14
+ __all__ = [
15
+ "ArrayType",
16
+ "Branch",
17
+ "BranchW",
18
+ "ByteValue",
19
+ "ConstPoolIndex",
20
+ "IInc",
21
+ "IIncW",
22
+ "InsnInfo",
23
+ "InsnInfoType",
24
+ "InvokeDynamic",
25
+ "InvokeInterface",
26
+ "LocalIndex",
27
+ "LocalIndexW",
28
+ "LookupSwitch",
29
+ "MatchOffsetPair",
30
+ "MultiANewArray",
31
+ "NewArray",
32
+ "ShortValue",
33
+ "TableSwitch",
34
+ ]
35
+
36
+
37
+ @dataclass
38
+ class InsnInfo:
39
+ """Base operand info for a JVM bytecode instruction.
40
+
41
+ Attributes:
42
+ type: The ``InsnInfoType`` member identifying the opcode.
43
+ bytecode_offset: Byte offset of this instruction within the Code
44
+ attribute.
45
+ """
46
+
47
+ type: InsnInfoType
48
+ bytecode_offset: int
49
+
50
+
51
+ @dataclass
52
+ class LocalIndex(InsnInfo):
53
+ """Operand carrying a single-byte local variable index (§6.5).
54
+
55
+ Attributes:
56
+ index: Local variable slot index (0–255).
57
+ """
58
+
59
+ index: int
60
+
61
+
62
+ @dataclass
63
+ class LocalIndexW(InsnInfo):
64
+ """Operand carrying a wide (two-byte) local variable index (§6.5.wide).
65
+
66
+ Attributes:
67
+ index: Local variable slot index (0–65535).
68
+ """
69
+
70
+ index: int
71
+
72
+
73
+ @dataclass
74
+ class ConstPoolIndex(InsnInfo):
75
+ """Operand carrying a two-byte constant pool index.
76
+
77
+ Attributes:
78
+ index: Index into the class file constant pool.
79
+ """
80
+
81
+ index: int
82
+
83
+
84
+ @dataclass
85
+ class ByteValue(InsnInfo):
86
+ """Operand carrying a signed byte immediate value (e.g. ``bipush``).
87
+
88
+ Attributes:
89
+ value: Signed byte value (−128–127).
90
+ """
91
+
92
+ value: int
93
+
94
+
95
+ @dataclass
96
+ class ShortValue(InsnInfo):
97
+ """Operand carrying a signed short immediate value (e.g. ``sipush``).
98
+
99
+ Attributes:
100
+ value: Signed short value (−32768–32767).
101
+ """
102
+
103
+ value: int
104
+
105
+
106
+ @dataclass
107
+ class Branch(InsnInfo):
108
+ """Operand for a branch instruction with a two-byte signed offset.
109
+
110
+ Attributes:
111
+ offset: Signed branch offset relative to this instruction.
112
+ """
113
+
114
+ offset: int
115
+
116
+
117
+ @dataclass
118
+ class BranchW(InsnInfo):
119
+ """Operand for a wide branch instruction with a four-byte signed offset.
120
+
121
+ Attributes:
122
+ offset: Signed branch offset relative to this instruction.
123
+ """
124
+
125
+ offset: int
126
+
127
+
128
+ @dataclass
129
+ class IInc(InsnInfo):
130
+ """Operand for the ``iinc`` instruction (§6.5.iinc).
131
+
132
+ Attributes:
133
+ index: Local variable slot index (0–255).
134
+ value: Signed byte increment constant.
135
+ """
136
+
137
+ index: int
138
+ value: int
139
+
140
+
141
+ @dataclass
142
+ class IIncW(InsnInfo):
143
+ """Operand for the wide form of ``iinc`` (§6.5.wide).
144
+
145
+ Attributes:
146
+ index: Local variable slot index (0–65535).
147
+ value: Signed short increment constant.
148
+ """
149
+
150
+ index: int
151
+ value: int
152
+
153
+
154
+ @dataclass
155
+ class InvokeDynamic(InsnInfo):
156
+ """Operand for the ``invokedynamic`` instruction (§6.5.invokedynamic).
157
+
158
+ Attributes:
159
+ index: Constant pool index to a ``CONSTANT_InvokeDynamic_info``.
160
+ unused: Two reserved zero bytes following the index.
161
+ """
162
+
163
+ index: int
164
+ unused: bytes
165
+
166
+
167
+ @dataclass
168
+ class InvokeInterface(InsnInfo):
169
+ """Operand for the ``invokeinterface`` instruction (§6.5.invokeinterface).
170
+
171
+ Attributes:
172
+ index: Constant pool index to a ``CONSTANT_InterfaceMethodref_info``.
173
+ count: Number of argument slots (non-zero).
174
+ unused: One reserved zero byte following count.
175
+ """
176
+
177
+ index: int
178
+ count: int
179
+ unused: bytes
180
+
181
+
182
+ @dataclass
183
+ class NewArray(InsnInfo):
184
+ """Operand for the ``newarray`` instruction (§6.5.newarray).
185
+
186
+ Attributes:
187
+ atype: ``ArrayType`` enum member identifying the primitive element type.
188
+ """
189
+
190
+ atype: ArrayType
191
+
192
+
193
+ @dataclass
194
+ class MultiANewArray(InsnInfo):
195
+ """Operand for the ``multianewarray`` instruction (§6.5.multianewarray).
196
+
197
+ Attributes:
198
+ index: Constant pool index to the array class.
199
+ dimensions: Number of dimensions to allocate (≥ 1).
200
+ """
201
+
202
+ index: int
203
+ dimensions: int
204
+
205
+
206
+ @dataclass
207
+ class MatchOffsetPair:
208
+ """A single match-offset entry used in a ``lookupswitch`` table.
209
+
210
+ Attributes:
211
+ match: The integer case value.
212
+ offset: Branch offset relative to the ``lookupswitch`` instruction.
213
+ """
214
+
215
+ match: int
216
+ offset: int
217
+
218
+
219
+ @dataclass
220
+ class LookupSwitch(InsnInfo):
221
+ """Operand for the ``lookupswitch`` instruction (§6.5.lookupswitch).
222
+
223
+ Attributes:
224
+ default: Default branch offset when no key matches.
225
+ npairs: Number of match-offset pairs.
226
+ pairs: Sorted list of ``MatchOffsetPair`` entries.
227
+ """
228
+
229
+ default: int
230
+ npairs: int
231
+ pairs: list[MatchOffsetPair]
232
+
233
+
234
+ @dataclass
235
+ class TableSwitch(InsnInfo):
236
+ """Operand for the ``tableswitch`` instruction (§6.5.tableswitch).
237
+
238
+ Attributes:
239
+ default: Default branch offset when the index is out of range.
240
+ low: Lowest case index value.
241
+ high: Highest case index value.
242
+ offsets: Branch offsets for each case from *low* to *high* inclusive.
243
+ """
244
+
245
+ default: int
246
+ low: int
247
+ high: int
248
+ offsets: list[int]
249
+
250
+
251
+ class InsnInfoType(IntEnum):
252
+ """Enum mapping every JVM opcode to its operand format.
253
+
254
+ Each member's integer value is the opcode byte, and its ``instinfo``
255
+ attribute is the ``InsnInfo`` subclass that describes the operand layout.
256
+ """
257
+
258
+ AALOAD = 0x32, InsnInfo
259
+ AASTORE = 0x53, InsnInfo
260
+ ACONST_NULL = 0x01, InsnInfo
261
+ ALOAD = 0x19, LocalIndex
262
+ ALOAD_0 = 0x2A, InsnInfo
263
+ ALOAD_1 = 0x2B, InsnInfo
264
+ ALOAD_2 = 0x2C, InsnInfo
265
+ ALOAD_3 = 0x2D, InsnInfo
266
+ ANEWARRAY = 0xBD, ConstPoolIndex
267
+ ARETURN = 0xB0, InsnInfo
268
+ ARRAYLENGTH = 0xBE, InsnInfo
269
+ ASTORE = 0x3A, LocalIndex
270
+ ASTORE_0 = 0x4B, InsnInfo
271
+ ASTORE_1 = 0x4C, InsnInfo
272
+ ASTORE_2 = 0x4D, InsnInfo
273
+ ASTORE_3 = 0x4E, InsnInfo
274
+ ATHROW = 0xBF, InsnInfo
275
+ BALOAD = 0x33, InsnInfo
276
+ BASTORE = 0x54, InsnInfo
277
+ BIPUSH = 0x10, ByteValue
278
+ CALOAD = 0x34, InsnInfo
279
+ CASTORE = 0x55, InsnInfo
280
+ CHECKCAST = 0xC0, ConstPoolIndex
281
+ D2F = 0x90, InsnInfo
282
+ D2I = 0x8E, InsnInfo
283
+ D2L = 0x8F, InsnInfo
284
+ DADD = 0x63, InsnInfo
285
+ DALOAD = 0x31, InsnInfo
286
+ DASTORE = 0x52, InsnInfo
287
+ DCMPG = 0x98, InsnInfo
288
+ DCMPL = 0x97, InsnInfo
289
+ DCONST_0 = 0x0E, InsnInfo
290
+ DCONST_1 = 0x0F, InsnInfo
291
+ DDIV = 0x6F, InsnInfo
292
+ DLOAD = 0x18, LocalIndex
293
+ DLOAD_0 = 0x26, InsnInfo
294
+ DLOAD_1 = 0x27, InsnInfo
295
+ DLOAD_2 = 0x28, InsnInfo
296
+ DLOAD_3 = 0x29, InsnInfo
297
+ DMUL = 0x6B, InsnInfo
298
+ DNEG = 0x77, InsnInfo
299
+ DREM = 0x73, InsnInfo
300
+ DRETURN = 0xAF, InsnInfo
301
+ DSTORE = 0x39, LocalIndex
302
+ DSTORE_0 = 0x47, InsnInfo
303
+ DSTORE_1 = 0x48, InsnInfo
304
+ DSTORE_2 = 0x49, InsnInfo
305
+ DSTORE_3 = 0x4A, InsnInfo
306
+ DSUB = 0x67, InsnInfo
307
+ DUP = 0x59, InsnInfo
308
+ DUP_X1 = 0x5A, InsnInfo
309
+ DUP_X2 = 0x5B, InsnInfo
310
+ DUP2 = 0x5C, InsnInfo
311
+ DUP2_X1 = 0x5D, InsnInfo
312
+ DUP2_X2 = 0x5E, InsnInfo
313
+ F2D = 0x8D, InsnInfo
314
+ F2I = 0x8B, InsnInfo
315
+ F2L = 0x8C, InsnInfo
316
+ FADD = 0x62, InsnInfo
317
+ FALOAD = 0x30, InsnInfo
318
+ FASTORE = 0x51, InsnInfo
319
+ FCMPG = 0x96, InsnInfo
320
+ FCMPL = 0x95, InsnInfo
321
+ FCONST_0 = 0x0B, InsnInfo
322
+ FCONST_1 = 0x0C, InsnInfo
323
+ FCONST_2 = 0x0D, InsnInfo
324
+ FDIV = 0x6E, InsnInfo
325
+ FLOAD = 0x17, LocalIndex
326
+ FLOAD_0 = 0x22, InsnInfo
327
+ FLOAD_1 = 0x23, InsnInfo
328
+ FLOAD_2 = 0x24, InsnInfo
329
+ FLOAD_3 = 0x25, InsnInfo
330
+ FMUL = 0x6A, InsnInfo
331
+ FNEG = 0x76, InsnInfo
332
+ FREM = 0x72, InsnInfo
333
+ FRETURN = 0xAE, InsnInfo
334
+ FSTORE = 0x38, LocalIndex
335
+ FSTORE_0 = 0x43, InsnInfo
336
+ FSTORE_1 = 0x44, InsnInfo
337
+ FSTORE_2 = 0x45, InsnInfo
338
+ FSTORE_3 = 0x46, InsnInfo
339
+ FSUB = 0x66, InsnInfo
340
+ GETFIELD = 0xB4, ConstPoolIndex
341
+ GETSTATIC = 0xB2, ConstPoolIndex
342
+ GOTO = 0xA7, Branch
343
+ GOTO_W = 0xC8, BranchW
344
+ I2B = 0x91, InsnInfo
345
+ I2C = 0x92, InsnInfo
346
+ I2D = 0x87, InsnInfo
347
+ I2F = 0x86, InsnInfo
348
+ I2L = 0x85, InsnInfo
349
+ I2S = 0x93, InsnInfo
350
+ IADD = 0x60, InsnInfo
351
+ IALOAD = 0x2E, InsnInfo
352
+ IAND = 0x7E, InsnInfo
353
+ IASTORE = 0x4F, InsnInfo
354
+ ICONST_M1 = 0x02, InsnInfo
355
+ ICONST_0 = 0x03, InsnInfo
356
+ ICONST_1 = 0x04, InsnInfo
357
+ ICONST_2 = 0x05, InsnInfo
358
+ ICONST_3 = 0x06, InsnInfo
359
+ ICONST_4 = 0x07, InsnInfo
360
+ ICONST_5 = 0x08, InsnInfo
361
+ IDIV = 0x6C, InsnInfo
362
+ IF_ACMPEQ = 0xA5, Branch
363
+ IF_ACMPNE = 0xA6, Branch
364
+ IF_ICMPEQ = 0x9F, Branch
365
+ IF_ICMPGE = 0xA2, Branch
366
+ IF_ICMPGT = 0xA3, Branch
367
+ IF_ICMPLE = 0xA4, Branch
368
+ IF_ICMPLT = 0xA1, Branch
369
+ IF_ICMPNE = 0xA0, Branch
370
+ IFEQ = 0x99, Branch
371
+ IFGE = 0x9C, Branch
372
+ IFGT = 0x9D, Branch
373
+ IFLE = 0x9E, Branch
374
+ IFLT = 0x9B, Branch
375
+ IFNE = 0x9A, Branch
376
+ IFNONNULL = 0xC7, Branch
377
+ IFNULL = 0xC6, Branch
378
+ IINC = 0x84, IInc
379
+ ILOAD = 0x15, LocalIndex
380
+ ILOAD_0 = 0x1A, InsnInfo
381
+ ILOAD_1 = 0x1B, InsnInfo
382
+ ILOAD_2 = 0x1C, InsnInfo
383
+ ILOAD_3 = 0x1D, InsnInfo
384
+ IMUL = 0x68, InsnInfo
385
+ INEG = 0x74, InsnInfo
386
+ INSTANCEOF = 0xC1, ConstPoolIndex
387
+ INVOKEDYNAMIC = 0xBA, InvokeDynamic
388
+ INVOKEINTERFACE = 0xB9, InvokeInterface
389
+ INVOKESPECIAL = 0xB7, ConstPoolIndex
390
+ INVOKESTATIC = 0xB8, ConstPoolIndex
391
+ INVOKEVIRTUAL = 0xB6, ConstPoolIndex
392
+ IOR = 0x80, InsnInfo
393
+ IREM = 0x70, InsnInfo
394
+ IRETURN = 0xAC, InsnInfo
395
+ ISHL = 0x78, InsnInfo
396
+ ISHR = 0x7A, InsnInfo
397
+ ISTORE = 0x36, LocalIndex
398
+ ISTORE_0 = 0x3B, InsnInfo
399
+ ISTORE_1 = 0x3C, InsnInfo
400
+ ISTORE_2 = 0x3D, InsnInfo
401
+ ISTORE_3 = 0x3E, InsnInfo
402
+ ISUB = 0x64, InsnInfo
403
+ IUSHR = 0x7C, InsnInfo
404
+ IXOR = 0x82, InsnInfo
405
+ JSR = 0xA8, Branch
406
+ JSR_W = 0xC9, BranchW
407
+ L2D = 0x8A, InsnInfo
408
+ L2F = 0x89, InsnInfo
409
+ L2I = 0x88, InsnInfo
410
+ LADD = 0x61, InsnInfo
411
+ LALOAD = 0x2F, InsnInfo
412
+ LAND = 0x7F, InsnInfo
413
+ LASTORE = 0x50, InsnInfo
414
+ LCMP = 0x94, InsnInfo
415
+ LCONST_0 = 0x09, InsnInfo
416
+ LCONST_1 = 0x0A, InsnInfo
417
+ LDC = 0x12, LocalIndex
418
+ LDC_W = 0x13, ConstPoolIndex
419
+ LDC2_W = 0x14, ConstPoolIndex
420
+ LDIV = 0x6D, InsnInfo
421
+ LLOAD = 0x16, LocalIndex
422
+ LLOAD_0 = 0x1E, InsnInfo
423
+ LLOAD_1 = 0x1F, InsnInfo
424
+ LLOAD_2 = 0x20, InsnInfo
425
+ LLOAD_3 = 0x21, InsnInfo
426
+ LMUL = 0x69, InsnInfo
427
+ LNEG = 0x75, InsnInfo
428
+ LOOKUPSWITCH = 0xAB, LookupSwitch
429
+ LOR = 0x81, InsnInfo
430
+ LREM = 0x71, InsnInfo
431
+ LRETURN = 0xAD, InsnInfo
432
+ LSHL = 0x79, InsnInfo
433
+ LSHR = 0x7B, InsnInfo
434
+ LSTORE = 0x37, LocalIndex
435
+ LSTORE_0 = 0x3F, InsnInfo
436
+ LSTORE_1 = 0x40, InsnInfo
437
+ LSTORE_2 = 0x41, InsnInfo
438
+ LSTORE_3 = 0x42, InsnInfo
439
+ LSUB = 0x65, InsnInfo
440
+ LUSHR = 0x7D, InsnInfo
441
+ LXOR = 0x83, InsnInfo
442
+ MONITORENTER = 0xC2, InsnInfo
443
+ MONITOREXIT = 0xC3, InsnInfo
444
+ MULTIANEWARRAY = 0xC5, MultiANewArray
445
+ NEW = 0xBB, ConstPoolIndex
446
+ NEWARRAY = 0xBC, NewArray
447
+ NOP = 0x00, InsnInfo
448
+ POP = 0x57, InsnInfo
449
+ POP2 = 0x58, InsnInfo
450
+ PUTFIELD = 0xB5, ConstPoolIndex
451
+ PUTSTATIC = 0xB3, ConstPoolIndex
452
+ RET = 0xA9, LocalIndex
453
+ RETURN = 0xB1, InsnInfo
454
+ SALOAD = 0x35, InsnInfo
455
+ SASTORE = 0x56, InsnInfo
456
+ SIPUSH = 0x11, ShortValue
457
+ SWAP = 0x5F, InsnInfo
458
+ TABLESWITCH = 0xAA, TableSwitch
459
+ WIDE = 0xC4, InsnInfo
460
+ ALOADW = (WIDE[0] + ALOAD[0]), LocalIndexW
461
+ ASTOREW = (WIDE[0] + ASTORE[0]), LocalIndexW
462
+ DLOADW = (WIDE[0] + DLOAD[0]), LocalIndexW
463
+ DSTOREW = (WIDE[0] + DSTORE[0]), LocalIndexW
464
+ FLOADW = (WIDE[0] + FLOAD[0]), LocalIndexW
465
+ FSTOREW = (WIDE[0] + FSTORE[0]), LocalIndexW
466
+ ILOADW = (WIDE[0] + ILOAD[0]), LocalIndexW
467
+ ISTOREW = (WIDE[0] + ISTORE[0]), LocalIndexW
468
+ LLOADW = (WIDE[0] + LLOAD[0]), LocalIndexW
469
+ LSTOREW = (WIDE[0] + LSTORE[0]), LocalIndexW
470
+ RETW = (WIDE[0] + RET[0]), LocalIndexW
471
+ IINCW = (WIDE[0] + IINC[0]), IIncW
472
+
473
+ instinfo: type[InsnInfo]
474
+
475
+ def __new__(cls, value: int, instinfo: type[InsnInfo]) -> InsnInfoType:
476
+ obj = int.__new__(cls, value)
477
+ obj._value_ = value
478
+ obj.instinfo = instinfo
479
+ return obj
480
+
481
+
482
+ class ArrayType(IntEnum):
483
+ """Primitive array element types for the ``newarray`` instruction (§6.5.newarray).
484
+
485
+ Each member's value corresponds to the ``atype`` operand byte.
486
+ """
487
+
488
+ BOOLEAN = 4
489
+ CHAR = 5
490
+ FLOAT = 6
491
+ DOUBLE = 7
492
+ BYTE = 8
493
+ SHORT = 9
494
+ INT = 10
495
+ LONG = 11