agz80asm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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:=ApplyAsmSymbolRestrictions(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. result:='';
  140. //case o.typ of
  141. // top_reg:
  142. // getopstr:=gas_regname(o.reg);
  143. // top_const:
  144. // getopstr:=tostr(longint(o.val));
  145. // top_ref:
  146. // if o.ref^.refaddr=addr_full then
  147. // begin
  148. // hs:=ApplyAsmSymbolRestrictions(o.ref^.symbol.name);
  149. // if o.ref^.offset>0 then
  150. // hs:=hs+'+'+tostr(o.ref^.offset)
  151. // else
  152. // if o.ref^.offset<0 then
  153. // hs:=hs+tostr(o.ref^.offset);
  154. // getopstr:=hs;
  155. // end
  156. // else
  157. // getopstr:=getreferencestring(o.ref^);
  158. // else
  159. // internalerror(2002070604);
  160. //end;
  161. end;
  162. //var op: TAsmOp;
  163. // s: string;
  164. // i: byte;
  165. // sep: string[3];
  166. var
  167. hp: tai;
  168. s: string;
  169. counter,lines,i,j,l,tokens: longint;
  170. quoted: Boolean;
  171. consttype: taiconst_type;
  172. begin
  173. if not assigned(p) then
  174. exit;
  175. hp:=tai(p.first);
  176. while assigned(hp) do
  177. begin
  178. prefetch(pointer(hp.next)^);
  179. case hp.typ of
  180. ait_comment :
  181. begin
  182. writer.AsmWrite(asminfo^.comment);
  183. writer.AsmWritePChar(tai_comment(hp).str);
  184. writer.AsmLn;
  185. end;
  186. ait_align :
  187. begin
  188. if tai_align_abstract(hp).aligntype>1 then
  189. writer.AsmWriteLn(asminfo^.comment+'Unsupported ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  190. end;
  191. ait_label :
  192. begin
  193. if tai_label(hp).labsym.is_used then
  194. begin
  195. writer.AsmWrite(EscapeLabel(tai_label(hp).labsym.name));
  196. writer.AsmWriteLn(':');
  197. end;
  198. end;
  199. ait_symbol :
  200. begin
  201. if tai_symbol(hp).has_value then
  202. internalerror(2009090802);
  203. { wasm is case insensitive, we nned to use only uppercase version
  204. if both a lowercase and an uppercase version are provided }
  205. {if (asminfo^.id = as_i386_wasm) then
  206. begin
  207. nhp:=tai(hp.next);
  208. while assigned(nhp) and (nhp.typ in [ait_function_name,ait_force_line]) do
  209. nhp:=tai(nhp.next);
  210. if assigned(nhp) and (tai(nhp).typ=ait_symbol) and
  211. (lower(tai_symbol(nhp).sym.name)=tai_symbol(hp).sym.name) then
  212. begin
  213. writer.AsmWriteln(asminfo^.comment+' '+tai_symbol(hp).sym.name+' removed');
  214. hp:=tai(nhp);
  215. end;
  216. end;}
  217. {if tai_symbol(hp).is_global then
  218. writer.AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);}
  219. writer.AsmWrite(EscapeLabel(tai_symbol(hp).sym.name));
  220. {if assigned(hp.next) and not(tai(hp.next).typ in
  221. [ait_const,ait_realconst,ait_string]) then}
  222. writer.AsmWriteLn(':');
  223. end;
  224. ait_symbol_end :
  225. begin
  226. end;
  227. ait_const:
  228. begin
  229. consttype:=tai_const(hp).consttype;
  230. case consttype of
  231. {aitconst_uleb128bit,
  232. aitconst_sleb128bit,
  233. aitconst_128bit,
  234. aitconst_64bit,
  235. aitconst_32bit,}
  236. aitconst_16bit,
  237. aitconst_8bit,
  238. aitconst_16bit_unaligned{,
  239. aitconst_32bit_unaligned,
  240. aitconst_64bit_unaligned,
  241. aitconst_rva_symbol,
  242. aitconst_secrel32_symbol} :
  243. begin
  244. writer.AsmWrite(ait_const2str[consttype]);
  245. l:=0;
  246. tokens:=1;
  247. repeat
  248. if assigned(tai_const(hp).sym) then
  249. begin
  250. if assigned(tai_const(hp).endsym) then
  251. s:=EscapeLabel(tai_const(hp).endsym.name)+'-'+EscapeLabel(tai_const(hp).sym.name)
  252. else
  253. s:=EscapeLabel(tai_const(hp).sym.name);
  254. if tai_const(hp).value<>0 then
  255. s:=s+tostr_with_plus(tai_const(hp).value);
  256. end
  257. else
  258. s:=tostr(tai_const(hp).value);
  259. writer.AsmWrite(s);
  260. inc(l,length(s));
  261. inc(tokens);
  262. if (l>line_length) or
  263. (tokens>max_tokens) or
  264. (hp.next=nil) or
  265. (tai(hp.next).typ<>ait_const) or
  266. (tai_const(hp.next).consttype<>consttype) then
  267. break;
  268. hp:=tai(hp.next);
  269. writer.AsmWrite(',');
  270. until false;
  271. { Substract section start for secrel32 type }
  272. if consttype=aitconst_secrel32_symbol then
  273. writer.AsmWrite(' - $$');
  274. writer.AsmLn;
  275. end;
  276. else
  277. begin
  278. writer.AsmWrite(asminfo^.comment);
  279. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  280. Str(consttype,s);
  281. writer.AsmWriteLn(s);
  282. end;
  283. end;
  284. end;
  285. ait_string :
  286. begin
  287. counter := 0;
  288. lines := tai_string(hp).len div line_length;
  289. { separate lines in different parts }
  290. if tai_string(hp).len > 0 then
  291. Begin
  292. for j := 0 to lines-1 do
  293. begin
  294. writer.AsmWrite(#9#9'DB'#9);
  295. quoted:=false;
  296. for i:=counter to counter+line_length-1 do
  297. begin
  298. { it is an ascii character. }
  299. if (ord(tai_string(hp).str[i])>31) and
  300. (ord(tai_string(hp).str[i])<127) and
  301. (tai_string(hp).str[i]<>'"') then
  302. begin
  303. if not(quoted) then
  304. begin
  305. if i>counter then
  306. writer.AsmWrite(',');
  307. writer.AsmWrite('"');
  308. end;
  309. writer.AsmWrite(tai_string(hp).str[i]);
  310. quoted:=true;
  311. end { if > 31 and < 127 and ord('"') }
  312. else
  313. begin
  314. if quoted then
  315. writer.AsmWrite('"');
  316. if i>counter then
  317. writer.AsmWrite(',');
  318. quoted:=false;
  319. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  320. end;
  321. end; { end for i:=0 to... }
  322. if quoted then writer.AsmWrite('"');
  323. writer.AsmWrite(target_info.newline);
  324. counter := counter+line_length;
  325. end; { end for j:=0 ... }
  326. { do last line of lines }
  327. if counter<tai_string(hp).len then
  328. writer.AsmWrite(#9#9'DB'#9);
  329. quoted:=false;
  330. for i:=counter to tai_string(hp).len-1 do
  331. begin
  332. { it is an ascii character. }
  333. if (ord(tai_string(hp).str[i])>31) and
  334. (ord(tai_string(hp).str[i])<128) and
  335. (tai_string(hp).str[i]<>'"') then
  336. begin
  337. if not(quoted) then
  338. begin
  339. if i>counter then
  340. writer.AsmWrite(',');
  341. writer.AsmWrite('"');
  342. end;
  343. writer.AsmWrite(tai_string(hp).str[i]);
  344. quoted:=true;
  345. end { if > 31 and < 128 and " }
  346. else
  347. begin
  348. if quoted then
  349. writer.AsmWrite('"');
  350. if i>counter then
  351. writer.AsmWrite(',');
  352. quoted:=false;
  353. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  354. end;
  355. end; { end for i:=0 to... }
  356. if quoted then
  357. writer.AsmWrite('"');
  358. end;
  359. writer.AsmLn;
  360. end;
  361. else
  362. begin
  363. writer.AsmWrite(asminfo^.comment);
  364. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  365. Str(hp.typ,s);
  366. writer.AsmWriteLn(s);
  367. end;
  368. end;
  369. hp:=tai(hp.next);
  370. end;
  371. //op:=taicpu(hp).opcode;
  372. //s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  373. //if taicpu(hp).ops<>0 then
  374. // begin
  375. // sep:=#9;
  376. // for i:=0 to taicpu(hp).ops-1 do
  377. // begin
  378. // s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  379. // sep:=',';
  380. // end;
  381. // end;
  382. //owner.writer.AsmWriteLn(s);
  383. end;
  384. procedure TZ80AsmAssembler.WriteAsmList;
  385. var
  386. hal: TAsmListType;
  387. begin
  388. for hal:=low(TasmlistType) to high(TasmlistType) do
  389. begin
  390. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  391. writetree(current_asmdata.asmlists[hal]);
  392. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  393. end;
  394. end;
  395. function TZ80AsmAssembler.MakeCmdLine: TCmdStr;
  396. begin
  397. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  398. end;
  399. const
  400. as_Z80_asm_info : tasminfo =
  401. (
  402. id : as_z80asm;
  403. idtxt : 'Z80ASM';
  404. asmbin : 'z80asm';
  405. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  406. supported_targets : [system_Z80_embedded];
  407. flags : [af_needar{,af_smartlink_sections}];
  408. labelprefix : '.L';
  409. labelmaxlen : -1;
  410. comment : '; ';
  411. dollarsign: 's';
  412. );
  413. begin
  414. RegisterAssembler(as_Z80_asm_info,TZ80AsmAssembler);
  415. end.