File: autogen.py

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (262 lines) | stat: -rwxr-xr-x 10,040 bytes parent folder | download
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
# ========================== begin_copyright_notice ============================
#
# Copyright (C) 2017-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
# =========================== end_copyright_notice =============================

import os
import sys
import errno
import re
from typing import List


class DeclHeader:
    # line contains the entire string of the line the decl was found on
    line: str
    # declName is just the identifier name
    declName: str
    # fields contains a list of all the names of the fields in the structure
    fields: List[str]

    def __init__(self, line_p, declName_p, fields_p):
        self.line = line_p
        self.declName = declName_p
        self.fields = fields_p

enumNames: List[DeclHeader] = []
structureNames: List[DeclHeader] = []


def parseCmdArgs():
    if (len(sys.argv) != 3):
        sys.exit("usage: autogen.py <path_to_MDFrameWork.h> <path_to_MDNodeFuncs.gen>")

    # usage: autogen.py <path_to_MDFrameWork.h> <path_to_MDNodeFuncs.gen>
    __MDFrameWorkFile__ = sys.argv[1]
    __genFile__         = sys.argv[2]

    if not os.path.isfile(__MDFrameWorkFile__):
        sys.exit("Could not find the file " + __MDFrameWorkFile__)

    __genDir__ = os.path.dirname(__genFile__)
    # Try to create the path to MDNodeFuncs.gen if it doesn't already exist.
    if not os.path.exists(__genDir__):
        try:
            os.makedirs(__genDir__)
        except OSError as err:
            # In case of a failure to create this directory, and the directory doesn't already exist.
            if err.errno != errno.EEXIST:
                sys.exit("Failed to create the directory " + __genDir__)

    return __MDFrameWorkFile__ , __genFile__


def storeVars(line, declHeader):
    vars = line.split()
    for i in range(1, len(vars)):
        res = vars[i].split(",")
        for j in range(0, len(res)):
            if(res[j] != '' and res[j]!= ';' and res[j].find("ModuleMetaData") == -1 and res[j].find("FunctionMetaData") == -1):
                if(i != len(vars)-1):
                    declHeader.fields.append(res[j] + ';')
                else:
                    if(j != len(res)-1):
                        declHeader.fields.append(res[j] + ";")
                    else:
                        declHeader.fields.append(res[j])


def extractVars(line, declHeader):
    vars = line.split()
    if any(ty in line for ty in ("std::vector", "std::map", "std::array", "MapVector")):
        declHeader.fields.append(vars[len(vars)-1])
        return
    if line.find("=") != -1:
        declHeader.fields.append(vars[vars.index("=")-1]+";")
    elif(line.find(",")) == -1:
        if len(vars) == 2:
            declHeader.fields.append(vars[1])
        else:
            if(len(vars) == 3):
                declHeader.fields.append(vars[len(vars)-1])
    else:
        storeVars(line, declHeader)


def extractEnumVal(line, declHeader):
    vars = line.split()
    if (len(vars) == 0) or (line.find("{") != -1):
        return

    val = vars[0]
    if val[-1] == ',':
        val = val[:-1]

    declHeader.fields.append(val)


def parseFile(fileName, insideIGCNameSpace):
    inputFile = None
    try:
        inputFile = open(fileName, 'r')
    except:
        sys.exit("Failed to open the file " + fileName)

    # with statement automatically closes the file
    with inputFile as file:
        pcount = 0
        for line in file:
            line = line.split("//")[0]

            if line.find("namespace IGC") != -1:
                while line.find("{") == -1:
                    line = next(file, None)
                insideIGCNameSpace = True
                pcount += 1
                #
            if insideIGCNameSpace:
                blockType = re.search("struct|enum", line)
                if blockType:
                    words = line.split()
                    idx = 2 if 'class' in words else 1
                    foundDecl = DeclHeader(line, words[idx], [])
                    opcount = pcount
                    namesList = structureNames
                    extractFunc = extractVars
                    if blockType[0] == 'enum':
                        namesList = enumNames
                        extractFunc = extractEnumVal
                    while True:
                        line = next(file, None)
                        assert line, "EOF reached with unclosed enum or struct, check " + fileName + " formatting"
                        assert re.search("/\*", line) == None, "Multi-line comments are not supported yet"
                        line = line.split("//")[0]
                        pcount += line.count("{") - line.count("}")
                        if pcount <= opcount:
                            break
                        extractFunc(re.sub("{|}","", line), foundDecl)
                    assert pcount == opcount, "Unexpected struct/enum ending, check " + fileName + " formatting"
                    namesList.append(foundDecl)
                elif line.lstrip().startswith("#include") == True:
                    words = line.split()
                    parent_dir = os.path.dirname(fileName)
                    include_file = words[1][1:-1]  # cut off the "" or <> surrounding the file name
                    include_file_path = os.path.join(parent_dir, include_file)
                    parseFile(include_file_path, True)
                elif line.find("}") != -1 and line.find("};") == -1:
                    insideIGCNameSpace = False
                    pcount -= 1
        assert pcount == 0, "EOF reached, with unclosed IGC namespace, check " + fileName + " formatting"

def printStructCalls(structDecl, outputFile):
    outputFile.write("    Metadata* v[] = \n")
    outputFile.write("    { \n")
    outputFile.write("        MDString::get(module->getContext(), name),\n")
    for item in structDecl.fields:
        item = item[:-1]
        p =  '"{}"'.format(item)
        outputFile.write("        CreateNode(" + structDecl.declName + "Var" + "." + item + ", module, ")
        outputFile.write(p)
        outputFile.write("),\n")
    outputFile.write("    };\n")
    outputFile.write("    MDNode* node = MDNode::get(module->getContext(), v);\n")
    outputFile.write("    return node;\n")


def printEnumCalls(enumDecl, outputFile):
    outputFile.write("    StringRef enumName;\n")
    outputFile.write("    switch("+ enumDecl.declName + "Var)\n")
    outputFile.write("    {\n")
    for item in enumDecl.fields:
        outputFile.write("        case IGC::" + enumDecl.declName + "::" + item + ":\n"  )
        outputFile.write("            enumName = ")
        outputFile.write("\"" + item + "\"")
        outputFile.write(";\n")
        outputFile.write("            break;\n" )
    outputFile.write("    }\n")
    outputFile.write("    Metadata* v[] = \n")
    outputFile.write("    { \n")
    outputFile.write("        MDString::get(module->getContext(), name),\n")
    outputFile.write("        MDString::get(module->getContext(), enumName),\n")
    outputFile.write("    };\n")
    outputFile.write("    MDNode* node = MDNode::get(module->getContext(), v);\n")
    outputFile.write("    return node;\n")


def printStructReadCalls(structDecl, outputFile):
     for item in structDecl.fields:
        item = item[:-1]
        p =  '"{}"'.format(item)
        outputFile.write("    readNode(" + structDecl.declName + "Var" + "." + item + ", node , ")
        outputFile.write(p)
        outputFile.write(");\n")

def printEnumReadCalls(enumDecl, outputFile):
    outputFile.write("    StringRef s = cast<MDString>(node->getOperand(1))->getString();\n")
    outputFile.write("    std::string str = s.str();\n")
    outputFile.write("    "+ enumDecl.declName + "Var = (IGC::" + enumDecl.declName + ")(0);\n")

    for item in enumDecl.fields:
        outputFile.write("    if((str.size() == sizeof(\""+ item + "\")-1) && (::memcmp(str.c_str(),")
        outputFile.write("\"" + item + "\"")
        outputFile.write(",str.size())==0))\n")
        outputFile.write("    {\n")
        outputFile.write("            "+ enumDecl.declName + "Var = IGC::" + enumDecl.declName + "::" + item + ";\n")
        outputFile.write("    } else\n")

    outputFile.write("    {\n")
    outputFile.write("            "+ enumDecl.declName + "Var = (IGC::" + enumDecl.declName + ")(0);\n")
    outputFile.write("    }\n")


def emitCodeBlock(names: List[DeclHeader], declType, fmtFn, printFn, outputFile):
    for item in names:
        outputFile.write(fmtFn(item.declName))
        outputFile.write("{\n")
        printFn(item, outputFile)
        outputFile.write("}\n\n")


def emitEnumCreateNode(outputFile):
    def fmtFn(item):
        return f"MDNode* CreateNode(IGC::{item} {item}Var, Module* module, StringRef name)\n"
    emitCodeBlock(enumNames, "enum", fmtFn, printEnumCalls, outputFile)

def emitStructCreateNode(outputFile):
    def fmtFn(item):
        return "MDNode* CreateNode(const IGC::" + item + "& " + item + "Var" +", Module* module, StringRef name)\n"
    emitCodeBlock(structureNames, "struct", fmtFn, printStructCalls, outputFile)

def emitEnumReadNode(outputFile):
    def fmtFn(item):
        return "void readNode( IGC::" + item + " &" + item + "Var," + " MDNode* node)\n"
    emitCodeBlock(enumNames, "enum", fmtFn, printEnumReadCalls, outputFile)

def emitStructReadNode(outputFile):
    def fmtFn(item):
        return "void readNode( IGC::" + item + " &" + item + "Var," + " MDNode* node)\n"
    emitCodeBlock(structureNames, "struct", fmtFn, printStructReadCalls, outputFile)


def genCode(fileName):
    outputFile = None
    try:
        outputFile = open(fileName, 'w')
    except:
        sys.exit("Failed to open the file " + fileName)

    emitEnumCreateNode(outputFile)
    emitStructCreateNode(outputFile)
    emitEnumReadNode(outputFile)
    emitStructReadNode(outputFile)

    outputFile.close()


if __name__ == '__main__':
    __MDFrameWorkFile__ , __genFile__ = parseCmdArgs()
    parseFile(__MDFrameWorkFile__, False)
    genCode(__genFile__)