ra386att.pas 55 KB

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