agz80asm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 }
  30. TZ80AsmAssembler=class(TExternalAssembler)
  31. private
  32. function EscapeLabel(s: ansistring): ansistring;
  33. public
  34. procedure WriteTree(p : TAsmList); override;
  35. procedure WriteAsmList;override;
  36. function MakeCmdLine: TCmdStr; override;
  37. end;
  38. implementation
  39. uses
  40. cutils,globals,verbose,
  41. aasmbase,aasmcpu,
  42. cpuinfo,
  43. cgbase,cgutils;
  44. const
  45. line_length = 70;
  46. max_tokens : longint = 25;
  47. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  48. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  49. #9'FIXMESLEB',#9'FIXEMEULEB',
  50. #9'DD RVA'#9,#9'DD SECREL32'#9,
  51. #9'FIXME',#9'FIXME',#9'FIXME',#9'FIXME',
  52. #9'DW'#9,#9'DD'#9,#9'DQ'#9
  53. );
  54. function TZ80AsmAssembler.EscapeLabel(s: ansistring): ansistring;
  55. var
  56. i: Integer;
  57. begin
  58. result:='';
  59. for i:=1 to length(s) do
  60. if ((s[i]>='a') and (s[i]<='z')) or
  61. ((s[i]>='A') and (s[i]<='Z')) or
  62. ((s[i]>='0') and (s[i]<='9')) or
  63. (s[i]='.') then
  64. result:=result+s[i]
  65. else if s[i]='_' then
  66. result:=result+'__'
  67. else
  68. result:=result+('_'+HexStr(Ord(s[i]),2));
  69. end;
  70. procedure TZ80AsmAssembler.WriteTree(p: TAsmList);
  71. function getreferencestring(var ref : treference) : string;
  72. var
  73. s : string;
  74. begin
  75. s:='';
  76. with ref do
  77. begin
  78. {$ifdef extdebug}
  79. // if base=NR_NO then
  80. // internalerror(200308292);
  81. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  82. // internalerror(200308293);
  83. {$endif extdebug}
  84. if index<>NR_NO then
  85. internalerror(2011021701)
  86. else if base<>NR_NO then
  87. begin
  88. // if addressmode=AM_PREDRECEMENT then
  89. // s:='-';
  90. //case base of
  91. // NR_R26:
  92. // s:=s+'X';
  93. // NR_R28:
  94. // s:=s+'Y';
  95. // NR_R30:
  96. // s:=s+'Z';
  97. // else
  98. // s:=gas_regname(base);
  99. //end;
  100. //if addressmode=AM_POSTINCREMENT then
  101. // s:=s+'+';
  102. //
  103. //if offset>0 then
  104. // s:=s+'+'+tostr(offset)
  105. //else if offset<0 then
  106. // s:=s+tostr(offset)
  107. end
  108. else if assigned(symbol) or (offset<>0) then
  109. begin
  110. //if assigned(symbol) then
  111. // s:=ReplaceForbiddenAsmSymbolChars(symbol.name);
  112. //
  113. //if offset<0 then
  114. // s:=s+tostr(offset)
  115. //else if offset>0 then
  116. // s:=s+'+'+tostr(offset);
  117. //case refaddr of
  118. // addr_hi8:
  119. // s:='hi8('+s+')';
  120. // addr_hi8_gs:
  121. // s:='hi8(gs('+s+'))';
  122. // addr_lo8:
  123. // s:='lo8('+s+')';
  124. // addr_lo8_gs:
  125. // s:='lo8(gs('+s+'))';
  126. // else
  127. // s:='('+s+')';
  128. //end;
  129. end;
  130. end;
  131. getreferencestring:=s;
  132. end;
  133. function getopstr(const o:toper) : string;
  134. var
  135. hs : string;
  136. first : boolean;
  137. r : tsuperregister;
  138. begin
  139. //case o.typ of
  140. // top_reg:
  141. // getopstr:=gas_regname(o.reg);
  142. // top_const:
  143. // getopstr:=tostr(longint(o.val));
  144. // top_ref:
  145. // if o.ref^.refaddr=addr_full then
  146. // begin
  147. // hs:=ReplaceForbiddenAsmSymbolChars(o.ref^.symbol.name);
  148. // if o.ref^.offset>0 then
  149. // hs:=hs+'+'+tostr(o.ref^.offset)
  150. // else
  151. // if o.ref^.offset<0 then
  152. // hs:=hs+tostr(o.ref^.offset);
  153. // getopstr:=hs;
  154. // end
  155. // else
  156. // getopstr:=getreferencestring(o.ref^);
  157. // else
  158. // internalerror(2002070604);
  159. //end;
  160. end;
  161. //var op: TAsmOp;
  162. // s: string;
  163. // i: byte;
  164. // sep: string[3];
  165. var
  166. hp: tai;
  167. s: string;
  168. counter,lines,i,j,l,tokens: longint;
  169. quoted: Boolean;
  170. consttype: taiconst_type;
  171. begin
  172. if not assigned(p) then
  173. exit;
  174. hp:=tai(p.first);
  175. while assigned(hp) do
  176. begin
  177. prefetch(pointer(hp.next)^);
  178. case hp.typ of
  179. ait_comment :
  180. begin
  181. writer.AsmWrite(asminfo^.comment);
  182. writer.AsmWritePChar(tai_comment(hp).str);
  183. writer.AsmLn;
  184. end;
  185. ait_align :
  186. begin
  187. if tai_align_abstract(hp).aligntype>1 then
  188. writer.AsmWriteLn(asminfo^.comment+'Unsupported ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  189. end;
  190. ait_label :
  191. begin
  192. if tai_label(hp).labsym.is_used then
  193. begin
  194. writer.AsmWrite(EscapeLabel(tai_label(hp).labsym.name));
  195. writer.AsmWriteLn(':');
  196. end;
  197. end;
  198. ait_symbol :
  199. begin
  200. if tai_symbol(hp).has_value then
  201. internalerror(2009090802);
  202. { wasm is case insensitive, we nned to use only uppercase version
  203. if both a lowercase and an uppercase version are provided }
  204. {if (asminfo^.id = as_i386_wasm) then
  205. begin
  206. nhp:=tai(hp.next);
  207. while assigned(nhp) and (nhp.typ in [ait_function_name,ait_force_line]) do
  208. nhp:=tai(nhp.next);
  209. if assigned(nhp) and (tai(nhp).typ=ait_symbol) and
  210. (lower(tai_symbol(nhp).sym.name)=tai_symbol(hp).sym.name) then
  211. begin
  212. writer.AsmWriteln(asminfo^.comment+' '+tai_symbol(hp).sym.name+' removed');
  213. hp:=tai(nhp);
  214. end;
  215. end;}
  216. {if tai_symbol(hp).is_global then
  217. writer.AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);}
  218. writer.AsmWrite(EscapeLabel(tai_symbol(hp).sym.name));
  219. {if assigned(hp.next) and not(tai(hp.next).typ in
  220. [ait_const,ait_realconst,ait_string]) then}
  221. writer.AsmWriteLn(':');
  222. end;
  223. ait_const:
  224. begin
  225. consttype:=tai_const(hp).consttype;
  226. case consttype of
  227. {aitconst_uleb128bit,
  228. aitconst_sleb128bit,
  229. aitconst_128bit,
  230. aitconst_64bit,
  231. aitconst_32bit,}
  232. aitconst_16bit,
  233. aitconst_8bit,
  234. aitconst_16bit_unaligned{,
  235. aitconst_32bit_unaligned,
  236. aitconst_64bit_unaligned,
  237. aitconst_rva_symbol,
  238. aitconst_secrel32_symbol} :
  239. begin
  240. writer.AsmWrite(ait_const2str[consttype]);
  241. l:=0;
  242. tokens:=1;
  243. repeat
  244. if assigned(tai_const(hp).sym) then
  245. begin
  246. if assigned(tai_const(hp).endsym) then
  247. s:=EscapeLabel(tai_const(hp).endsym.name)+'-'+EscapeLabel(tai_const(hp).sym.name)
  248. else
  249. s:=EscapeLabel(tai_const(hp).sym.name);
  250. if tai_const(hp).value<>0 then
  251. s:=s+tostr_with_plus(tai_const(hp).value);
  252. end
  253. else
  254. s:=tostr(tai_const(hp).value);
  255. writer.AsmWrite(s);
  256. inc(l,length(s));
  257. inc(tokens);
  258. if (l>line_length) or
  259. (tokens>max_tokens) or
  260. (hp.next=nil) or
  261. (tai(hp.next).typ<>ait_const) or
  262. (tai_const(hp.next).consttype<>consttype) then
  263. break;
  264. hp:=tai(hp.next);
  265. writer.AsmWrite(',');
  266. until false;
  267. { Substract section start for secrel32 type }
  268. if consttype=aitconst_secrel32_symbol then
  269. writer.AsmWrite(' - $$');
  270. writer.AsmLn;
  271. end;
  272. else
  273. begin
  274. writer.AsmWrite(asminfo^.comment);
  275. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  276. Str(consttype,s);
  277. writer.AsmWriteLn(s);
  278. end;
  279. end;
  280. end;
  281. ait_string :
  282. begin
  283. counter := 0;
  284. lines := tai_string(hp).len div line_length;
  285. { separate lines in different parts }
  286. if tai_string(hp).len > 0 then
  287. Begin
  288. for j := 0 to lines-1 do
  289. begin
  290. writer.AsmWrite(#9#9'DB'#9);
  291. quoted:=false;
  292. for i:=counter to counter+line_length-1 do
  293. begin
  294. { it is an ascii character. }
  295. if (ord(tai_string(hp).str[i])>31) and
  296. (ord(tai_string(hp).str[i])<127) and
  297. (tai_string(hp).str[i]<>'"') then
  298. begin
  299. if not(quoted) then
  300. begin
  301. if i>counter then
  302. writer.AsmWrite(',');
  303. writer.AsmWrite('"');
  304. end;
  305. writer.AsmWrite(tai_string(hp).str[i]);
  306. quoted:=true;
  307. end { if > 31 and < 127 and ord('"') }
  308. else
  309. begin
  310. if quoted then
  311. writer.AsmWrite('"');
  312. if i>counter then
  313. writer.AsmWrite(',');
  314. quoted:=false;
  315. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  316. end;
  317. end; { end for i:=0 to... }
  318. if quoted then writer.AsmWrite('"');
  319. writer.AsmWrite(target_info.newline);
  320. counter := counter+line_length;
  321. end; { end for j:=0 ... }
  322. { do last line of lines }
  323. if counter<tai_string(hp).len then
  324. writer.AsmWrite(#9#9'DB'#9);
  325. quoted:=false;
  326. for i:=counter to tai_string(hp).len-1 do
  327. begin
  328. { it is an ascii character. }
  329. if (ord(tai_string(hp).str[i])>31) and
  330. (ord(tai_string(hp).str[i])<128) and
  331. (tai_string(hp).str[i]<>'"') then
  332. begin
  333. if not(quoted) then
  334. begin
  335. if i>counter then
  336. writer.AsmWrite(',');
  337. writer.AsmWrite('"');
  338. end;
  339. writer.AsmWrite(tai_string(hp).str[i]);
  340. quoted:=true;
  341. end { if > 31 and < 128 and " }
  342. else
  343. begin
  344. if quoted then
  345. writer.AsmWrite('"');
  346. if i>counter then
  347. writer.AsmWrite(',');
  348. quoted:=false;
  349. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  350. end;
  351. end; { end for i:=0 to... }
  352. if quoted then
  353. writer.AsmWrite('"');
  354. end;
  355. writer.AsmLn;
  356. end;
  357. else
  358. begin
  359. writer.AsmWrite(asminfo^.comment);
  360. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  361. Str(hp.typ,s);
  362. writer.AsmWriteLn(s);
  363. end;
  364. end;
  365. hp:=tai(hp.next);
  366. end;
  367. //op:=taicpu(hp).opcode;
  368. //s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  369. //if taicpu(hp).ops<>0 then
  370. // begin
  371. // sep:=#9;
  372. // for i:=0 to taicpu(hp).ops-1 do
  373. // begin
  374. // s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  375. // sep:=',';
  376. // end;
  377. // end;
  378. //owner.writer.AsmWriteLn(s);
  379. end;
  380. procedure TZ80AsmAssembler.WriteAsmList;
  381. var
  382. hal: TAsmListType;
  383. begin
  384. for hal:=low(TasmlistType) to high(TasmlistType) do
  385. begin
  386. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  387. writetree(current_asmdata.asmlists[hal]);
  388. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  389. end;
  390. end;
  391. function TZ80AsmAssembler.MakeCmdLine: TCmdStr;
  392. begin
  393. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  394. end;
  395. const
  396. as_Z80_asm_info : tasminfo =
  397. (
  398. id : as_z80asm;
  399. idtxt : 'Z80Asm';
  400. asmbin : 'z80asm';
  401. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  402. supported_targets : [system_Z80_embedded];
  403. flags : [af_needar,af_smartlink_sections];
  404. labelprefix : '.L';
  405. comment : '; ';
  406. dollarsign: 's';
  407. );
  408. begin
  409. RegisterAssembler(as_Z80_asm_info,TZ80AsmAssembler);
  410. end.