ra386att.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symtable,scanner,hcodegen
  31. ,i386base
  32. ,rautils,ra386;
  33. type
  34. tasmtoken = (
  35. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  36. AS_REALNUM,AS_COMMA,AS_LPAREN,
  37. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  38. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  39. {------------------ Assembler directives --------------------}
  40. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,AS_ALIGN,AS_ASCII,
  41. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  42. AS_DATA,AS_TEXT,AS_END,
  43. {------------------ Assembler Operators --------------------}
  44. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  45. tasmkeyword = string[8];
  46. const
  47. { These tokens should be modified accordingly to the modifications }
  48. { in the different enumerations. }
  49. firstdirective = AS_DB;
  50. lastdirective = AS_END;
  51. _count_asmprefixes = 5;
  52. _count_asmspecialops = 25;
  53. _count_asmoverrides = 3;
  54. token2str : array[tasmtoken] of string[10]=(
  55. '','Label','LLabel','string','integer',
  56. 'float',',','(',
  57. ')',':','.','+','-','*',
  58. ';','identifier','register','opcode','/','$',
  59. '.byte','.word','.long','.quad','.globl','.align','.ascii',
  60. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  61. '.data','.text','END',
  62. '%','<<','>>','!','&','|','^','~');
  63. const
  64. newline = #10;
  65. firsttoken : boolean = TRUE;
  66. charcount : byte = 0;
  67. var
  68. _asmsorted,
  69. inexpression : boolean;
  70. curlist : paasmoutput;
  71. c : char;
  72. actasmtoken : tasmtoken;
  73. prevasmtoken : tasmtoken;
  74. actasmpattern : string;
  75. actopcode : tasmop;
  76. actasmregister : tregister;
  77. actopsize : topsize;
  78. actcondition : tasmcond;
  79. iasmops : ^op2strtable;
  80. iasmregs : ^reg2strtable;
  81. Procedure SetupTables;
  82. { creates uppercased symbol tables for speed access }
  83. var
  84. i : tasmop;
  85. j : tregister;
  86. Begin
  87. { opcodes }
  88. new(iasmops);
  89. for i:=firstop to lastop do
  90. iasmops^[i] := upper(att_op2str[i]);
  91. { registers }
  92. new(iasmregs);
  93. for j:=firstreg to lastreg do
  94. iasmregs^[j] := upper(att_reg2str[j]);
  95. end;
  96. {---------------------------------------------------------------------}
  97. { Routines for the tokenizing }
  98. {---------------------------------------------------------------------}
  99. function is_asmopcode(const s: string):boolean;
  100. const
  101. att_sizesuffixstr : array[0..8] of string[2] = (
  102. '','B','W','L','BW','BL','WL','Q','T'
  103. );
  104. att_sizesuffix : array[0..8] of topsize = (
  105. S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,S_IQ,S_FX
  106. );
  107. var
  108. i : tasmop;
  109. cond : string[4];
  110. cnd : tasmcond;
  111. len,
  112. j,
  113. sufidx : longint;
  114. hid : string;
  115. Begin
  116. is_asmopcode:=FALSE;
  117. actopcode:=A_None;
  118. actcondition:=C_None;
  119. actopsize:=S_NO;
  120. { search for all possible suffixes }
  121. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  122. begin
  123. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  124. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  125. begin
  126. { here we search the entire table... }
  127. hid:=copy(s,1,len);
  128. for i:=firstop to lastop do
  129. if (length(hid) > 0) and (hid=iasmops^[i]) then
  130. begin
  131. actopsize:=att_sizesuffix[sufidx];
  132. actopcode:=i;
  133. actasmtoken:=AS_OPCODE;
  134. is_asmopcode:=TRUE;
  135. exit;
  136. end;
  137. { not found, check condition opcodes }
  138. j:=0;
  139. while (j<CondAsmOps) do
  140. begin
  141. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  142. begin
  143. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  144. if cond<>'' then
  145. begin
  146. for cnd:=low(TasmCond) to high(TasmCond) do
  147. if Cond=Upper(cond2str[cnd]) then
  148. begin
  149. actopcode:=CondASmOp[j];
  150. actopsize:=att_sizesuffix[sufidx];
  151. actcondition:=cnd;
  152. actasmtoken:=AS_OPCODE;
  153. is_asmopcode:=TRUE;
  154. exit;
  155. end;
  156. end;
  157. end;
  158. inc(j);
  159. end;
  160. end;
  161. end;
  162. end;
  163. Function is_asmdirective(const s: string):boolean;
  164. var
  165. i : tasmtoken;
  166. hs : string;
  167. Begin
  168. { GNU as is also not casesensitive with this }
  169. hs:=lower(s);
  170. for i:=firstdirective to lastdirective do
  171. if hs=token2str[i] then
  172. begin
  173. actasmtoken:=i;
  174. is_asmdirective:=true;
  175. exit;
  176. end;
  177. is_asmdirective:=false;
  178. end;
  179. Function is_register(const s: string):boolean;
  180. Var
  181. i : tregister;
  182. Begin
  183. actasmregister:=R_NO;
  184. for i:=firstreg to lastreg do
  185. if s=iasmregs^[i] then
  186. begin
  187. actasmtoken:=AS_REGISTER;
  188. actasmregister:=i;
  189. is_register:=true;
  190. exit;
  191. end;
  192. is_register:=false;
  193. end;
  194. Function is_locallabel(const s: string):boolean;
  195. begin
  196. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  197. end;
  198. Procedure GetToken;
  199. var
  200. forcelabel: boolean;
  201. errorflag : boolean;
  202. len : longint;
  203. begin
  204. { save old token and reset new token }
  205. prevasmtoken:=actasmtoken;
  206. actasmtoken:=AS_NONE;
  207. { reset }
  208. errorflag:=FALSE;
  209. forcelabel:=FALSE;
  210. actasmpattern:='';
  211. { while space and tab , continue scan... }
  212. while c in [' ',#9] do
  213. c:=current_scanner^.asmgetchar;
  214. { get token pos }
  215. if not (c in [newline,#13,'{',';']) then
  216. current_scanner^.gettokenpos;
  217. { Local Label, Label, Directive, Prefix or Opcode }
  218. if firsttoken and not(c in [newline,#13,'{',';']) then
  219. begin
  220. firsttoken:=FALSE;
  221. len:=0;
  222. { directive or local label }
  223. if c = '.' then
  224. begin
  225. inc(len);
  226. actasmpattern[len]:=c;
  227. { Let us point to the next character }
  228. c:=current_scanner^.asmgetchar;
  229. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  230. begin
  231. inc(len);
  232. actasmpattern[len]:=c;
  233. c:=current_scanner^.asmgetchar;
  234. end;
  235. actasmpattern[0]:=chr(len);
  236. { this is a local label... }
  237. if (c=':') and is_locallabel(actasmpattern) then
  238. Begin
  239. { local variables are case sensitive }
  240. actasmtoken:=AS_LLABEL;
  241. c:=current_scanner^.asmgetchar;
  242. exit;
  243. end
  244. { must be a directive }
  245. else
  246. Begin
  247. { directives are case sensitive!! }
  248. if is_asmdirective(actasmpattern) then
  249. exit;
  250. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  251. end;
  252. end;
  253. { only opcodes and global labels are allowed now. }
  254. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  255. begin
  256. inc(len);
  257. actasmpattern[len]:=c;
  258. c:=current_scanner^.asmgetchar;
  259. end;
  260. actasmpattern[0]:=chr(len);
  261. { Label ? }
  262. if c = ':' then
  263. begin
  264. actasmtoken:=AS_LABEL;
  265. { let us point to the next character }
  266. c:=current_scanner^.asmgetchar;
  267. exit;
  268. end;
  269. { Opcode ? }
  270. If is_asmopcode(upper(actasmpattern)) then
  271. Begin
  272. uppervar(actasmpattern);
  273. exit;
  274. end;
  275. { End of assemblerblock ? }
  276. if upper(actasmpattern) = 'END' then
  277. begin
  278. actasmtoken:=AS_END;
  279. exit;
  280. end;
  281. actasmtoken:=AS_NONE;
  282. end
  283. else { else firsttoken }
  284. { Here we must handle all possible cases }
  285. begin
  286. case c of
  287. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  288. { - field of object/record }
  289. { - directive. }
  290. begin
  291. if (prevasmtoken=AS_ID) then
  292. begin
  293. c:=current_scanner^.asmgetchar;
  294. actasmtoken:=AS_DOT;
  295. exit;
  296. end;
  297. actasmpattern:=c;
  298. c:=current_scanner^.asmgetchar;
  299. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  300. begin
  301. actasmpattern:=actasmpattern + c;
  302. c:=current_scanner^.asmgetchar;
  303. end;
  304. if is_asmdirective(actasmpattern) then
  305. exit;
  306. { local label references and directives }
  307. { are case sensitive }
  308. actasmtoken:=AS_ID;
  309. exit;
  310. end;
  311. { identifier, register, prefix or directive }
  312. '_','A'..'Z','a'..'z':
  313. begin
  314. len:=0;
  315. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  316. begin
  317. inc(len);
  318. actasmpattern[len]:=c;
  319. c:=current_scanner^.asmgetchar;
  320. end;
  321. actasmpattern[0]:=chr(len);
  322. uppervar(actasmpattern);
  323. { Opcode, can only be when the previous was a prefix }
  324. If is_prefix(actopcode) and is_asmopcode(upper(actasmpattern)) then
  325. Begin
  326. uppervar(actasmpattern);
  327. exit;
  328. end;
  329. { check for end which is a reserved word unlike the opcodes }
  330. if actasmpattern = 'END' then
  331. Begin
  332. actasmtoken:=AS_END;
  333. exit;
  334. end;
  335. actasmtoken:=AS_ID;
  336. exit;
  337. end;
  338. '%' : { register or modulo }
  339. begin
  340. len:=1;
  341. actasmpattern[len]:='%';
  342. c:=current_scanner^.asmgetchar;
  343. while c in ['a'..'z','A'..'Z','0'..'9'] do
  344. Begin
  345. inc(len);
  346. actasmpattern[len]:=c;
  347. c:=current_scanner^.asmgetchar;
  348. end;
  349. actasmpattern[0]:=chr(len);
  350. uppervar(actasmpattern);
  351. if (actasmpattern = '%ST') and (c='(') then
  352. Begin
  353. actasmpattern:=actasmpattern+c;
  354. c:=current_scanner^.asmgetchar;
  355. if c in ['0'..'9'] then
  356. actasmpattern:=actasmpattern + c
  357. else
  358. Message(asmr_e_invalid_fpu_register);
  359. c:=current_scanner^.asmgetchar;
  360. if c <> ')' then
  361. Message(asmr_e_invalid_fpu_register)
  362. else
  363. Begin
  364. actasmpattern:=actasmpattern + c;
  365. c:=current_scanner^.asmgetchar; { let us point to next character. }
  366. end;
  367. end;
  368. if is_register(actasmpattern) then
  369. exit;
  370. Message(asmr_w_modulo_not_supported);
  371. end;
  372. '1'..'9': { integer number }
  373. begin
  374. len:=0;
  375. while c in ['0'..'9'] do
  376. Begin
  377. inc(len);
  378. actasmpattern[len]:=c;
  379. c:=current_scanner^.asmgetchar;
  380. end;
  381. actasmpattern[0]:=chr(len);
  382. actasmpattern:=tostr(ValDecimal(actasmpattern));
  383. actasmtoken:=AS_INTNUM;
  384. exit;
  385. end;
  386. '0' : { octal,hexa,real or binary number. }
  387. begin
  388. actasmpattern:=c;
  389. c:=current_scanner^.asmgetchar;
  390. case upcase(c) of
  391. 'B': { binary }
  392. Begin
  393. c:=current_scanner^.asmgetchar;
  394. while c in ['0','1'] do
  395. Begin
  396. actasmpattern:=actasmpattern + c;
  397. c:=current_scanner^.asmgetchar;
  398. end;
  399. actasmpattern:=tostr(ValBinary(actasmpattern));
  400. actasmtoken:=AS_INTNUM;
  401. exit;
  402. end;
  403. 'D': { real }
  404. Begin
  405. c:=current_scanner^.asmgetchar;
  406. { get ridd of the 0d }
  407. if (c in ['+','-']) then
  408. begin
  409. actasmpattern:=c;
  410. c:=current_scanner^.asmgetchar;
  411. end
  412. else
  413. actasmpattern:='';
  414. while c in ['0'..'9'] do
  415. Begin
  416. actasmpattern:=actasmpattern + c;
  417. c:=current_scanner^.asmgetchar;
  418. end;
  419. if c='.' then
  420. begin
  421. actasmpattern:=actasmpattern + c;
  422. c:=current_scanner^.asmgetchar;
  423. while c in ['0'..'9'] do
  424. Begin
  425. actasmpattern:=actasmpattern + c;
  426. c:=current_scanner^.asmgetchar;
  427. end;
  428. if upcase(c) = 'E' then
  429. begin
  430. actasmpattern:=actasmpattern + c;
  431. c:=current_scanner^.asmgetchar;
  432. if (c in ['+','-']) then
  433. begin
  434. actasmpattern:=actasmpattern + c;
  435. c:=current_scanner^.asmgetchar;
  436. end;
  437. while c in ['0'..'9'] do
  438. Begin
  439. actasmpattern:=actasmpattern + c;
  440. c:=current_scanner^.asmgetchar;
  441. end;
  442. end;
  443. actasmtoken:=AS_REALNUM;
  444. exit;
  445. end
  446. else
  447. begin
  448. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  449. actasmtoken:=AS_NONE;
  450. end;
  451. end;
  452. 'X': { hexadecimal }
  453. Begin
  454. c:=current_scanner^.asmgetchar;
  455. while c in ['0'..'9','a'..'f','A'..'F'] do
  456. Begin
  457. actasmpattern:=actasmpattern + c;
  458. c:=current_scanner^.asmgetchar;
  459. end;
  460. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  461. actasmtoken:=AS_INTNUM;
  462. exit;
  463. end;
  464. '1'..'7': { octal }
  465. begin
  466. actasmpattern:=actasmpattern + c;
  467. while c in ['0'..'7'] do
  468. Begin
  469. actasmpattern:=actasmpattern + c;
  470. c:=current_scanner^.asmgetchar;
  471. end;
  472. actasmpattern:=tostr(ValOctal(actasmpattern));
  473. actasmtoken:=AS_INTNUM;
  474. exit;
  475. end;
  476. else { octal number zero value...}
  477. Begin
  478. actasmpattern:=tostr(ValOctal(actasmpattern));
  479. actasmtoken:=AS_INTNUM;
  480. exit;
  481. end;
  482. end; { end case }
  483. end;
  484. '&' :
  485. begin
  486. c:=current_scanner^.asmgetchar;
  487. actasmtoken:=AS_AND;
  488. end;
  489. '''' : { char }
  490. begin
  491. actasmpattern:='';
  492. repeat
  493. c:=current_scanner^.asmgetchar;
  494. case c of
  495. '\' :
  496. begin
  497. { copy also the next char so \" is parsed correctly }
  498. c:=current_scanner^.asmgetchar;
  499. actasmpattern:=actasmpattern+c;
  500. end;
  501. '''' :
  502. begin
  503. c:=current_scanner^.asmgetchar;
  504. break;
  505. end;
  506. newline:
  507. Message(scan_f_string_exceeds_line);
  508. else
  509. actasmpattern:=actasmpattern+c;
  510. end;
  511. until false;
  512. actasmpattern:=EscapeToPascal(actasmpattern);
  513. actasmtoken:=AS_STRING;
  514. exit;
  515. end;
  516. '"' : { string }
  517. begin
  518. actasmpattern:='';
  519. repeat
  520. c:=current_scanner^.asmgetchar;
  521. case c of
  522. '\' :
  523. begin
  524. { copy also the next char so \" is parsed correctly }
  525. c:=current_scanner^.asmgetchar;
  526. actasmpattern:=actasmpattern+c;
  527. end;
  528. '"' :
  529. begin
  530. c:=current_scanner^.asmgetchar;
  531. break;
  532. end;
  533. newline:
  534. Message(scan_f_string_exceeds_line);
  535. else
  536. actasmpattern:=actasmpattern+c;
  537. end;
  538. until false;
  539. actasmpattern:=EscapeToPascal(actasmpattern);
  540. actasmtoken:=AS_STRING;
  541. exit;
  542. end;
  543. '$' :
  544. begin
  545. actasmtoken:=AS_DOLLAR;
  546. c:=current_scanner^.asmgetchar;
  547. exit;
  548. end;
  549. ',' :
  550. begin
  551. actasmtoken:=AS_COMMA;
  552. c:=current_scanner^.asmgetchar;
  553. exit;
  554. end;
  555. '<' :
  556. begin
  557. actasmtoken:=AS_SHL;
  558. c:=current_scanner^.asmgetchar;
  559. if c = '<' then
  560. c:=current_scanner^.asmgetchar;
  561. exit;
  562. end;
  563. '>' :
  564. begin
  565. actasmtoken:=AS_SHL;
  566. c:=current_scanner^.asmgetchar;
  567. if c = '>' then
  568. c:=current_scanner^.asmgetchar;
  569. exit;
  570. end;
  571. '|' :
  572. begin
  573. actasmtoken:=AS_OR;
  574. c:=current_scanner^.asmgetchar;
  575. exit;
  576. end;
  577. '^' :
  578. begin
  579. actasmtoken:=AS_XOR;
  580. c:=current_scanner^.asmgetchar;
  581. exit;
  582. end;
  583. '!' :
  584. begin
  585. Message(asmr_e_nor_not_supported);
  586. c:=current_scanner^.asmgetchar;
  587. actasmtoken:=AS_NONE;
  588. exit;
  589. end;
  590. '(' :
  591. begin
  592. actasmtoken:=AS_LPAREN;
  593. c:=current_scanner^.asmgetchar;
  594. exit;
  595. end;
  596. ')' :
  597. begin
  598. actasmtoken:=AS_RPAREN;
  599. c:=current_scanner^.asmgetchar;
  600. exit;
  601. end;
  602. ':' :
  603. begin
  604. actasmtoken:=AS_COLON;
  605. c:=current_scanner^.asmgetchar;
  606. exit;
  607. end;
  608. '+' :
  609. begin
  610. actasmtoken:=AS_PLUS;
  611. c:=current_scanner^.asmgetchar;
  612. exit;
  613. end;
  614. '-' :
  615. begin
  616. actasmtoken:=AS_MINUS;
  617. c:=current_scanner^.asmgetchar;
  618. exit;
  619. end;
  620. '*' :
  621. begin
  622. actasmtoken:=AS_STAR;
  623. c:=current_scanner^.asmgetchar;
  624. exit;
  625. end;
  626. '/' :
  627. begin
  628. c:=current_scanner^.asmgetchar;
  629. actasmtoken:=AS_SLASH;
  630. exit;
  631. end;
  632. '{',#13,newline,';' :
  633. begin
  634. { the comment is read by asmgetchar }
  635. c:=current_scanner^.asmgetchar;
  636. firsttoken:=TRUE;
  637. actasmtoken:=AS_SEPARATOR;
  638. exit;
  639. end;
  640. else
  641. Begin
  642. Message(scan_f_illegal_char);
  643. end;
  644. end;
  645. end;
  646. end;
  647. function consume(t : tasmtoken):boolean;
  648. begin
  649. Consume:=true;
  650. if t<>actasmtoken then
  651. begin
  652. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  653. Consume:=false;
  654. end;
  655. repeat
  656. gettoken;
  657. until actasmtoken<>AS_NONE;
  658. end;
  659. procedure RecoverConsume(allowcomma:boolean);
  660. begin
  661. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  662. begin
  663. if allowcomma and (actasmtoken=AS_COMMA) then
  664. break;
  665. Consume(actasmtoken);
  666. end;
  667. end;
  668. {*****************************************************************************
  669. Parsing Helpers
  670. *****************************************************************************}
  671. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  672. { Description: This routine builds up a record offset after a AS_DOT }
  673. { token is encountered. }
  674. { On entry actasmtoken should be equal to AS_DOT }
  675. var
  676. s : string;
  677. Begin
  678. offset:=0;
  679. size:=0;
  680. s:=expr;
  681. while (actasmtoken=AS_DOT) do
  682. begin
  683. Consume(AS_DOT);
  684. if actasmtoken=AS_ID then
  685. s:=s+'.'+actasmpattern;
  686. if not Consume(AS_ID) then
  687. begin
  688. RecoverConsume(true);
  689. break;
  690. end;
  691. end;
  692. if not GetRecordOffsetSize(s,offset,size) then
  693. Message(asmr_e_building_record_offset);
  694. end;
  695. Procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:longint;var asmsym:string);
  696. var
  697. hs,tempstr,expr : string;
  698. parenlevel,l,k : longint;
  699. errorflag : boolean;
  700. prevtok : tasmtoken;
  701. sym : psym;
  702. hl : PAsmLabel;
  703. Begin
  704. asmsym:='';
  705. value:=0;
  706. errorflag:=FALSE;
  707. tempstr:='';
  708. expr:='';
  709. inexpression:=TRUE;
  710. parenlevel:=0;
  711. Repeat
  712. Case actasmtoken of
  713. AS_LPAREN:
  714. Begin
  715. { Exit if ref? }
  716. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  717. break;
  718. Consume(AS_LPAREN);
  719. expr:=expr + '(';
  720. inc(parenlevel);
  721. end;
  722. AS_RPAREN:
  723. Begin
  724. { end of ref ? }
  725. if (parenlevel=0) and betweenbracket then
  726. break;
  727. Consume(AS_RPAREN);
  728. expr:=expr + ')';
  729. dec(parenlevel);
  730. end;
  731. AS_SHL:
  732. Begin
  733. Consume(AS_SHL);
  734. expr:=expr + '<';
  735. end;
  736. AS_SHR:
  737. Begin
  738. Consume(AS_SHR);
  739. expr:=expr + '>';
  740. end;
  741. AS_SLASH:
  742. Begin
  743. Consume(AS_SLASH);
  744. expr:=expr + '/';
  745. end;
  746. AS_MOD:
  747. Begin
  748. Consume(AS_MOD);
  749. expr:=expr + '%';
  750. end;
  751. AS_STAR:
  752. Begin
  753. Consume(AS_STAR);
  754. expr:=expr + '*';
  755. end;
  756. AS_PLUS:
  757. Begin
  758. Consume(AS_PLUS);
  759. expr:=expr + '+';
  760. end;
  761. AS_MINUS:
  762. Begin
  763. Consume(AS_MINUS);
  764. expr:=expr + '-';
  765. end;
  766. AS_AND:
  767. Begin
  768. Consume(AS_AND);
  769. expr:=expr + '&';
  770. end;
  771. AS_NOT:
  772. Begin
  773. Consume(AS_NOT);
  774. expr:=expr + '~';
  775. end;
  776. AS_XOR:
  777. Begin
  778. Consume(AS_XOR);
  779. expr:=expr + '^';
  780. end;
  781. AS_OR:
  782. Begin
  783. Consume(AS_OR);
  784. expr:=expr + '|';
  785. end;
  786. AS_INTNUM:
  787. Begin
  788. expr:=expr + actasmpattern;
  789. Consume(AS_INTNUM);
  790. end;
  791. AS_DOLLAR:
  792. begin
  793. Consume(AS_DOLLAR);
  794. if actasmtoken<>AS_ID then
  795. Comment(V_Error,'assem_e_dollar_without_identifier');
  796. end;
  797. AS_ID:
  798. Begin
  799. tempstr:=actasmpattern;
  800. prevtok:=prevasmtoken;
  801. consume(AS_ID);
  802. if actasmtoken=AS_DOT then
  803. begin
  804. BuildRecordOffsetSize(tempstr,l,k);
  805. str(l, tempstr);
  806. expr:=expr + tempstr;
  807. end
  808. else
  809. if SearchIConstant(tempstr,l) then
  810. begin
  811. str(l, tempstr);
  812. expr:=expr + tempstr;
  813. end
  814. else
  815. begin
  816. hs:='';
  817. if is_locallabel(tempstr) then
  818. begin
  819. CreateLocalLabel(tempstr,hl,false);
  820. hs:=hl^.name
  821. end
  822. else
  823. if SearchLabel(tempstr,hl,false) then
  824. hs:=hl^.name
  825. else
  826. begin
  827. getsym(tempstr,false);
  828. sym:=srsym;
  829. if assigned(sym) then
  830. begin
  831. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  832. Message(asmr_e_no_local_or_para_allowed);
  833. case srsym^.typ of
  834. varsym :
  835. hs:=pvarsym(srsym)^.mangledname;
  836. typedconstsym :
  837. hs:=ptypedconstsym(srsym)^.mangledname;
  838. procsym :
  839. hs:=pprocsym(srsym)^.mangledname;
  840. else
  841. Message(asmr_e_wrong_sym_type);
  842. end;
  843. end
  844. else
  845. Message1(sym_e_unknown_id,tempstr);
  846. end;
  847. { symbol found? }
  848. if hs<>'' then
  849. begin
  850. if needofs and (prevtok<>AS_DOLLAR) then
  851. Message(asmr_e_need_offset);
  852. if asmsym='' then
  853. asmsym:=hs
  854. else
  855. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  856. if (expr='') or (expr[length(expr)]='+') then
  857. begin
  858. delete(expr,length(expr),1);
  859. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  860. Message(asmr_e_only_add_relocatable_symbol);
  861. end
  862. else
  863. Message(asmr_e_only_add_relocatable_symbol);
  864. end
  865. else
  866. begin
  867. { Error recovery }
  868. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  869. delete(expr,length(expr),1);
  870. end;
  871. end;
  872. end;
  873. AS_SEPARATOR,
  874. AS_COMMA:
  875. Begin
  876. break;
  877. end;
  878. else
  879. Begin
  880. { write error only once. }
  881. if not errorflag then
  882. Message(asmr_e_invalid_constant_expression);
  883. { consume tokens until we find COMMA or SEPARATOR }
  884. Consume(actasmtoken);
  885. errorflag:=TRUE;
  886. end;
  887. end;
  888. Until false;
  889. { calculate expression }
  890. if not ErrorFlag then
  891. value:=CalculateExpression(expr)
  892. else
  893. value:=0;
  894. { no longer in an expression }
  895. inexpression:=FALSE;
  896. end;
  897. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  898. var
  899. l : longint;
  900. hs : string;
  901. begin
  902. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  903. if hs<>'' then
  904. Message(asmr_e_relocatable_symbol_not_allowed);
  905. BuildConstExpression:=l;
  906. end;
  907. {****************************************************************************
  908. T386ATTOperand
  909. ****************************************************************************}
  910. type
  911. P386ATTOperand=^T386ATTOperand;
  912. T386ATTOperand=object(T386Operand)
  913. Procedure BuildOperand;virtual;
  914. private
  915. Procedure BuildReference;
  916. Procedure BuildConstant;
  917. end;
  918. Procedure T386ATTOperand.BuildReference;
  919. procedure Consume_RParen;
  920. begin
  921. if actasmtoken <> AS_RPAREN then
  922. Begin
  923. Message(asmr_e_invalid_reference_syntax);
  924. RecoverConsume(true);
  925. end
  926. else
  927. begin
  928. Consume(AS_RPAREN);
  929. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  930. Begin
  931. Message(asmr_e_invalid_reference_syntax);
  932. RecoverConsume(true);
  933. end;
  934. end;
  935. end;
  936. procedure Consume_Scale;
  937. var
  938. l : longint;
  939. begin
  940. { we have to process the scaling }
  941. l:=BuildConstExpression(false,true);
  942. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  943. opr.ref.scalefactor:=l
  944. else
  945. Begin
  946. Message(asmr_e_wrong_scale_factor);
  947. opr.ref.scalefactor:=0;
  948. end;
  949. end;
  950. Begin
  951. Consume(AS_LPAREN);
  952. Case actasmtoken of
  953. AS_INTNUM,
  954. AS_MINUS,
  955. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  956. Begin
  957. { offset(offset) is invalid }
  958. If opr.Ref.Offset <> 0 Then
  959. Begin
  960. Message(asmr_e_invalid_reference_syntax);
  961. RecoverConsume(true);
  962. End
  963. Else
  964. Begin
  965. opr.Ref.Offset:=BuildConstExpression(false,true);
  966. Consume_RParen;
  967. end;
  968. exit;
  969. End;
  970. AS_REGISTER: { (reg ... }
  971. Begin
  972. { Check if there is already a base (mostly ebp,esp) than this is
  973. not allowed,becuase it will give crashing code }
  974. if opr.ref.base<>R_NO then
  975. Message(asmr_e_cannot_index_relative_var);
  976. opr.ref.base:=actasmregister;
  977. Consume(AS_REGISTER);
  978. { can either be a register or a right parenthesis }
  979. { (reg) }
  980. if actasmtoken=AS_RPAREN then
  981. Begin
  982. Consume_RParen;
  983. exit;
  984. end;
  985. { (reg,reg .. }
  986. Consume(AS_COMMA);
  987. if actasmtoken=AS_REGISTER then
  988. Begin
  989. opr.ref.index:=actasmregister;
  990. Consume(AS_REGISTER);
  991. { check for scaling ... }
  992. case actasmtoken of
  993. AS_RPAREN:
  994. Begin
  995. Consume_RParen;
  996. exit;
  997. end;
  998. AS_COMMA:
  999. Begin
  1000. Consume(AS_COMMA);
  1001. Consume_Scale;
  1002. Consume_RParen;
  1003. end;
  1004. else
  1005. Begin
  1006. Message(asmr_e_invalid_reference_syntax);
  1007. RecoverConsume(false);
  1008. end;
  1009. end; { end case }
  1010. end
  1011. else
  1012. Begin
  1013. Message(asmr_e_invalid_reference_syntax);
  1014. RecoverConsume(false);
  1015. end;
  1016. end; {end case }
  1017. AS_COMMA: { (, ... can either be scaling, or index }
  1018. Begin
  1019. Consume(AS_COMMA);
  1020. { Index }
  1021. if (actasmtoken=AS_REGISTER) then
  1022. Begin
  1023. opr.ref.index:=actasmregister;
  1024. Consume(AS_REGISTER);
  1025. { check for scaling ... }
  1026. case actasmtoken of
  1027. AS_RPAREN:
  1028. Begin
  1029. Consume_RParen;
  1030. exit;
  1031. end;
  1032. AS_COMMA:
  1033. Begin
  1034. Consume(AS_COMMA);
  1035. Consume_Scale;
  1036. Consume_RParen;
  1037. end;
  1038. else
  1039. Begin
  1040. Message(asmr_e_invalid_reference_syntax);
  1041. RecoverConsume(false);
  1042. end;
  1043. end; {end case }
  1044. end
  1045. { Scaling }
  1046. else
  1047. Begin
  1048. Consume_Scale;
  1049. Consume_RParen;
  1050. exit;
  1051. end;
  1052. end;
  1053. else
  1054. Begin
  1055. Message(asmr_e_invalid_reference_syntax);
  1056. RecoverConsume(false);
  1057. end;
  1058. end;
  1059. end;
  1060. Procedure T386ATTOperand.BuildConstant;
  1061. var
  1062. l : longint;
  1063. tempstr : string;
  1064. begin
  1065. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1066. if tempstr<>'' then
  1067. begin
  1068. opr.typ:=OPR_SYMBOL;
  1069. opr.symofs:=l;
  1070. opr.symbol:=newasmsymbol(tempstr);
  1071. end
  1072. else
  1073. begin
  1074. opr.typ:=OPR_CONSTANT;
  1075. opr.val:=l;
  1076. end;
  1077. end;
  1078. Procedure T386ATTOperand.BuildOperand;
  1079. procedure AddLabelOperand(hl:pasmlabel);
  1080. begin
  1081. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  1082. is_calljmp(actopcode) then
  1083. begin
  1084. opr.typ:=OPR_SYMBOL;
  1085. opr.symbol:=hl;
  1086. end
  1087. else
  1088. begin
  1089. InitRef;
  1090. opr.ref.symbol:=hl;
  1091. end;
  1092. end;
  1093. function MaybeBuildReference:boolean;
  1094. { Try to create a reference, if not a reference is found then false
  1095. is returned }
  1096. begin
  1097. MaybeBuildReference:=true;
  1098. case actasmtoken of
  1099. AS_INTNUM,
  1100. AS_MINUS,
  1101. AS_PLUS:
  1102. Begin
  1103. opr.ref.offset:=BuildConstExpression(True,False);
  1104. if actasmtoken<>AS_LPAREN then
  1105. Message(asmr_e_invalid_reference_syntax)
  1106. else
  1107. BuildReference;
  1108. end;
  1109. AS_LPAREN:
  1110. BuildReference;
  1111. AS_ID: { only a variable is allowed ... }
  1112. Begin
  1113. if not SetupVar(actasmpattern) then
  1114. Message(asmr_e_invalid_reference_syntax);
  1115. Consume(actasmtoken);
  1116. case actasmtoken of
  1117. AS_SEPARATOR,
  1118. AS_COMMA: ;
  1119. AS_LPAREN: BuildReference;
  1120. else
  1121. Begin
  1122. Message(asmr_e_invalid_reference_syntax);
  1123. Consume(actasmtoken);
  1124. end;
  1125. end; {end case }
  1126. end;
  1127. else
  1128. MaybeBuildReference:=false;
  1129. end; { end case }
  1130. end;
  1131. var
  1132. expr,
  1133. tempstr : string;
  1134. tempreg : tregister;
  1135. hl : PAsmLabel;
  1136. tsize,l,
  1137. toffset : longint;
  1138. Begin
  1139. tempstr:='';
  1140. expr:='';
  1141. case actasmtoken of
  1142. AS_LPAREN: { Memory reference or constant expression }
  1143. Begin
  1144. InitRef;
  1145. BuildReference;
  1146. end;
  1147. AS_DOLLAR: { Constant expression }
  1148. Begin
  1149. Consume(AS_DOLLAR);
  1150. BuildConstant;
  1151. end;
  1152. AS_INTNUM,
  1153. AS_MINUS,
  1154. AS_PLUS:
  1155. Begin
  1156. { Constant memory offset }
  1157. { This must absolutely be followed by ( }
  1158. InitRef;
  1159. opr.ref.offset:=BuildConstExpression(True,False);
  1160. if actasmtoken<>AS_LPAREN then
  1161. Message(asmr_e_invalid_reference_syntax)
  1162. else
  1163. BuildReference;
  1164. end;
  1165. AS_STAR: { Call from memory address }
  1166. Begin
  1167. Consume(AS_STAR);
  1168. if actasmtoken=AS_REGISTER then
  1169. begin
  1170. opr.typ:=OPR_REGISTER;
  1171. opr.reg:=actasmregister;
  1172. size:=reg_2_opsize[actasmregister];
  1173. Consume(AS_REGISTER);
  1174. end
  1175. else
  1176. begin
  1177. InitRef;
  1178. if not MaybeBuildReference then
  1179. Message(asmr_e_syn_operand);
  1180. end;
  1181. { this is only allowed for call's and jmp's }
  1182. if not is_calljmp(actopcode) then
  1183. Message(asmr_e_syn_operand);
  1184. end;
  1185. AS_ID: { A constant expression, or a Variable ref. }
  1186. Begin
  1187. { Local Label ? }
  1188. if is_locallabel(actasmpattern) then
  1189. begin
  1190. CreateLocalLabel(actasmpattern,hl,false);
  1191. Consume(AS_ID);
  1192. AddLabelOperand(hl);
  1193. end
  1194. else
  1195. { Check for label }
  1196. if SearchLabel(actasmpattern,hl,false) then
  1197. begin
  1198. Consume(AS_ID);
  1199. AddLabelOperand(hl);
  1200. end
  1201. else
  1202. { probably a variable or normal expression }
  1203. { or a procedure (such as in CALL ID) }
  1204. Begin
  1205. InitRef;
  1206. if not SetupVar(actasmpattern) then
  1207. Begin
  1208. { look for special symbols ... }
  1209. if actasmpattern = '__RESULT' then
  1210. SetUpResult
  1211. else
  1212. if actasmpattern = '__SELF' then
  1213. SetupSelf
  1214. else
  1215. if actasmpattern = '__OLDEBP' then
  1216. SetupOldEBP
  1217. else
  1218. { check for direct symbolic names }
  1219. { only if compiling the system unit }
  1220. if (cs_compilesystem in aktmoduleswitches) then
  1221. begin
  1222. if not SetupDirectVar(actasmpattern) then
  1223. Begin
  1224. { not found, finally ... add it anyways ... }
  1225. Message1(asmr_w_id_supposed_external,actasmpattern);
  1226. opr.ref.symbol:=newasmsymbol(actasmpattern);
  1227. end;
  1228. end
  1229. else
  1230. Message1(sym_e_unknown_id,actasmpattern);
  1231. end;
  1232. { constant expression? }
  1233. if (opr.typ=OPR_CONSTANT) then
  1234. begin
  1235. l:=BuildConstExpression(true,false);
  1236. { indexing? }
  1237. if actasmtoken=AS_LPAREN then
  1238. begin
  1239. opr.typ:=OPR_REFERENCE;
  1240. reset_reference(opr.Ref);
  1241. opr.Ref.Offset:=l;
  1242. BuildReference;
  1243. end
  1244. else
  1245. opr.Val:=l;
  1246. end
  1247. else
  1248. begin
  1249. expr:=actasmpattern;
  1250. Consume(AS_ID);
  1251. if actasmtoken=AS_DOT then
  1252. begin
  1253. BuildRecordOffsetSize(expr,toffset,tsize);
  1254. inc(opr.ref.offset,toffset);
  1255. SetSize(tsize);
  1256. end;
  1257. end;
  1258. end;
  1259. if opr.typ=OPR_REFERENCE then
  1260. begin
  1261. { Do we have a +[constant] ? }
  1262. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1263. inc(opr.ref.offset,BuildConstExpression(true,false));
  1264. { Do we have a indexing reference, then parse it also }
  1265. if actasmtoken=AS_LPAREN then
  1266. BuildReference;
  1267. end;
  1268. end;
  1269. AS_REGISTER: { Register, a variable reference or a constant reference }
  1270. Begin
  1271. { save the type of register used. }
  1272. tempreg:=actasmregister;
  1273. Consume(AS_REGISTER);
  1274. if actasmtoken = AS_COLON then
  1275. Begin
  1276. Consume(AS_COLON);
  1277. InitRef;
  1278. opr.ref.segment:=tempreg;
  1279. { This must absolutely be followed by a reference }
  1280. if not MaybeBuildReference then
  1281. Begin
  1282. Message(asmr_e_invalid_seg_override);
  1283. Consume(actasmtoken);
  1284. end;
  1285. end
  1286. { Simple register }
  1287. else if (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1288. Begin
  1289. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1290. Message(asmr_e_invalid_operand_type);
  1291. opr.typ:=OPR_REGISTER;
  1292. opr.reg:=tempreg;
  1293. size:=reg_2_opsize[tempreg];
  1294. end
  1295. else
  1296. Message(asmr_e_syn_operand);
  1297. end;
  1298. AS_SEPARATOR,
  1299. AS_COMMA: ;
  1300. else
  1301. Begin
  1302. Message(asmr_e_syn_operand);
  1303. Consume(actasmtoken);
  1304. end;
  1305. end; { end case }
  1306. end;
  1307. {*****************************************************************************
  1308. T386ATTInstruction
  1309. *****************************************************************************}
  1310. type
  1311. P386AttInstruction=^T386AttInstruction;
  1312. T386AttInstruction=object(T386Instruction)
  1313. procedure InitOperands;virtual;
  1314. procedure BuildOpcode;virtual;
  1315. end;
  1316. procedure T386AttInstruction.InitOperands;
  1317. var
  1318. i : longint;
  1319. begin
  1320. for i:=1to 3 do
  1321. Operands[i]:=new(P386AttOperand,Init);
  1322. end;
  1323. Procedure T386AttInstruction.BuildOpCode;
  1324. var
  1325. operandnum : longint;
  1326. PrefixOp,OverrideOp: tasmop;
  1327. expr : string;
  1328. Begin
  1329. expr:='';
  1330. PrefixOp:=A_None;
  1331. OverrideOp:=A_None;
  1332. { prefix seg opcode / prefix opcode }
  1333. repeat
  1334. if is_prefix(actopcode) then
  1335. begin
  1336. PrefixOp:=ActOpcode;
  1337. opcode:=ActOpcode;
  1338. condition:=ActCondition;
  1339. opsize:=ActOpsize;
  1340. ConcatInstruction(curlist);
  1341. Consume(AS_OPCODE);
  1342. end
  1343. else
  1344. if is_override(actopcode) then
  1345. begin
  1346. OverrideOp:=ActOpcode;
  1347. opcode:=ActOpcode;
  1348. condition:=ActCondition;
  1349. opsize:=ActOpsize;
  1350. ConcatInstruction(curlist);
  1351. Consume(AS_OPCODE);
  1352. end
  1353. else
  1354. break;
  1355. { allow for newline as in gas styled syntax }
  1356. while actasmtoken=AS_SEPARATOR do
  1357. Consume(AS_SEPARATOR);
  1358. until (actasmtoken<>AS_OPCODE);
  1359. { opcode }
  1360. if (actasmtoken <> AS_OPCODE) then
  1361. Begin
  1362. Message(asmr_e_invalid_or_missing_opcode);
  1363. RecoverConsume(true);
  1364. exit;
  1365. end;
  1366. { Fill the instr object with the current state }
  1367. Opcode:=ActOpcode;
  1368. condition:=ActCondition;
  1369. opsize:=ActOpsize;
  1370. { Valid combination of prefix/override and instruction ? }
  1371. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1372. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1373. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1374. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1375. { We are reading operands, so opcode will be an AS_ID }
  1376. operandnum:=1;
  1377. Consume(AS_OPCODE);
  1378. { Zero operand opcode ? }
  1379. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1380. begin
  1381. operandnum:=0;
  1382. exit;
  1383. end;
  1384. { Read the operands }
  1385. repeat
  1386. case actasmtoken of
  1387. AS_COMMA: { Operand delimiter }
  1388. Begin
  1389. if operandnum > MaxOperands then
  1390. Message(asmr_e_too_many_operands)
  1391. else
  1392. Inc(operandnum);
  1393. Consume(AS_COMMA);
  1394. end;
  1395. AS_SEPARATOR,
  1396. AS_END : { End of asm operands for this opcode }
  1397. begin
  1398. break;
  1399. end;
  1400. else
  1401. Operands[operandnum]^.BuildOperand;
  1402. end; { end case }
  1403. until false;
  1404. Ops:=operandnum;
  1405. end;
  1406. Procedure BuildConstant(maxvalue: longint);
  1407. var
  1408. strlength: byte;
  1409. asmsym,
  1410. expr: string;
  1411. value : longint;
  1412. Begin
  1413. Repeat
  1414. Case actasmtoken of
  1415. AS_STRING:
  1416. Begin
  1417. if maxvalue = $ff then
  1418. strlength:=1
  1419. else
  1420. Message(asmr_e_string_not_allowed_as_const);
  1421. expr:=actasmpattern;
  1422. if length(expr) > 1 then
  1423. Message(asmr_e_string_not_allowed_as_const);
  1424. Consume(AS_STRING);
  1425. Case actasmtoken of
  1426. AS_COMMA: Consume(AS_COMMA);
  1427. AS_SEPARATOR: ;
  1428. else
  1429. Message(asmr_e_invalid_string_expression);
  1430. end; { end case }
  1431. ConcatString(curlist,expr);
  1432. end;
  1433. AS_INTNUM,
  1434. AS_PLUS,
  1435. AS_MINUS,
  1436. AS_LPAREN,
  1437. AS_NOT,
  1438. AS_ID :
  1439. Begin
  1440. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1441. if asmsym<>'' then
  1442. begin
  1443. if maxvalue<>$ffffffff then
  1444. Comment(V_Warning,'32bit constant created for address');
  1445. ConcatConstSymbol(curlist,asmsym,value)
  1446. end
  1447. else
  1448. ConcatConstant(curlist,value,maxvalue);
  1449. end;
  1450. AS_COMMA:
  1451. Consume(AS_COMMA);
  1452. AS_SEPARATOR:
  1453. break;
  1454. else
  1455. begin
  1456. Message(asmr_e_syn_constant);
  1457. RecoverConsume(false);
  1458. end
  1459. end; { end case }
  1460. Until false;
  1461. end;
  1462. Procedure BuildRealConstant(typ : tfloattype);
  1463. var
  1464. expr : string;
  1465. r : bestreal;
  1466. code : integer;
  1467. negativ : boolean;
  1468. errorflag: boolean;
  1469. Begin
  1470. errorflag:=FALSE;
  1471. Repeat
  1472. negativ:=false;
  1473. expr:='';
  1474. if actasmtoken=AS_PLUS then
  1475. Consume(AS_PLUS)
  1476. else
  1477. if actasmtoken=AS_MINUS then
  1478. begin
  1479. negativ:=true;
  1480. consume(AS_MINUS);
  1481. end;
  1482. Case actasmtoken of
  1483. AS_INTNUM:
  1484. Begin
  1485. expr:=actasmpattern;
  1486. Consume(AS_INTNUM);
  1487. if negativ then
  1488. expr:='-'+expr;
  1489. val(expr,r,code);
  1490. if code<>0 then
  1491. Begin
  1492. r:=0;
  1493. Message(asmr_e_invalid_float_expr);
  1494. End;
  1495. ConcatRealConstant(curlist,r,typ);
  1496. end;
  1497. AS_REALNUM:
  1498. Begin
  1499. expr:=actasmpattern;
  1500. Consume(AS_REALNUM);
  1501. { in ATT syntax you have 0d in front of the real }
  1502. { should this be forced ? yes i think so, as to }
  1503. { conform to gas as much as possible. }
  1504. if (expr[1]='0') and (upper(expr[2])='D') then
  1505. Delete(expr,1,2);
  1506. if negativ then
  1507. expr:='-'+expr;
  1508. val(expr,r,code);
  1509. if code<>0 then
  1510. Begin
  1511. r:=0;
  1512. Message(asmr_e_invalid_float_expr);
  1513. End;
  1514. ConcatRealConstant(curlist,r,typ);
  1515. end;
  1516. AS_COMMA:
  1517. begin
  1518. Consume(AS_COMMA);
  1519. end;
  1520. AS_SEPARATOR:
  1521. begin
  1522. break;
  1523. end;
  1524. else
  1525. Begin
  1526. Consume(actasmtoken);
  1527. if not errorflag then
  1528. Message(asmr_e_invalid_float_expr);
  1529. errorflag:=TRUE;
  1530. end;
  1531. end;
  1532. Until false;
  1533. end;
  1534. Procedure BuildStringConstant(asciiz: boolean);
  1535. var
  1536. expr: string;
  1537. errorflag : boolean;
  1538. Begin
  1539. errorflag:=FALSE;
  1540. Repeat
  1541. Case actasmtoken of
  1542. AS_STRING:
  1543. Begin
  1544. expr:=actasmpattern;
  1545. if asciiz then
  1546. expr:=expr+#0;
  1547. ConcatPasString(curlist,expr);
  1548. Consume(AS_STRING);
  1549. end;
  1550. AS_COMMA:
  1551. begin
  1552. Consume(AS_COMMA);
  1553. end;
  1554. AS_SEPARATOR:
  1555. begin
  1556. break;
  1557. end;
  1558. else
  1559. Begin
  1560. Consume(actasmtoken);
  1561. if not errorflag then
  1562. Message(asmr_e_invalid_string_expression);
  1563. errorflag:=TRUE;
  1564. end;
  1565. end;
  1566. Until false;
  1567. end;
  1568. Function Assemble: Ptree;
  1569. Var
  1570. hl : PAsmLabel;
  1571. commname : string;
  1572. lastsec : tsection;
  1573. instr : T386ATTInstruction;
  1574. Begin
  1575. Message1(asmr_d_start_reading,'AT&T');
  1576. firsttoken:=TRUE;
  1577. if assigned(procinfo.retdef) and
  1578. (is_fpu(procinfo.retdef) or
  1579. ret_in_acc(procinfo.retdef)) then
  1580. procinfo.funcret_is_valid:=true;
  1581. { sets up all opcode and register tables in uppercase }
  1582. if not _asmsorted then
  1583. Begin
  1584. SetupTables;
  1585. _asmsorted:=TRUE;
  1586. end;
  1587. curlist:=new(paasmoutput,init);
  1588. lastsec:=sec_code;
  1589. { setup label linked list }
  1590. new(LocalLabelList,Init);
  1591. { start tokenizer }
  1592. c:=current_scanner^.asmgetchar;
  1593. gettoken;
  1594. { main loop }
  1595. repeat
  1596. case actasmtoken of
  1597. AS_LLABEL:
  1598. Begin
  1599. if CreateLocalLabel(actasmpattern,hl,true) then
  1600. ConcatLabel(curlist,hl);
  1601. Consume(AS_LLABEL);
  1602. end;
  1603. AS_LABEL:
  1604. Begin
  1605. if SearchLabel(upper(actasmpattern),hl,true) then
  1606. ConcatLabel(curlist,hl)
  1607. else
  1608. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1609. Consume(AS_LABEL);
  1610. end;
  1611. AS_DW:
  1612. Begin
  1613. Consume(AS_DW);
  1614. BuildConstant($ffff);
  1615. end;
  1616. AS_DATA:
  1617. Begin
  1618. curlist^.Concat(new(pai_section,init(sec_data)));
  1619. lastsec:=sec_data;
  1620. Consume(AS_DATA);
  1621. end;
  1622. AS_TEXT:
  1623. Begin
  1624. curlist^.Concat(new(pai_section,init(sec_code)));
  1625. lastsec:=sec_code;
  1626. Consume(AS_TEXT);
  1627. end;
  1628. AS_DB:
  1629. Begin
  1630. Consume(AS_DB);
  1631. BuildConstant($ff);
  1632. end;
  1633. AS_DD:
  1634. Begin
  1635. Consume(AS_DD);
  1636. BuildConstant($ffffffff);
  1637. end;
  1638. AS_DQ:
  1639. Begin
  1640. Consume(AS_DQ);
  1641. BuildRealConstant(s64comp);
  1642. end;
  1643. AS_SINGLE:
  1644. Begin
  1645. Consume(AS_SINGLE);
  1646. BuildRealConstant(s32real);
  1647. end;
  1648. AS_DOUBLE:
  1649. Begin
  1650. Consume(AS_DOUBLE);
  1651. BuildRealConstant(s64real);
  1652. end;
  1653. AS_EXTENDED:
  1654. Begin
  1655. Consume(AS_EXTENDED);
  1656. BuildRealConstant(s80real);
  1657. end;
  1658. AS_GLOBAL:
  1659. Begin
  1660. Consume(AS_GLOBAL);
  1661. if actasmtoken=AS_ID then
  1662. ConcatPublic(curlist,actasmpattern);
  1663. Consume(AS_ID);
  1664. if actasmtoken<>AS_SEPARATOR then
  1665. Consume(AS_SEPARATOR);
  1666. end;
  1667. AS_ALIGN:
  1668. Begin
  1669. Consume(AS_ALIGN);
  1670. ConcatAlign(curlist,BuildConstExpression(false,false));
  1671. if actasmtoken<>AS_SEPARATOR then
  1672. Consume(AS_SEPARATOR);
  1673. end;
  1674. AS_ASCIIZ:
  1675. Begin
  1676. Consume(AS_ASCIIZ);
  1677. BuildStringConstant(TRUE);
  1678. end;
  1679. AS_ASCII:
  1680. Begin
  1681. Consume(AS_ASCII);
  1682. BuildStringConstant(FALSE);
  1683. end;
  1684. AS_LCOMM:
  1685. Begin
  1686. Consume(AS_LCOMM);
  1687. commname:=actasmpattern;
  1688. Consume(AS_ID);
  1689. Consume(AS_COMMA);
  1690. ConcatLocalBss(actasmpattern,BuildConstExpression(false,false));
  1691. if actasmtoken<>AS_SEPARATOR then
  1692. Consume(AS_SEPARATOR);
  1693. end;
  1694. AS_COMM:
  1695. Begin
  1696. Consume(AS_LCOMM);
  1697. commname:=actasmpattern;
  1698. Consume(AS_ID);
  1699. Consume(AS_COMMA);
  1700. ConcatGlobalBss(actasmpattern,BuildConstExpression(false,false));
  1701. if actasmtoken<>AS_SEPARATOR then
  1702. Consume(AS_SEPARATOR);
  1703. end;
  1704. AS_OPCODE:
  1705. Begin
  1706. instr.init;
  1707. instr.BuildOpcode;
  1708. instr.AddReferenceSizes;
  1709. instr.SetInstructionOpsize;
  1710. instr.CheckOperandSizes;
  1711. instr.ConcatInstruction(curlist);
  1712. instr.done;
  1713. end;
  1714. AS_SEPARATOR:
  1715. Begin
  1716. Consume(AS_SEPARATOR);
  1717. end;
  1718. AS_END:
  1719. begin
  1720. break; { end assembly block }
  1721. end;
  1722. else
  1723. Begin
  1724. Message(asmr_e_syntax_error);
  1725. RecoverConsume(false);
  1726. end;
  1727. end;
  1728. until false;
  1729. { Check LocalLabelList }
  1730. LocalLabelList^.CheckEmitted;
  1731. dispose(LocalLabelList,Done);
  1732. { are we back in the code section? }
  1733. if lastsec<>sec_code then
  1734. begin
  1735. Message(asmr_w_assembler_code_not_returned_to_text);
  1736. curlist^.Concat(new(pai_section,init(sec_code)));
  1737. end;
  1738. { Return the list in an asmnode }
  1739. assemble:=genasmnode(curlist);
  1740. Message1(asmr_d_finish_reading,'AT&T');
  1741. end;
  1742. {*****************************************************************************
  1743. Initialize
  1744. *****************************************************************************}
  1745. var
  1746. old_exit : pointer;
  1747. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1748. begin
  1749. if assigned(iasmops) then
  1750. dispose(iasmops);
  1751. if assigned(iasmregs) then
  1752. dispose(iasmregs);
  1753. exitproc:=old_exit;
  1754. end;
  1755. begin
  1756. old_exit:=exitproc;
  1757. exitproc:=@ra386att_exit;
  1758. end.
  1759. {
  1760. $Log$
  1761. Revision 1.48 1999-05-27 19:44:56 peter
  1762. * removed oldasm
  1763. * plabel -> pasmlabel
  1764. * -a switches to source writing automaticly
  1765. * assembler readers OOPed
  1766. * asmsymbol automaticly external
  1767. * jumptables and other label fixes for asm readers
  1768. Revision 1.47 1999/05/21 13:55:13 peter
  1769. * NEWLAB for label as symbol
  1770. Revision 1.46 1999/05/12 00:19:56 peter
  1771. * removed R_DEFAULT_SEG
  1772. * uniform float names
  1773. Revision 1.45 1999/05/06 09:05:25 peter
  1774. * generic write_float and str_float
  1775. * fixed constant float conversions
  1776. Revision 1.44 1999/05/05 22:22:00 peter
  1777. * updated messages
  1778. Revision 1.43 1999/05/04 21:45:01 florian
  1779. * changes to compile it with Delphi 4.0
  1780. Revision 1.42 1999/05/02 14:25:07 peter
  1781. * only allow *<reg/ref> when call/jmp is used
  1782. Revision 1.41 1999/05/01 13:48:39 peter
  1783. * merged nasm compiler
  1784. Revision 1.7 1999/04/29 09:37:47 peter
  1785. * fixed var+const support
  1786. Revision 1.6 1999/04/26 23:26:17 peter
  1787. * redesigned record offset parsing to support nested records
  1788. * normal compiler uses the redesigned createvarinstr()
  1789. Revision 1.5 1999/04/20 11:01:23 peter
  1790. * better tokenpos info
  1791. Revision 1.4 1999/04/14 09:07:45 peter
  1792. * asm reader improvements
  1793. Revision 1.3 1999/03/06 17:24:26 peter
  1794. * rewritten intel parser a lot, especially reference reading
  1795. * size checking added for asm parsers
  1796. Revision 1.2 1999/03/02 02:56:30 peter
  1797. + stabs support for binary writers
  1798. * more fixes and missing updates from the previous commit :(
  1799. Revision 1.1 1999/03/01 15:46:26 peter
  1800. * ag386bin finally make cycles correct
  1801. * prefixes are now also normal opcodes
  1802. }