agz80asm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_symbol_end :
  224. begin
  225. end;
  226. ait_const:
  227. begin
  228. consttype:=tai_const(hp).consttype;
  229. case consttype of
  230. {aitconst_uleb128bit,
  231. aitconst_sleb128bit,
  232. aitconst_128bit,
  233. aitconst_64bit,
  234. aitconst_32bit,}
  235. aitconst_16bit,
  236. aitconst_8bit,
  237. aitconst_16bit_unaligned{,
  238. aitconst_32bit_unaligned,
  239. aitconst_64bit_unaligned,
  240. aitconst_rva_symbol,
  241. aitconst_secrel32_symbol} :
  242. begin
  243. writer.AsmWrite(ait_const2str[consttype]);
  244. l:=0;
  245. tokens:=1;
  246. repeat
  247. if assigned(tai_const(hp).sym) then
  248. begin
  249. if assigned(tai_const(hp).endsym) then
  250. s:=EscapeLabel(tai_const(hp).endsym.name)+'-'+EscapeLabel(tai_const(hp).sym.name)
  251. else
  252. s:=EscapeLabel(tai_const(hp).sym.name);
  253. if tai_const(hp).value<>0 then
  254. s:=s+tostr_with_plus(tai_const(hp).value);
  255. end
  256. else
  257. s:=tostr(tai_const(hp).value);
  258. writer.AsmWrite(s);
  259. inc(l,length(s));
  260. inc(tokens);
  261. if (l>line_length) or
  262. (tokens>max_tokens) or
  263. (hp.next=nil) or
  264. (tai(hp.next).typ<>ait_const) or
  265. (tai_const(hp.next).consttype<>consttype) then
  266. break;
  267. hp:=tai(hp.next);
  268. writer.AsmWrite(',');
  269. until false;
  270. { Substract section start for secrel32 type }
  271. if consttype=aitconst_secrel32_symbol then
  272. writer.AsmWrite(' - $$');
  273. writer.AsmLn;
  274. end;
  275. else
  276. begin
  277. writer.AsmWrite(asminfo^.comment);
  278. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  279. Str(consttype,s);
  280. writer.AsmWriteLn(s);
  281. end;
  282. end;
  283. end;
  284. ait_string :
  285. begin
  286. counter := 0;
  287. lines := tai_string(hp).len div line_length;
  288. { separate lines in different parts }
  289. if tai_string(hp).len > 0 then
  290. Begin
  291. for j := 0 to lines-1 do
  292. begin
  293. writer.AsmWrite(#9#9'DB'#9);
  294. quoted:=false;
  295. for i:=counter to counter+line_length-1 do
  296. begin
  297. { it is an ascii character. }
  298. if (ord(tai_string(hp).str[i])>31) and
  299. (ord(tai_string(hp).str[i])<127) and
  300. (tai_string(hp).str[i]<>'"') then
  301. begin
  302. if not(quoted) then
  303. begin
  304. if i>counter then
  305. writer.AsmWrite(',');
  306. writer.AsmWrite('"');
  307. end;
  308. writer.AsmWrite(tai_string(hp).str[i]);
  309. quoted:=true;
  310. end { if > 31 and < 127 and ord('"') }
  311. else
  312. begin
  313. if quoted then
  314. writer.AsmWrite('"');
  315. if i>counter then
  316. writer.AsmWrite(',');
  317. quoted:=false;
  318. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  319. end;
  320. end; { end for i:=0 to... }
  321. if quoted then writer.AsmWrite('"');
  322. writer.AsmWrite(target_info.newline);
  323. counter := counter+line_length;
  324. end; { end for j:=0 ... }
  325. { do last line of lines }
  326. if counter<tai_string(hp).len then
  327. writer.AsmWrite(#9#9'DB'#9);
  328. quoted:=false;
  329. for i:=counter to tai_string(hp).len-1 do
  330. begin
  331. { it is an ascii character. }
  332. if (ord(tai_string(hp).str[i])>31) and
  333. (ord(tai_string(hp).str[i])<128) and
  334. (tai_string(hp).str[i]<>'"') then
  335. begin
  336. if not(quoted) then
  337. begin
  338. if i>counter then
  339. writer.AsmWrite(',');
  340. writer.AsmWrite('"');
  341. end;
  342. writer.AsmWrite(tai_string(hp).str[i]);
  343. quoted:=true;
  344. end { if > 31 and < 128 and " }
  345. else
  346. begin
  347. if quoted then
  348. writer.AsmWrite('"');
  349. if i>counter then
  350. writer.AsmWrite(',');
  351. quoted:=false;
  352. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  353. end;
  354. end; { end for i:=0 to... }
  355. if quoted then
  356. writer.AsmWrite('"');
  357. end;
  358. writer.AsmLn;
  359. end;
  360. else
  361. begin
  362. writer.AsmWrite(asminfo^.comment);
  363. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  364. Str(hp.typ,s);
  365. writer.AsmWriteLn(s);
  366. end;
  367. end;
  368. hp:=tai(hp.next);
  369. end;
  370. //op:=taicpu(hp).opcode;
  371. //s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  372. //if taicpu(hp).ops<>0 then
  373. // begin
  374. // sep:=#9;
  375. // for i:=0 to taicpu(hp).ops-1 do
  376. // begin
  377. // s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  378. // sep:=',';
  379. // end;
  380. // end;
  381. //owner.writer.AsmWriteLn(s);
  382. end;
  383. procedure TZ80AsmAssembler.WriteAsmList;
  384. var
  385. hal: TAsmListType;
  386. begin
  387. for hal:=low(TasmlistType) to high(TasmlistType) do
  388. begin
  389. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  390. writetree(current_asmdata.asmlists[hal]);
  391. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  392. end;
  393. end;
  394. function TZ80AsmAssembler.MakeCmdLine: TCmdStr;
  395. begin
  396. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  397. end;
  398. const
  399. as_Z80_asm_info : tasminfo =
  400. (
  401. id : as_z80asm;
  402. idtxt : 'Z80ASM';
  403. asmbin : 'z80asm';
  404. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  405. supported_targets : [system_Z80_embedded];
  406. flags : [af_needar,af_smartlink_sections];
  407. labelprefix : '.L';
  408. comment : '; ';
  409. dollarsign: 's';
  410. );
  411. begin
  412. RegisterAssembler(as_Z80_asm_info,TZ80AsmAssembler);
  413. end.