| 1 | """handle riesling frontend's '-x config' option |
| 2 | """ |
| 3 | |
| 4 | import re |
| 5 | |
| 6 | PROMPT = '@SIM_set_prompt' |
| 7 | DIAG_CONF = 'read-configuration' |
| 8 | MEM_IMAGE = 'load-veri-file' |
| 9 | IGNORE_SPARC = 'ignore_sparc' |
| 10 | AT_DEF = '@def' |
| 11 | CMD = 'cmd' |
| 12 | NEW_CMD = '@new_command' |
| 13 | AT_CONF = '@conf' |
| 14 | |
| 15 | |
| 16 | class ReadConfigRies: |
| 17 | """handle -x config option |
| 18 | """ |
| 19 | |
| 20 | def __init__ (self): |
| 21 | """ |
| 22 | """ |
| 23 | self.data = { } |
| 24 | self.data[PROMPT] = '' |
| 25 | self.data[DIAG_CONF] = '' |
| 26 | self.data[MEM_IMAGE] = '' |
| 27 | self.data[AT_DEF] = { } |
| 28 | self.data[CMD] = [ ] |
| 29 | self.data[IGNORE_SPARC] = [ ] |
| 30 | self.data[NEW_CMD] = { } |
| 31 | self.data[AT_CONF] = [ ] |
| 32 | |
| 33 | self.initKeyword() |
| 34 | |
| 35 | |
| 36 | def __str__ (self): |
| 37 | """ |
| 38 | """ |
| 39 | buffer = [ '-----ReadConfigRies.py-----\n' ] |
| 40 | buffer.append('%s = %s\n' % (PROMPT, self.data[PROMPT])) |
| 41 | buffer.append('%s = %s\n' % (DIAG_CONF, self.data[DIAG_CONF])) |
| 42 | buffer.append('%s = %s\n' % (MEM_IMAGE, self.data[MEM_IMAGE])) |
| 43 | buffer.append('%s = %s\n' % (IGNORE_SPARC, self.data[IGNORE_SPARC])) |
| 44 | buffer.append('---%s---\n' % (AT_DEF)) |
| 45 | for key in self.data[AT_DEF].keys(): |
| 46 | buffer.append(''.join(self.data[AT_DEF][key])) |
| 47 | buffer.append('---%s---\n' % (NEW_CMD)) |
| 48 | for key in self.data[NEW_CMD].keys(): |
| 49 | buffer.append(''.join(self.data[NEW_CMD][key])) |
| 50 | buffer.append('---%s---\n' % (CMD)) |
| 51 | buffer.append(''.join(self.data[CMD])) |
| 52 | |
| 53 | return ''.join(buffer) |
| 54 | |
| 55 | |
| 56 | def initKeyword (self): |
| 57 | """ |
| 58 | """ |
| 59 | self.keywords = { } |
| 60 | self.keywords[re.compile('^@num_processors')] = self.ignore |
| 61 | self.keywords[re.compile('^\$sim_no_magic_breakpoint')] = self.ignore |
| 62 | self.keywords[re.compile('^@SIM_set_prompt')] = self.handlePrompt |
| 63 | self.keywords[re.compile('^//')] = self.ignore |
| 64 | self.keywords[re.compile('^read-configuration')] = self.handleReadConfig |
| 65 | self.keywords[re.compile('^load-kernel')] = self.handleLoadMem |
| 66 | self.keywords[re.compile('^load-veri-file')] = self.handleLoadMem |
| 67 | self.keywords[re.compile('^@init_swerver')] = self.ignore |
| 68 | self.keywords[re.compile('^ciop\d+\.cmd')] = self.ignore |
| 69 | self.keywords[re.compile('^ciop\d+\.setvar')] = self.ignore |
| 70 | self.keywords[re.compile('^pdisable')] = self.handlePdisable |
| 71 | #self.keywords[re.compile('^@conf\.spu\d+\.spu_ignore_asi_check')] = self.ignore |
| 72 | self.keywords[re.compile('^@?def\s+')] = self.handleAtDef |
| 73 | self.keywords[re.compile('^@?new_command\s*\(')] = self.handleNewcmd |
| 74 | #self.keywords[re.compile('^@conf\.mom\d+\.setvar')] = self.ignore |
| 75 | #self.keywords[re.compile('^@conf\.mom\d+\.call')] = self.ignore |
| 76 | #self.keywords[re.compile('^@conf\.mom\d+\.thread_status')] = self.ignore |
| 77 | #self.keywords[re.compile('^@conf\.mom\d+\.PASS')] = self.ignore |
| 78 | #self.keywords[re.compile('^@conf\.mom\d+\.FAIL')] = self.ignore |
| 79 | #self.keywords[re.compile('^@conf\.mom\d+\.HPASS')] = self.ignore |
| 80 | #self.keywords[re.compile('^@conf\.mom\d+\.HFAIL')] = self.ignore |
| 81 | #self.keywords[re.compile('^@conf\.mom\d+\.DC_ON')] = self.ignore |
| 82 | #self.keywords[re.compile('^@conf\.mom\d+\.DC_OFF')] = self.ignore |
| 83 | #self.keywords[re.compile('^@conf\.mom\d+\.IC_ON')] = self.ignore |
| 84 | #self.keywords[re.compile('^@conf\.mom\d+\.IC_OFF')] = self.ignore |
| 85 | #self.keywords[re.compile('^@conf\.mom\d+\.APASS')] = self.ignore |
| 86 | #self.keywords[re.compile('^@conf\.mom\d+\.AFAIL')] = self.ignore |
| 87 | #self.keywords[re.compile('^@conf\.mom\d+\.start_cycle')] = self.ignore |
| 88 | #self.keywords[re.compile('^@conf\.mom\d+\.reset_all_stat')] = self.ignore |
| 89 | #self.keywords[re.compile('^@conf\.mom\d+\.T1PASS')] = self.ignore |
| 90 | #self.keywords[re.compile('^@conf\.mom\d+\.T1FAIL')] = self.ignore |
| 91 | self.keywords[re.compile('^run')] = self.handleRun |
| 92 | self.keywords[re.compile('^quit')] = self.handleQuit |
| 93 | #self.keywords[re.compile('^@conf\.swvmem\d+\.ignore_sparc')] = self.handleIgnoreSparc |
| 94 | #self.keywords[re.compile('^@conf\.swvmem\d+\.printf')] = self.ignore |
| 95 | #self.keywords[re.compile('^@conf\.swvmem\d+\.thread_status')] = self.ignore |
| 96 | self.keywords[re.compile('^th\d+\.write-reg')] = self.handleWriteReg |
| 97 | self.keywords[re.compile('^th\d+\.read-reg')] = self.handleReadReg |
| 98 | #self.keywords[re.compile('^@conf\.swvmem\d+\.good_trap')] = self.handleTrap |
| 99 | #self.keywords[re.compile('^@conf\.swvmem\d+\.bad_trap')] = self.handleTrap |
| 100 | #self.keywords[re.compile('^@conf\.swvmem\d+\.max_cycle')] = self.ignore |
| 101 | self.keywords[re.compile('^pli-run')] = self.handlePlirun |
| 102 | self.keywords[re.compile('^@.*riesling\.sys\.')] = self.handleRiesling |
| 103 | self.keywords[re.compile('^@conf\.')] = self.handleAtConf |
| 104 | |
| 105 | self.klist = self.keywords.keys() |
| 106 | |
| 107 | |
| 108 | def ignore (self, line): |
| 109 | """ |
| 110 | """ |
| 111 | self.fdtmp.write(line) |
| 112 | return None |
| 113 | |
| 114 | |
| 115 | def arbitrary (self, line): |
| 116 | """ |
| 117 | """ |
| 118 | # even though we may not know what to do with this line, we will |
| 119 | # try to execute it, and ignore any exception it may raise. |
| 120 | if line.startswith('@'): |
| 121 | line = line[1:] |
| 122 | self.data[CMD].append(line) |
| 123 | return None |
| 124 | |
| 125 | |
| 126 | def handlePrompt (self, line): |
| 127 | """ |
| 128 | """ |
| 129 | lindex = line.find('"') |
| 130 | rindex = line.find('"', lindex+1) |
| 131 | self.data[PROMPT] = line[lindex+1:rindex] |
| 132 | return None |
| 133 | |
| 134 | |
| 135 | def handleReadConfig (self, line): |
| 136 | """ |
| 137 | """ |
| 138 | lindex = line.find('"') |
| 139 | rindex = line.find('"', lindex+1) |
| 140 | self.data[DIAG_CONF] = line[lindex+1:rindex] |
| 141 | return None |
| 142 | |
| 143 | |
| 144 | def handleLoadMem (self, line): |
| 145 | """ |
| 146 | """ |
| 147 | self.data[MEM_IMAGE] = line.split()[1] |
| 148 | return None |
| 149 | |
| 150 | |
| 151 | def handlePdisable (self, line): |
| 152 | """ |
| 153 | """ |
| 154 | self.data[CMD].append(line) |
| 155 | return None |
| 156 | |
| 157 | |
| 158 | def handleAtDef (self, line): |
| 159 | """ |
| 160 | """ |
| 161 | # @def get_addr(name): |
| 162 | # def get_addr(name): |
| 163 | if line.startswith('@'): |
| 164 | line = line[1:] |
| 165 | tokens = line.split() |
| 166 | index = tokens[1].find('(') |
| 167 | fname = tokens[1][:index].strip() |
| 168 | if self.data[AT_DEF].has_key(fname): |
| 169 | raise RuntimeError, 'function %s is already defined' % (fname) |
| 170 | else: |
| 171 | self.data[AT_DEF][fname] = [ line ] |
| 172 | line = self.fd.readline() |
| 173 | # process until encounter a line starting with non-blank char |
| 174 | # at the 1st column |
| 175 | while re.match('^\s', line): |
| 176 | self.data[AT_DEF][fname].append(line) |
| 177 | line = self.fd.readline() |
| 178 | |
| 179 | return line |
| 180 | |
| 181 | |
| 182 | def handleNewcmd (self, line): |
| 183 | """ |
| 184 | """ |
| 185 | # @new_command(...) |
| 186 | # new_command(...) |
| 187 | if line.startswith('@'): |
| 188 | line = line[1:] |
| 189 | lindex = line.find('"') |
| 190 | rindex = line.find('"', lindex+1) |
| 191 | cmd = line[lindex+1:rindex] |
| 192 | if self.data[NEW_CMD].has_key(cmd): |
| 193 | raise RuntimeError, 'command %s is already defined' % (cmd) |
| 194 | else: |
| 195 | self.data[NEW_CMD][cmd] = [ line ] |
| 196 | line = self.fd.readline() |
| 197 | while line.strip(): |
| 198 | self.data[NEW_CMD][cmd].append(line) |
| 199 | line = self.fd.readline() |
| 200 | return None |
| 201 | |
| 202 | |
| 203 | def handleRun (self, line): |
| 204 | """ |
| 205 | """ |
| 206 | self.data[CMD].append(line) |
| 207 | return None |
| 208 | |
| 209 | |
| 210 | def handleQuit (self, line): |
| 211 | """ |
| 212 | """ |
| 213 | self.data[CMD].append(line) |
| 214 | return None |
| 215 | |
| 216 | |
| 217 | def handleIgnoreSparc (self, line): |
| 218 | """ |
| 219 | @conf.swvmem0.ignore_sparc = 0 |
| 220 | """ |
| 221 | i = line.find('=') |
| 222 | id = line[i+1:].split()[0] |
| 223 | self.data[IGNORE_SPARC].append(id) |
| 224 | return None |
| 225 | |
| 226 | |
| 227 | def handleThreadMask (self, line): |
| 228 | """ |
| 229 | # @conf.swvmem0.thread_mask0 = THREAD_MASK |
| 230 | # @conf.swvmem0.thread_mask1 = THREAD_MASK1 |
| 231 | |
| 232 | use THREAD_MASK to produce THREADS and SPx (for IGNORE_SPARC) |
| 233 | """ |
| 234 | MAX_CORE = 8 |
| 235 | |
| 236 | ii = line.find('thread_mask') + len('thread_mask') |
| 237 | cpu = int(line[ii:ii+1]) |
| 238 | tokens = line.replace('=', ' ').split() |
| 239 | tokens[1] = tokens[1].lower() |
| 240 | if tokens[1].startswith('0x'): |
| 241 | tokens[1] = tokens[1][2:] |
| 242 | # make sure we have even number of characters, for ease process. |
| 243 | if len(tokens[1])%2 != 0: |
| 244 | if tokens[1][0] == 'x': |
| 245 | tokens[1] = 'x' + tokens[1] |
| 246 | else: |
| 247 | tokens[1] = '0' + tokens[1] |
| 248 | |
| 249 | # produce IGNORE_SPARC |
| 250 | # thread_mask = 00xx11xx, each core is presented by two characters |
| 251 | # (8 bits, one for each strand). A 'xx' mean this core should be |
| 252 | # disabled, a 'not-mentioned' core is also disabled, otherwise it is |
| 253 | # enabled. |
| 254 | for i in range(MAX_CORE): |
| 255 | if ((2*(i+1) > len(tokens[1])) or |
| 256 | (tokens[1][(-2*i)-1] == 'x' and tokens[1][(-2*i)-2] == 'x')): |
| 257 | coreid = i + (cpu * MAX_CORE) |
| 258 | self.data[IGNORE_SPARC].append(coreid) |
| 259 | self.data[AT_CONF].append('@conf.swvmem0.ignore_sparc =+ %d' % (coreid)) |
| 260 | if (2*i < len(tokens[1])): |
| 261 | if (tokens[1][2*i] == 'x') and (tokens[1][(2*i)+1] != 'x'): |
| 262 | if i == 0: |
| 263 | tokens[1] = '0' + tokens[1][1:] |
| 264 | else: |
| 265 | tokens[1] = tokens[1][:2*i] + '0' + tokens[1][(2*i)+1:] |
| 266 | elif (tokens[1][2*i] != 'x') and (tokens[1][(2*i)+1] == 'x'): |
| 267 | if 2*(i+1) >= len(tokens[1]): |
| 268 | tokens[1] = tokens[1][:-1] + '0' |
| 269 | else: |
| 270 | tokens[1] = tokens[1][:(2*i)+1] + '0' + tokens[1][2*(i+1):] |
| 271 | |
| 272 | # produce THREADS |
| 273 | self.data[AT_CONF].append('@conf.swvmem0.threads%d = %s' % (cpu, tokens[1].replace('x', '0'))) |
| 274 | |
| 275 | # we don't really use thread_mask to set CMP registers, instead we |
| 276 | # use the threads and ignore_sparc produced by thread_mask to do that. |
| 277 | # this is mainly for backward compatibility. |
| 278 | |
| 279 | # keep the revised line to be added to list |
| 280 | line = ' = '.join(tokens) |
| 281 | |
| 282 | return line |
| 283 | |
| 284 | |
| 285 | def handleWriteReg (self, line): |
| 286 | """ |
| 287 | th00.write-reg reg-name = ccr 0x00 |
| 288 | """ |
| 289 | # just to make sure there are space before/after the = sign |
| 290 | tokens = line.replace('=', ' = ').split() |
| 291 | cmd = '%s %s %s\n' % (tokens[0], tokens[3], tokens[4]) |
| 292 | self.data[CMD].append(cmd) |
| 293 | return None |
| 294 | |
| 295 | |
| 296 | def handleReadReg (self, line): |
| 297 | """ |
| 298 | th00.read-reg reg-name = ccr |
| 299 | """ |
| 300 | # just to make sure there are space before/after the = sign |
| 301 | tokens = line.replace('=', ' = ').split() |
| 302 | cmd = '%s %s\n' % (tokens[0], tokens[3]) |
| 303 | self.data[CMD].append(cmd) |
| 304 | return None |
| 305 | |
| 306 | |
| 307 | def handleTrap (self, line): |
| 308 | """ |
| 309 | @conf.swvmem0.good_trap = get_addr('\.HTRAPS\.HT0_GoodTrap_0x100') |
| 310 | @conf.swvmem0.bad_trap = get_addr('\.TRAPS\.T0_BadTrap_0x101') |
| 311 | """ |
| 312 | lindex = line.find('get_addr') |
| 313 | lindex = line.find("'", lindex) |
| 314 | rindex = line.find("'", lindex+1) |
| 315 | symbol = line[lindex+1:rindex] |
| 316 | symbol = symbol.replace('\\', '') |
| 317 | # convert it into a break command |
| 318 | cmd = 'break &%s' % (symbol) |
| 319 | self.data[CMD].append(cmd) |
| 320 | return None |
| 321 | |
| 322 | |
| 323 | def handlePlirun (self, line): |
| 324 | """ |
| 325 | """ |
| 326 | self.data[CMD].append(line) |
| 327 | return None |
| 328 | |
| 329 | |
| 330 | def handleRiesling (self, line): |
| 331 | """allow riesling frontend native calls in configuration file |
| 332 | @riesling.sys.frontend-native-call, e.g., |
| 333 | @riesling.sys.cpu0.core0.strand0.archstate.getPc() |
| 334 | """ |
| 335 | self.data[CMD].append(line[1:]) |
| 336 | return None |
| 337 | |
| 338 | |
| 339 | def handleAtConf (self, line): |
| 340 | """ |
| 341 | """ |
| 342 | if line.find('ignore_sparc') > -1: |
| 343 | self.handleIgnoreSparc(line) |
| 344 | elif line.find('good_trap') > -1 or line.find('bad_trap') > -1: |
| 345 | self.handleTrap(line) |
| 346 | elif line.startswith('@conf.swvmem0.thread_mask'): |
| 347 | # @conf.swvmem0.thread_mask = THREAD_MASK |
| 348 | line = self.handleThreadMask(line) |
| 349 | ## elif line.startswith('@conf.swvmem0.cpu'): |
| 350 | ## # @conf.swvmem0.cpu = 1 |
| 351 | ## tokens = line.replace('=', ' ').split() |
| 352 | ## cpu = int(tokens[1]) |
| 353 | ## if cpu < 1: |
| 354 | ## cpu = 1 |
| 355 | ## elif cpu > 4: |
| 356 | ## cpu = 4 |
| 357 | ## tokens[1] = str(cpu) |
| 358 | ## line = ' = '.join(tokens) |
| 359 | |
| 360 | # keep all @conf.zzz statements in AT_CONF |
| 361 | self.data[AT_CONF].append(line) |
| 362 | |
| 363 | # @conf.mom0's, which is used by blaze/mom, can mix with other |
| 364 | # commands, e.g., |
| 365 | # @conf.mom0.call= "init-anno-sas" |
| 366 | # @conf.mom0.setvar= "itlb0_size_v=0" |
| 367 | # @conf.mom0.PASS= get_addr('\.TRAPS\.T0_GoodTrap_0x100') |
| 368 | # @conf.mom0.thread_status= THREAD_STATUS_ADDR |
| 369 | # @conf.mom0.start_cycle= 1 |
| 370 | # run 200 |
| 371 | # @conf.mom0.reset_all_stat= 1 |
| 372 | # their execution order is important, so we have to keep @conf.mom0 |
| 373 | # as part of CMD, blaze use 'mom setvar' as keyword |
| 374 | CONF_MOM = '@conf.mom0.' |
| 375 | CONF_MOM_SET = '@conf.mom0.setvar' |
| 376 | sline = line.strip() |
| 377 | if sline.startswith(CONF_MOM): |
| 378 | sline = sline[len(CONF_MOM):] |
| 379 | sline = sline.replace('=', ' ').replace('"', ' ') |
| 380 | tokens = sline.split() |
| 381 | cmd = 'mom %s\n' % (' '.join(tokens)) |
| 382 | self.data[CMD].append(cmd) |
| 383 | return None |
| 384 | |
| 385 | ## CONF_MOM = '@conf.mom0.' |
| 386 | ## CONF_MOM_SET = '@conf.mom0.setvar' |
| 387 | ## sline = line.strip() |
| 388 | ## if sline.startswith(CONF_MOM): |
| 389 | ## if sline.startswith(CONF_MOM_SET): |
| 390 | ## lindex = sline.find('"') |
| 391 | ## rindex = sline.find('"', lindex+1) |
| 392 | ## setcmd = sline[lindex+1:rindex] |
| 393 | ## else: |
| 394 | ## setcmd = sline[len(CONF_MOM):] |
| 395 | |
| 396 | ## index = setcmd.find('=') |
| 397 | ## lop = setcmd[0:index].strip() |
| 398 | ## rop = setcmd[index+1:].strip() |
| 399 | ## cmd = 'mom setvar %s %s\n' % (lop, rop) |
| 400 | ## self.data[CMD].append(cmd) |
| 401 | ## return None |
| 402 | |
| 403 | |
| 404 | def readConfig (self, fname): |
| 405 | """ |
| 406 | """ |
| 407 | try: |
| 408 | TMP = '.rs_config_ignore' |
| 409 | self.fdtmp = open(TMP, 'w') |
| 410 | self.fd = open(fname, 'r') |
| 411 | line = self.fd.readline() |
| 412 | while line: |
| 413 | sline = line.strip() |
| 414 | rline = None |
| 415 | if sline: |
| 416 | match = 0 |
| 417 | for key in self.klist: |
| 418 | if key.match(sline): |
| 419 | rline = self.keywords[key](line) |
| 420 | match = 1 |
| 421 | break # out of for loop |
| 422 | if match == 0: |
| 423 | # if cannot find a match, handle it as arbitrary |
| 424 | # statement |
| 425 | self.arbitrary(line) |
| 426 | |
| 427 | # next line |
| 428 | if rline != None: |
| 429 | line = rline |
| 430 | else: |
| 431 | line = self.fd.readline() |
| 432 | |
| 433 | self.fdtmp.close() |
| 434 | self.fd.close() |
| 435 | except: |
| 436 | raise |
| 437 | |
| 438 | |
| 439 | """self-testing |
| 440 | """ |
| 441 | if __name__ == "__main__": |
| 442 | """ |
| 443 | """ |
| 444 | # unit test here |
| 445 | import sys |
| 446 | reader = ReadConfigRies() |
| 447 | reader.readConfig(sys.argv[1]) |
| 448 | print reader |