agz80asm.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the Z80
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the assembler writer for the z80asm assembler:
  18. http://savannah.nongnu.org/projects/z80asm
  19. }
  20. unit agz80asm;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,systems,
  25. aasmtai,aasmdata,
  26. assemble,
  27. cpubase;
  28. type
  29. TZ80AsmAssembler=class(TExternalAssembler)
  30. procedure WriteTree(p : TAsmList); override;
  31. procedure WriteAsmList;override;
  32. function MakeCmdLine: TCmdStr; override;
  33. end;
  34. implementation
  35. uses
  36. cutils,globals,verbose,
  37. aasmbase,aasmcpu,
  38. cpuinfo,
  39. cgbase,cgutils;
  40. procedure TZ80AsmAssembler.WriteTree(p: TAsmList);
  41. function getreferencestring(var ref : treference) : string;
  42. var
  43. s : string;
  44. begin
  45. s:='';
  46. with ref do
  47. begin
  48. {$ifdef extdebug}
  49. // if base=NR_NO then
  50. // internalerror(200308292);
  51. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  52. // internalerror(200308293);
  53. {$endif extdebug}
  54. if index<>NR_NO then
  55. internalerror(2011021701)
  56. else if base<>NR_NO then
  57. begin
  58. // if addressmode=AM_PREDRECEMENT then
  59. // s:='-';
  60. //case base of
  61. // NR_R26:
  62. // s:=s+'X';
  63. // NR_R28:
  64. // s:=s+'Y';
  65. // NR_R30:
  66. // s:=s+'Z';
  67. // else
  68. // s:=gas_regname(base);
  69. //end;
  70. //if addressmode=AM_POSTINCREMENT then
  71. // s:=s+'+';
  72. //
  73. //if offset>0 then
  74. // s:=s+'+'+tostr(offset)
  75. //else if offset<0 then
  76. // s:=s+tostr(offset)
  77. end
  78. else if assigned(symbol) or (offset<>0) then
  79. begin
  80. //if assigned(symbol) then
  81. // s:=ReplaceForbiddenAsmSymbolChars(symbol.name);
  82. //
  83. //if offset<0 then
  84. // s:=s+tostr(offset)
  85. //else if offset>0 then
  86. // s:=s+'+'+tostr(offset);
  87. //case refaddr of
  88. // addr_hi8:
  89. // s:='hi8('+s+')';
  90. // addr_hi8_gs:
  91. // s:='hi8(gs('+s+'))';
  92. // addr_lo8:
  93. // s:='lo8('+s+')';
  94. // addr_lo8_gs:
  95. // s:='lo8(gs('+s+'))';
  96. // else
  97. // s:='('+s+')';
  98. //end;
  99. end;
  100. end;
  101. getreferencestring:=s;
  102. end;
  103. function getopstr(const o:toper) : string;
  104. var
  105. hs : string;
  106. first : boolean;
  107. r : tsuperregister;
  108. begin
  109. //case o.typ of
  110. // top_reg:
  111. // getopstr:=gas_regname(o.reg);
  112. // top_const:
  113. // getopstr:=tostr(longint(o.val));
  114. // top_ref:
  115. // if o.ref^.refaddr=addr_full then
  116. // begin
  117. // hs:=ReplaceForbiddenAsmSymbolChars(o.ref^.symbol.name);
  118. // if o.ref^.offset>0 then
  119. // hs:=hs+'+'+tostr(o.ref^.offset)
  120. // else
  121. // if o.ref^.offset<0 then
  122. // hs:=hs+tostr(o.ref^.offset);
  123. // getopstr:=hs;
  124. // end
  125. // else
  126. // getopstr:=getreferencestring(o.ref^);
  127. // else
  128. // internalerror(2002070604);
  129. //end;
  130. end;
  131. //var op: TAsmOp;
  132. // s: string;
  133. // i: byte;
  134. // sep: string[3];
  135. var
  136. hp: tai;
  137. s: string;
  138. begin
  139. if not assigned(p) then
  140. exit;
  141. hp:=tai(p.first);
  142. while assigned(hp) do
  143. begin
  144. prefetch(pointer(hp.next)^);
  145. case hp.typ of
  146. ait_comment :
  147. begin
  148. writer.AsmWrite(asminfo^.comment);
  149. writer.AsmWritePChar(tai_comment(hp).str);
  150. writer.AsmLn;
  151. end;
  152. ait_label :
  153. begin
  154. if tai_label(hp).labsym.is_used then
  155. begin
  156. writer.AsmWrite(tai_label(hp).labsym.name);
  157. writer.AsmWriteLn(':');
  158. end;
  159. end;
  160. else
  161. begin
  162. writer.AsmWrite(asminfo^.comment);
  163. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  164. Str(hp.typ,s);
  165. writer.AsmWriteLn(s);
  166. end;
  167. end;
  168. hp:=tai(hp.next);
  169. end;
  170. //op:=taicpu(hp).opcode;
  171. //s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  172. //if taicpu(hp).ops<>0 then
  173. // begin
  174. // sep:=#9;
  175. // for i:=0 to taicpu(hp).ops-1 do
  176. // begin
  177. // s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  178. // sep:=',';
  179. // end;
  180. // end;
  181. //owner.writer.AsmWriteLn(s);
  182. end;
  183. procedure TZ80AsmAssembler.WriteAsmList;
  184. var
  185. hal: TAsmListType;
  186. begin
  187. for hal:=low(TasmlistType) to high(TasmlistType) do
  188. begin
  189. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  190. writetree(current_asmdata.asmlists[hal]);
  191. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  192. end;
  193. end;
  194. function TZ80AsmAssembler.MakeCmdLine: TCmdStr;
  195. begin
  196. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  197. end;
  198. const
  199. as_Z80_asm_info : tasminfo =
  200. (
  201. id : as_z80asm;
  202. idtxt : 'Z80Asm';
  203. asmbin : 'z80asm';
  204. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  205. supported_targets : [system_Z80_embedded];
  206. flags : [af_needar,af_smartlink_sections];
  207. labelprefix : '.L';
  208. comment : '; ';
  209. dollarsign: 's';
  210. );
  211. begin
  212. RegisterAssembler(as_Z80_asm_info,TZ80AsmAssembler);
  213. end.