Add DW_LANG to generated dwarf.py

While we're here, make generate_dwarf_constants.py use the bundled
dwarf.h, generate code that black is happy with, and use the keyword
list from the standard library.
This commit is contained in:
Omar Sandoval 2020-02-25 14:56:10 -08:00
parent 8be7ae5299
commit fe42a71116
2 changed files with 59 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3
import keyword
import re
@ -8,6 +9,7 @@ prefixes = [
"DW_ATE",
"DW_CHILDREN",
"DW_FORM",
"DW_LANG",
"DW_LNE",
"DW_LNS",
"DW_OP",
@ -15,7 +17,7 @@ prefixes = [
]
if __name__ == "__main__":
with open("/usr/include/dwarf.h", "r") as f:
with open("libdrgn/elfutils/libdw/dwarf.h", "r") as f:
dwarf_h = f.read()
dwarf_h = re.sub(r"/\*.*?\*/", "", dwarf_h, flags=re.DOTALL)
dwarf_h = re.sub(r"\\\n", "", dwarf_h)
@ -50,9 +52,9 @@ from typing import Text
first = False
print(f"class {enum}(enum.IntEnum):")
for name, value in enums[enum]:
if name in ["import", "not", "and", "or"]:
if keyword.iskeyword(name):
name += "_"
print(f" {name} = 0x{value:x}", end="")
print(f" {name} = 0x{value:X}", end="")
if name == "name":
print(" # type: ignore")
else:
@ -61,6 +63,6 @@ from typing import Text
print(" @classmethod")
print(" def str(cls, value: int) -> Text:")
print(" try:")
print(f" return f'{enum}_{{cls(value).name}}'")
print(f' return f"{enum}_{{cls(value).name}}"')
print(" except ValueError:")
print(" return hex(value)")

View File

@ -178,6 +178,9 @@ class DW_AT(enum.IntEnum):
GNU_addr_base = 0x2133
GNU_pubnames = 0x2134
GNU_pubtypes = 0x2135
GNU_numerator = 0x2303
GNU_denominator = 0x2304
GNU_bias = 0x2305
hi_user = 0x3FFF
@classmethod
@ -288,6 +291,56 @@ class DW_FORM(enum.IntEnum):
return hex(value)
class DW_LANG(enum.IntEnum):
C89 = 0x1
C = 0x2
Ada83 = 0x3
C_plus_plus = 0x4
Cobol74 = 0x5
Cobol85 = 0x6
Fortran77 = 0x7
Fortran90 = 0x8
Pascal83 = 0x9
Modula2 = 0xA
Java = 0xB
C99 = 0xC
Ada95 = 0xD
Fortran95 = 0xE
PLI = 0xF
ObjC = 0x10
ObjC_plus_plus = 0x11
UPC = 0x12
D = 0x13
Python = 0x14
OpenCL = 0x15
Go = 0x16
Modula3 = 0x17
Haskell = 0x18
C_plus_plus_03 = 0x19
C_plus_plus_11 = 0x1A
OCaml = 0x1B
Rust = 0x1C
C11 = 0x1D
Swift = 0x1E
Julia = 0x1F
Dylan = 0x20
C_plus_plus_14 = 0x21
Fortran03 = 0x22
Fortran08 = 0x23
RenderScript = 0x24
BLISS = 0x25
lo_user = 0x8000
Mips_Assembler = 0x8001
hi_user = 0xFFFF
@classmethod
def str(cls, value: int) -> Text:
try:
return f"DW_LANG_{cls(value).name}"
except ValueError:
return hex(value)
class DW_LNE(enum.IntEnum):
end_sequence = 0x1
set_address = 0x2