nasmconv.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Peter Vreman and Florian Klaempfl
  4. Convert insns.dat from Nasm to a .inc file for usage with
  5. the Free pascal compiler
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  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.
  11. **********************************************************************}
  12. program nasmconv;
  13. var
  14. infile,outfile : text;
  15. s : string;
  16. i : longint;
  17. function Replace(var s:string;const s1,s2:string):boolean;
  18. var
  19. i : longint;
  20. begin
  21. i:=pos(s1,s);
  22. if i>0 then
  23. begin
  24. Delete(s,i,length(s1));
  25. Insert(s2,s,i);
  26. Replace:=true;
  27. end
  28. else
  29. Replace:=false;
  30. end;
  31. function formatop(s:string):string;
  32. const
  33. replaces=19;
  34. replacetab : array[1..replaces,1..2] of string[32]=(
  35. (':',' or ot_colon'),
  36. ('mem8','mem or ot_bits8'),
  37. ('mem16','mem or ot_bits16'),
  38. ('mem32','mem or ot_bits32'),
  39. ('mem64','mem or ot_bits64'),
  40. ('mem80','mem or ot_bits80'),
  41. ('mem','memory'),
  42. ('memory_offs','mem_offs'),
  43. ('imm8','imm or ot_bits8'),
  44. ('imm16','imm or ot_bits16'),
  45. ('imm32','imm or ot_bits32'),
  46. ('imm64','imm or ot_bits64'),
  47. ('imm80','imm or ot_bits80'),
  48. ('imm','immediate'),
  49. ('rm8','regmem or ot_bits8'),
  50. ('rm16','regmem or ot_bits16'),
  51. ('rm32','regmem or ot_bits32'),
  52. ('rm64','regmem or ot_bits64'),
  53. ('rm80','regmem or ot_bits80')
  54. );
  55. var
  56. i : longint;
  57. begin
  58. for i:=1to replaces do
  59. replace(s,replacetab[i,1],replacetab[i,2]);
  60. formatop:=s;
  61. end;
  62. procedure maybe_newline;
  63. begin
  64. if s[i]=#10 then
  65. begin
  66. readln(infile,s);
  67. i:=1;
  68. end;
  69. while s[1]=';' do
  70. begin
  71. readln(infile,s);
  72. i:=1;
  73. end;
  74. end;
  75. function readnumber : longint;
  76. var
  77. base : longint;
  78. result : longint;
  79. begin
  80. result:=0;
  81. if s[i]='\' then
  82. begin
  83. base:=8;
  84. inc(i);
  85. if s[i]='x' then
  86. begin
  87. base:=16;
  88. inc(i);
  89. end;
  90. end
  91. else
  92. base:=10;
  93. s[i]:=upcase(s[i]);
  94. while s[i] in ['0'..'9','A'..'F'] do
  95. begin
  96. case s[i] of
  97. '0'..'9':
  98. result:=result*base+ord(s[i])-ord('0');
  99. 'A'..'F':
  100. result:=result*base+ord(s[i])-ord('A')+10;
  101. end;
  102. inc(i);
  103. end;
  104. readnumber:=result;
  105. end;
  106. function tostr(l : longint) : string;
  107. var
  108. hs : string;
  109. begin
  110. str(l,hs);
  111. tostr:=hs;
  112. end;
  113. function readstr : string;
  114. var
  115. result : string;
  116. begin
  117. result:='';
  118. while (s[i] in ['0'..'9','A'..'Z','a'..'z','_']) and (i<=length(s)) do
  119. begin
  120. result:=result+s[i];
  121. inc(i);
  122. end;
  123. readstr:=result;
  124. end;
  125. procedure skipspace;
  126. begin
  127. while (s[i] in [' ',#9]) do
  128. inc(i);
  129. end;
  130. var
  131. hs : string;
  132. j : longint;
  133. first : boolean;
  134. maxinfolen,
  135. code : byte;
  136. insns : longint;
  137. { instruction fields }
  138. last,
  139. ops : longint;
  140. opcode,
  141. codes,
  142. flags : string;
  143. optypes : array[1..3] of string;
  144. begin
  145. writeln('Nasm Instruction Table Converter Version 0.99.11');
  146. insns:=0;
  147. assign(infile,'insns.dat');
  148. reset(infile);
  149. assign(outfile,'i386tab.inc');
  150. rewrite(outfile);
  151. writeln(outfile,'(');
  152. first:=true;
  153. while not(eof(infile)) do
  154. begin
  155. { handle comment }
  156. readln(infile,s);
  157. if s[1]=';' then
  158. continue;
  159. { clear }
  160. opcode:='';
  161. ops:=0;
  162. optypes[1]:='';
  163. optypes[2]:='';
  164. optypes[3]:='';
  165. codes:='';
  166. flags:='';
  167. { opcode }
  168. opcode:='A_';
  169. i:=1;
  170. while not(s[i] in [' ',#9]) do
  171. begin
  172. opcode:=opcode+s[i];
  173. inc(i);
  174. end;
  175. skipspace;
  176. { ops and optypes }
  177. repeat
  178. hs:=readstr;
  179. if (hs='void') or (hs='ignore') then
  180. break;
  181. inc(ops);
  182. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  183. if s[i]=':' then
  184. begin
  185. inc(i);
  186. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  187. end;
  188. while s[i]='|' do
  189. begin
  190. inc(i);
  191. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  192. end;
  193. if s[i]=',' then
  194. inc(i)
  195. else
  196. break;
  197. until false;
  198. for j:=1 to 3-ops do
  199. optypes[3-j+1]:='ot_none';
  200. { codes }
  201. skipspace;
  202. j:=0;
  203. last:=0;
  204. if s[i] in ['\','0'..'9'] then
  205. begin
  206. while not(s[i] in [' ',#9]) do
  207. begin
  208. code:=readnumber;
  209. { for some codes we want also to change the optypes, but not
  210. if the last byte was a 1 then this byte belongs to a direct
  211. copy }
  212. if last<>1 then
  213. begin
  214. case code of
  215. 12,13,14 :
  216. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  217. end;
  218. end;
  219. codes:=codes+'#'+tostr(code);
  220. last:=code;
  221. inc(j);
  222. end;
  223. end
  224. else
  225. codes:='#0';
  226. if j>maxinfolen then
  227. maxinfolen:=j;
  228. { flags }
  229. skipspace;
  230. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  231. begin
  232. hs:=readstr;
  233. if hs='ignore' then
  234. begin
  235. flags:='0';
  236. break;
  237. end;
  238. if hs<>'ND' then
  239. begin
  240. if flags<>'' then
  241. flags:=flags+' or ';
  242. flags:=flags+'if_'+hs;
  243. end;
  244. if (s[i]=',') and (i<=length(s)) then
  245. inc(i)
  246. else
  247. break;
  248. end;
  249. { write instruction }
  250. if not(first) then
  251. writeln(outfile,',')
  252. else
  253. first:=false;
  254. writeln(outfile,' (');
  255. writeln(outfile,' opcode : ',opcode,';');
  256. writeln(outfile,' ops : ',ops,';');
  257. writeln(outfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],');');
  258. writeln(outfile,' code : ',codes,';');
  259. writeln(outfile,' flags : ',flags);
  260. write(outfile,' )');
  261. maybe_newline;
  262. inc(insns);
  263. end;
  264. writeln(outfile);
  265. writeln(outfile,');');
  266. close(infile);
  267. close(outfile);
  268. writeln(insns,' nodes procesed (maxinfolen=',maxinfolen,')');
  269. end.
  270. {
  271. $Log$
  272. Revision 1.2 1999-05-23 18:42:24 florian
  273. * better error recovering in typed constants
  274. * some problems with arrays of const fixed, some problems
  275. due my previous
  276. - the location type of array constructor is now LOC_MEM
  277. - the pushing of high fixed
  278. - parameter copying fixed
  279. - zero temp. allocation removed
  280. * small problem in the assembler writers fixed:
  281. ref to nil wasn't written correctly
  282. Revision 1.1 1999/05/12 16:17:10 peter
  283. * init
  284. Revision 1.1 1999/05/12 16:08:27 peter
  285. + moved compiler utils
  286. }