Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # ========== Copyright Header Begin ========================================== |
2 | # | |
3 | # OpenSPARC T2 Processor File: Pfe_Assembler.py | |
4 | # Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | # | |
7 | # The above named program is free software; you can redistribute it and/or | |
8 | # modify it under the terms of the GNU General Public | |
9 | # License version 2 as published by the Free Software Foundation. | |
10 | # | |
11 | # The above named program is distributed in the hope that it will be | |
12 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | # General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public | |
17 | # License along with this work; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | # | |
20 | # ========== Copyright Header End ============================================ | |
21 | ||
22 | import os | |
23 | import tempfile | |
24 | ||
25 | as = "/usr/ccs/bin/as" | |
26 | dis = "/usr/ccs/bin/dis" | |
27 | grep = "/bin/egrep" | |
28 | ||
29 | class AssemblerError(Exception): | |
30 | def __init__(self,*args): | |
31 | Exception.__init__(self,args) | |
32 | ||
33 | ||
34 | def asm(instr): | |
35 | """ | |
36 | asm() takes a string of assembly code and produces a list of | |
37 | instruction words. When asm() is passed a list of strings it | |
38 | will produce a list of list of instruction words. For example | |
39 | asm('done;nop') returns [0x81f00000,0x1000000] and | |
40 | asm(['done','nop']) returns [[0x81f00000],[0x1000000]]. | |
41 | """ | |
42 | if type(instr) == list: | |
43 | result = [] | |
44 | [result.append(asm_instr(i)) for i in instr] | |
45 | else: | |
46 | result = asm_instr(instr) | |
47 | return result | |
48 | ||
49 | ||
50 | def asm_instr(instr): | |
51 | s_list = [] | |
52 | ||
53 | s_name = tempfile.mktemp(suffix=".s") | |
54 | o_name = tempfile.mktemp(suffix=".o") | |
55 | d_name = tempfile.mktemp(suffix=".d") | |
56 | e_name = tempfile.mktemp(suffix=".2") | |
57 | t_name = tempfile.mktemp(suffix=".t") | |
58 | ||
59 | s_file = open(s_name,'w') | |
60 | s_file.writelines([ | |
61 | ".register %g2,#scratch\n", | |
62 | ".register %g3,#scratch\n", | |
63 | ".register %g6,#scratch\n", | |
64 | ".register %g7,#scratch\n", | |
65 | "main:\n", | |
66 | instr]) | |
67 | s_file.write("\n") | |
68 | s_file.close() | |
69 | ||
70 | s_exec = "%s -xarch=v9c -o %s %s 2> %s" % (as,o_name,s_name,e_name) | |
71 | if os.system(s_exec) == 0: | |
72 | d_exec = "%s %s | /bin/awk '{print $2$3$4$5}' | %s '^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' > %s 2> /dev/null" % (dis,o_name,grep,d_name) | |
73 | if os.system(d_exec) == 0: | |
74 | d_file = open(d_name) | |
75 | s_list = [int(nr,16) for nr in d_file.readlines()] | |
76 | d_file.close() | |
77 | os.unlink(d_name) | |
78 | os.unlink(s_name) | |
79 | os.unlink(o_name) | |
80 | else: | |
81 | os.unlink(s_name) | |
82 | t_exec = "/bin/sed -e 's/[^:]*:[^:]*:\ //g' %s > %s" % (e_name,t_name) | |
83 | os.system(t_exec) | |
84 | t_file = open(t_name,'r') | |
85 | x = t_file.readlines() | |
86 | t_file.close() | |
87 | raise AssemblerError(x) | |
88 | return s_list | |
89 | ||
90 | ||
91 | if __name__ == '__main__': | |
92 | print asm("adx %g0, %g1, %g2") | |
93 | print asm("add %g4, %g5, %g3\nadd %g0, %g1, %g2") | |
94 | try: | |
95 | print asm(['add %g0, %g1, %o6', 'asd %g7, %g6, %g2']) | |
96 | except AssemblerError, e: | |
97 | print e | |
98 | ||
99 | ||
100 | ||
101 |