agz80asm.pas 15 KB

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