|
7 | 7 |
|
8 | 8 | from analyzer import ( |
9 | 9 | Analysis, |
10 | | - Instruction, |
11 | | - Uop, |
12 | | - Part, |
13 | 10 | analyze_files, |
14 | | - Skip, |
15 | | - Flush, |
16 | | - analysis_error, |
17 | | - StackItem, |
18 | 11 | ) |
19 | 12 | from generators_common import ( |
20 | 13 | DEFAULT_INPUT, |
21 | 14 | ROOT, |
22 | 15 | write_header, |
23 | | - type_and_null, |
24 | | - Emitter, |
25 | 16 | ) |
26 | 17 | from cwriter import CWriter |
27 | 18 | from typing import TextIO |
28 | | -from stack import Local, Stack, StackError, get_stack_effect, Storage |
29 | 19 |
|
30 | 20 |
|
31 | 21 | DEFAULT_OUTPUT = ROOT / "Python/generated_labels.c.h" |
32 | 22 |
|
33 | 23 |
|
34 | | - |
35 | | -def declare_variable(var: StackItem, out: CWriter) -> None: |
36 | | - type, null = type_and_null(var) |
37 | | - space = " " if type[-1].isalnum() else "" |
38 | | - if var.condition: |
39 | | - out.emit(f"{type}{space}{var.name} = {null};\n") |
40 | | - else: |
41 | | - out.emit(f"{type}{space}{var.name};\n") |
42 | | - |
43 | | - |
44 | | -def declare_variables(inst: Instruction, out: CWriter) -> None: |
45 | | - try: |
46 | | - stack = get_stack_effect(inst) |
47 | | - except StackError as ex: |
48 | | - raise analysis_error(ex.args[0], inst.where) from None |
49 | | - required = set(stack.defined) |
50 | | - required.discard("unused") |
51 | | - for part in inst.parts: |
52 | | - if not isinstance(part, Uop): |
53 | | - continue |
54 | | - for var in part.stack.inputs: |
55 | | - if var.name in required: |
56 | | - required.remove(var.name) |
57 | | - declare_variable(var, out) |
58 | | - for var in part.stack.outputs: |
59 | | - if var.name in required: |
60 | | - required.remove(var.name) |
61 | | - declare_variable(var, out) |
62 | | - |
63 | | - |
64 | | -def write_uop( |
65 | | - uop: Part, |
66 | | - emitter: Emitter, |
67 | | - offset: int, |
68 | | - stack: Stack, |
69 | | - inst: Instruction, |
70 | | - braces: bool, |
71 | | -) -> tuple[int, Stack]: |
72 | | - # out.emit(stack.as_comment() + "\n") |
73 | | - if isinstance(uop, Skip): |
74 | | - entries = "entries" if uop.size > 1 else "entry" |
75 | | - emitter.emit(f"/* Skip {uop.size} cache {entries} */\n") |
76 | | - return (offset + uop.size), stack |
77 | | - if isinstance(uop, Flush): |
78 | | - emitter.emit(f"// flush\n") |
79 | | - stack.flush(emitter.out) |
80 | | - return offset, stack |
81 | | - try: |
82 | | - locals: dict[str, Local] = {} |
83 | | - emitter.out.start_line() |
84 | | - if braces: |
85 | | - emitter.out.emit(f"// {uop.name}\n") |
86 | | - emitter.emit("{\n") |
87 | | - code_list, storage = Storage.for_uop(stack, uop) |
88 | | - emitter._print_storage(storage) |
89 | | - for code in code_list: |
90 | | - emitter.emit(code) |
91 | | - |
92 | | - for cache in uop.caches: |
93 | | - if cache.name != "unused": |
94 | | - if cache.size == 4: |
95 | | - type = "PyObject *" |
96 | | - reader = "read_obj" |
97 | | - else: |
98 | | - type = f"uint{cache.size*16}_t " |
99 | | - reader = f"read_u{cache.size*16}" |
100 | | - emitter.emit( |
101 | | - f"{type}{cache.name} = {reader}(&this_instr[{offset}].cache);\n" |
102 | | - ) |
103 | | - if inst.family is None: |
104 | | - emitter.emit(f"(void){cache.name};\n") |
105 | | - offset += cache.size |
106 | | - |
107 | | - storage = emitter.emit_tokens(uop, storage, inst) |
108 | | - if braces: |
109 | | - emitter.out.start_line() |
110 | | - emitter.emit("}\n") |
111 | | - # emitter.emit(stack.as_comment() + "\n") |
112 | | - return offset, storage.stack |
113 | | - except StackError as ex: |
114 | | - raise analysis_error(ex.args[0], uop.body[0]) |
115 | | - |
116 | | - |
117 | | -def uses_this(inst: Instruction) -> bool: |
118 | | - if inst.properties.needs_this: |
119 | | - return True |
120 | | - for uop in inst.parts: |
121 | | - if not isinstance(uop, Uop): |
122 | | - continue |
123 | | - for cache in uop.caches: |
124 | | - if cache.name != "unused": |
125 | | - return True |
126 | | - return False |
127 | | - |
128 | | - |
129 | 24 | def generate_labels( |
130 | 25 | filenames: list[str], analysis: Analysis, outfile: TextIO |
131 | 26 | ) -> None: |
|
0 commit comments