ra386att.pas 54 KB

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