clang  19.0.0git
gen-msp430-def.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # ===----------------------------------------------------------------------===##
3 #
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 #
8 # ===----------------------------------------------------------------------===##
9 """
10 Script to generate MSP430 definitions from TI's devices.csv
11 
12 Download the devices.csv from [1] using the link "Header and Support Files".
13 
14 [1]: https://www.ti.com/tool/MSP430-GCC-OPENSOURCE#downloads
15 """
16 import csv
17 import sys
18 
19 DEVICE_COLUMN = 0
20 MULTIPLIER_COLUMN = 3
21 
22 MULTIPLIER_SW = "0"
23 MULTIPLIER_HW_16 = ("1", "2")
24 MULTIPLIER_HW_32 = ("4", "8")
25 
26 PREFIX = """//===--- MSP430Target.def - MSP430 Feature/Processor Database----*- C++ -*-===//
27 //
28 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
29 // See https://llvm.org/LICENSE.txt for license information.
30 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
31 //
32 //===----------------------------------------------------------------------===//
33 //
34 // This file defines the MSP430 devices and their features.
35 //
36 // Generated from TI's devices.csv in version {} using the script in
37 // Target/MSP430/gen-msp430-def.py - use this tool rather than adding
38 // new MCUs by hand.
39 //
40 //===----------------------------------------------------------------------===//
41 
42 #ifndef MSP430_MCU_FEAT
43 #define MSP430_MCU_FEAT(NAME, HWMULT) MSP430_MCU(NAME)
44 #endif
45 
46 #ifndef MSP430_MCU
47 #define MSP430_MCU(NAME)
48 #endif
49 
50 """
51 
52 SUFFIX = """
53 // Generic MCUs
54 MSP430_MCU("msp430i2xxgeneric")
55 
56 #undef MSP430_MCU
57 #undef MSP430_MCU_FEAT
58 """
59 
60 
61 def csv2def(csv_path, def_path):
62  """
63  Parse the devices.csv file at the given path, generate the definitions and
64  write them to the given path.
65 
66  :param csv_path: Path to the devices.csv to parse
67  :type csv_path: str
68  :param def_path: Path to the output file to write the definitions to
69  "type def_path: str
70  """
71 
72  mcus_multiplier_sw = []
73  mcus_multiplier_hw_16 = []
74  mcus_multiplier_hw_32 = []
75  version = "unknown"
76 
77  with open(csv_path) as csv_file:
78  csv_reader = csv.reader(csv_file)
79  while True:
80  row = next(csv_reader)
81  if len(row) < MULTIPLIER_COLUMN:
82  continue
83 
84  if row[DEVICE_COLUMN] == "# Device Name":
85  assert row[MULTIPLIER_COLUMN] == "MPY_TYPE", "File format changed"
86  break
87 
88  if row[0] == "Version:":
89  version = row[1]
90 
91  for row in csv_reader:
92  if row[DEVICE_COLUMN].endswith("generic"):
93  continue
94  if row[MULTIPLIER_COLUMN] == MULTIPLIER_SW:
95  mcus_multiplier_sw.append(row[DEVICE_COLUMN])
96  elif row[MULTIPLIER_COLUMN] in MULTIPLIER_HW_16:
97  mcus_multiplier_hw_16.append(row[DEVICE_COLUMN])
98  elif row[MULTIPLIER_COLUMN] in MULTIPLIER_HW_32:
99  mcus_multiplier_hw_32.append(row[DEVICE_COLUMN])
100  else:
101  assert 0, "Unknown multiplier type"
102 
103  with open(def_path, "w") as def_file:
104  def_file.write(PREFIX.format(version))
105 
106  for mcu in mcus_multiplier_sw:
107  def_file.write(f'MSP430_MCU("{mcu}")\n')
108 
109  def_file.write("\n// With 16-bit hardware multiplier\n")
110 
111  for mcu in mcus_multiplier_hw_16:
112  def_file.write(f'MSP430_MCU_FEAT("{mcu}", "16bit")\n')
113 
114  def_file.write("\n// With 32-bit hardware multiplier\n")
115 
116  for mcu in mcus_multiplier_hw_32:
117  def_file.write(f'MSP430_MCU_FEAT("{mcu}", "32bit")\n')
118 
119  def_file.write(SUFFIX)
120 
121 
122 if __name__ == "__main__":
123  if len(sys.argv) != 3:
124  sys.exit(f"Usage: {sys.argv[0]} <CSV_FILE> <DEF_FILE>")
125 
126  csv2def(sys.argv[1], sys.argv[2])
def csv2def(csv_path, def_path)