ra386att.pas 53 KB

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