asm.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import elf
  2. import sys
  3. pc = 0
  4. named_addresses={}
  5. exports = []
  6. memory = {}
  7. def directive(line):
  8. global pc
  9. global exports
  10. if line[0] == '.org':
  11. pc = int(line[1],0)
  12. memory[pc] = []
  13. if line[0] == '.export':
  14. if line[1] in named_addresses:
  15. exports.append(line[1])
  16. def label(line):
  17. global named_addresses
  18. name = line[0][:-1]
  19. named_addresses[name]=pc
  20. rtypes = {
  21. 'add': '0000000xxxxxxxxxx000xxxxx0110011',
  22. 'sub': '0100000xxxxxxxxxx000xxxxx0110011',
  23. 'sll': '0000000xxxxxxxxxx001xxxxx0110011',
  24. 'slt': '0000000xxxxxxxxxx010xxxxx0110011',
  25. 'sltu': '0000000xxxxxxxxxx011xxxxx0110011',
  26. 'xor': '0000000xxxxxxxxxx100xxxxx0110011',
  27. 'srl': '0000000xxxxxxxxxx101xxxxx0110011',
  28. 'sra': '0100000xxxxxxxxxx101xxxxx0110011',
  29. 'or': '0000000xxxxxxxxxx110xxxxx0110011',
  30. 'and': '0000000xxxxxxxxxx111xxxxx0110011',
  31. }
  32. utypes = {
  33. 'lui': 'xxxxxxxxxxxxxxxxxxxxddddd0110111',
  34. 'auipc': 'xxxxxxxxxxxxxxxxxxxxddddd0010111',
  35. }
  36. itypes = {
  37. 'addi': 'xxxxxxxxxxxxxxxxx000xxxxx0010011',
  38. 'slti': 'xxxxxxxxxxxxxxxxx010xxxxx0010011',
  39. 'sltiu': 'xxxxxxxxxxxxxxxxx011xxxxx0010011',
  40. 'xori': 'xxxxxxxxxxxxxxxxx100xxxxx0010011',
  41. 'ori': 'xxxxxxxxxxxxxxxxx110xxxxx0010011',
  42. 'andi': 'xxxxxxxxxxxxxxxxx111xxxxx0010011',
  43. 'slli': '00000xxxxxxxxxxxx001xxxxx0010011',
  44. 'srli': '00000xxxxxxxxxxxx101xxxxx0010011',
  45. 'srai': '01000xxxxxxxxxxxx101xxxxx0010011',
  46. }
  47. def name2reg(name):
  48. namelist = [
  49. 'zero','ra','sp','gp','tp','t0','t1','t2',
  50. 's0','s1','a0','a1','a2','a3','a4','a5',
  51. 'a6','a7','s2','s3','s4','s5','s6','s7',
  52. 's8','s9','s10','s11','t3','t4','t5','t6']
  53. if name[0] == 'x':
  54. return int(name[1:])
  55. return namelist.index(name)
  56. def rtype(line):
  57. global pc
  58. global memory
  59. global rtypes
  60. instr = [x for x in rtypes[line[0]]]
  61. rd = name2reg(line[1])
  62. rs1 = name2reg(line[2])
  63. rs2 = name2reg(line[3])
  64. for i in range(-8,-13,-1):
  65. instr[i] = str(rd&1)
  66. rd >>= 1
  67. for i in range(-16,-21,-1):
  68. instr[i] = str(rs1&1)
  69. rs1 >>= 1
  70. for i in range(-21,-26,-1):
  71. instr[i] = str(rs2&1)
  72. rs2 >>= 1
  73. instrb = int(''.join(instr),2)
  74. print(hex(instrb))
  75. pc += 4
  76. def utype(line):
  77. global pc
  78. global memory
  79. global utypes
  80. instr = [x for x in utypes[line[0]]]
  81. print(instr)
  82. def itype(line):
  83. global pc
  84. global memory
  85. global itypes
  86. instr = [x for x in itypes[line[0]]]
  87. rd = name2reg(line[1])
  88. rs1 = name2reg(line[2])
  89. imm = int(line[3])
  90. if imm < 0:
  91. imm = 4096 + imm
  92. for i in range(-21,-33,-1):
  93. instr[i] = str(imm & 1)
  94. imm >>= 1
  95. for i in range(-8,-13,-1):
  96. instr[i] = str(rd&1)
  97. rd >>= 1
  98. for i in range(-16,-21,-1):
  99. instr[i] = str(rs1&1)
  100. rs1 >>= 1
  101. instrb = int(''.join(instr),2)
  102. print(hex(instrb))
  103. pc += 4
  104. instrs = {
  105. 'lui': utype,
  106. 'auipc': utype,
  107. 'jal': utype,
  108. 'jalr': None,
  109. 'beq': None,
  110. 'bne': None,
  111. 'blt': None,
  112. 'bge': None,
  113. 'bltu': None,
  114. 'bgeu': None,
  115. 'lb': None,
  116. 'lh': None,
  117. 'lw': None,
  118. 'lbu': None,
  119. 'lhu': None,
  120. 'sb': None,
  121. 'sh': None,
  122. 'sw': None,
  123. 'addi': itype,
  124. 'slti': itype,
  125. 'sltiu': itype,
  126. 'xori': itype,
  127. 'ori': itype,
  128. 'andi': itype,
  129. 'slli': itype,
  130. 'srli': itype,
  131. 'srai': itype,
  132. 'add': rtype,
  133. 'sub': rtype,
  134. 'sll': rtype,
  135. 'slt': rtype,
  136. 'sltu': rtype,
  137. 'xor': rtype,
  138. 'srl': rtype,
  139. 'sra': rtype,
  140. 'or': rtype,
  141. 'and': rtype,
  142. }
  143. def trimmed(line):
  144. trimmed = line[:line.find(';')]
  145. s = [w for w in filter(lambda x: len(x) > 0,trimmed.split(' '))]
  146. o = []
  147. for w in s:
  148. o += [w for w in filter(lambda x: len(x) > 0,w.split(','))]
  149. return o
  150. if __name__ == '__main__':
  151. infile = open(sys.argv[1],'r')
  152. for line in infile.readlines():
  153. trim = trimmed(line)
  154. if trim[0][0] == '.':
  155. directive(trim)
  156. elif ':' in trim[0]:
  157. label(trim)
  158. infile.seek(0)
  159. for line in infile.readlines():
  160. trim = trimmed(line)
  161. if trim[0] in instrs:
  162. if instrs[trim[0]] != None:
  163. instrs[trim[0]](trim)
  164. else:
  165. print('not impl: {}'.format(trim[0]))
  166. else:
  167. print(trim)