nasmconv.pp 7.9 KB

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