ra386int.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. Does the parsing process for the intel 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. {$E+,N+}
  20. {$endif}
  21. Unit Ra386int;
  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,scanner,symconst,symtable,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_COMMA,AS_LBRACKET,AS_RBRACKET,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,
  43. {------------------ Assembler directives --------------------}
  44. AS_DB,AS_DW,AS_DD,AS_END,
  45. {------------------ Assembler Operators --------------------}
  46. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  47. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  48. AS_AND,AS_OR,AS_XOR);
  49. tasmkeyword = string[6];
  50. const
  51. { These tokens should be modified accordingly to the modifications }
  52. { in the different enumerations. }
  53. firstdirective = AS_DB;
  54. lastdirective = AS_END;
  55. firstoperator = AS_BYTE;
  56. lastoperator = AS_XOR;
  57. firstsreg = R_CS;
  58. lastsreg = R_SS;
  59. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  60. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  61. _count_asmprefixes = 5;
  62. _count_asmspecialops = 25;
  63. _count_asmoverrides = 3;
  64. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  65. ('DB','DW','DD','END');
  66. { problems with shl,shr,not,and,or and xor, they are }
  67. { context sensitive. }
  68. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  69. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  70. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  71. 'OR','XOR');
  72. token2str : array[tasmtoken] of string[10] = (
  73. '','Label','LLabel','String','Integer',
  74. ',','[',']','(',
  75. ')',':','.','+','-','*',
  76. ';','identifier','register','opcode','/',
  77. '','','','END',
  78. '','','','','','','','',
  79. '','','','type','ptr','mod','shl','shr','not',
  80. 'and','or','xor'
  81. );
  82. const
  83. newline = #10;
  84. firsttoken : boolean = TRUE;
  85. var
  86. _asmsorted : boolean;
  87. inexpression : boolean;
  88. curlist : paasmoutput;
  89. c : char;
  90. prevasmtoken : tasmtoken;
  91. actasmtoken : tasmtoken;
  92. actasmpattern : string;
  93. actasmregister : tregister;
  94. actopcode : tasmop;
  95. actopsize : topsize;
  96. actcondition : tasmcond;
  97. iasmops : Pdictionary;
  98. iasmregs : ^reg2strtable;
  99. Procedure SetupTables;
  100. { creates uppercased symbol tables for speed access }
  101. var
  102. i : tasmop;
  103. j : tregister;
  104. str2opentry: pstr2opentry;
  105. Begin
  106. { opcodes }
  107. new(iasmops,init);
  108. for i:=firstop to lastop do
  109. begin
  110. new(str2opentry,initname(upper(int_op2str[i])));
  111. str2opentry^.op:=i;
  112. iasmops^.insert(str2opentry);
  113. end;
  114. { registers }
  115. new(iasmregs);
  116. for j:=firstreg to lastreg do
  117. iasmregs^[j] := upper(int_reg2str[j]);
  118. end;
  119. {---------------------------------------------------------------------}
  120. { Routines for the tokenizing }
  121. {---------------------------------------------------------------------}
  122. function is_asmopcode(const s: string):boolean;
  123. var
  124. str2opentry: pstr2opentry;
  125. cond : string[4];
  126. cnd : tasmcond;
  127. j: longint;
  128. Begin
  129. is_asmopcode:=FALSE;
  130. actopcode:=A_None;
  131. actcondition:=C_None;
  132. actopsize:=S_NO;
  133. str2opentry:=pstr2opentry(iasmops^.search(s));
  134. if assigned(str2opentry) then
  135. begin
  136. actopcode:=str2opentry^.op;
  137. actasmtoken:=AS_OPCODE;
  138. is_asmopcode:=TRUE;
  139. exit;
  140. end;
  141. { not found yet, 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,255);
  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. actcondition:=cnd;
  155. is_asmopcode:=TRUE;
  156. actasmtoken:=AS_OPCODE;
  157. exit
  158. end;
  159. end;
  160. end;
  161. inc(j);
  162. end;
  163. end;
  164. function is_asmoperator(const s: string):boolean;
  165. var
  166. i : longint;
  167. Begin
  168. for i:=0 to _count_asmoperators do
  169. if s=_asmoperators[i] then
  170. begin
  171. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  172. is_asmoperator:=true;
  173. exit;
  174. end;
  175. is_asmoperator:=false;
  176. end;
  177. Function is_asmdirective(const s: string):boolean;
  178. var
  179. i : longint;
  180. Begin
  181. for i:=0 to _count_asmdirectives do
  182. if s=_asmdirectives[i] then
  183. begin
  184. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  185. is_asmdirective:=true;
  186. exit;
  187. end;
  188. is_asmdirective:=false;
  189. end;
  190. Function is_register(const s: string):boolean;
  191. Var
  192. i : tregister;
  193. Begin
  194. actasmregister:=R_NO;
  195. for i:=firstreg to lastreg do
  196. if s=iasmregs^[i] then
  197. begin
  198. actasmtoken:=AS_REGISTER;
  199. actasmregister:=i;
  200. is_register:=true;
  201. exit;
  202. end;
  203. is_register:=false;
  204. end;
  205. function is_locallabel(const s:string):boolean;
  206. begin
  207. is_locallabel:=(length(s)>1) and (s[1]='@');
  208. end;
  209. Procedure GetToken;
  210. var
  211. len : longint;
  212. forcelabel : boolean;
  213. begin
  214. { save old token and reset new token }
  215. prevasmtoken:=actasmtoken;
  216. actasmtoken:=AS_NONE;
  217. { reset }
  218. forcelabel:=FALSE;
  219. actasmpattern:='';
  220. { while space and tab , continue scan... }
  221. while (c in [' ',#9]) do
  222. c:=current_scanner^.asmgetchar;
  223. { get token pos }
  224. if not (c in [newline,#13,'{',';']) then
  225. current_scanner^.gettokenpos;
  226. { Local Label, Label, Directive, Prefix or Opcode }
  227. if firsttoken and not (c in [newline,#13,'{',';']) then
  228. begin
  229. firsttoken:=FALSE;
  230. len:=0;
  231. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  232. begin
  233. { if there is an at_sign, then this must absolutely be a label }
  234. if c = '@' then
  235. forcelabel:=TRUE;
  236. inc(len);
  237. actasmpattern[len]:=c;
  238. c:=current_scanner^.asmgetchar;
  239. end;
  240. actasmpattern[0]:=chr(len);
  241. uppervar(actasmpattern);
  242. { label ? }
  243. if c = ':' then
  244. begin
  245. if actasmpattern[1]='@' then
  246. actasmtoken:=AS_LLABEL
  247. else
  248. actasmtoken:=AS_LABEL;
  249. { let us point to the next character }
  250. c:=current_scanner^.asmgetchar;
  251. firsttoken:=true;
  252. exit;
  253. end;
  254. { Are we trying to create an identifier with }
  255. { an at-sign...? }
  256. if forcelabel then
  257. Message(asmr_e_none_label_contain_at);
  258. { opcode ? }
  259. If is_asmopcode(actasmpattern) then
  260. Begin
  261. { check if we are in an expression }
  262. { then continue with asm directives }
  263. if not inexpression then
  264. exit;
  265. end;
  266. if is_asmdirective(actasmpattern) then
  267. exit;
  268. message1(asmr_e_unknown_opcode,actasmpattern);
  269. actasmtoken:=AS_NONE;
  270. exit;
  271. end
  272. else { else firsttoken }
  273. begin
  274. case c of
  275. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  276. { - @Result, @Code or @Data special variables. }
  277. begin
  278. actasmpattern:=c;
  279. c:=current_scanner^.asmgetchar;
  280. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  281. begin
  282. actasmpattern:=actasmpattern + c;
  283. c:=current_scanner^.asmgetchar;
  284. end;
  285. uppervar(actasmpattern);
  286. actasmtoken:=AS_ID;
  287. exit;
  288. end;
  289. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  290. begin
  291. actasmpattern:=c;
  292. c:=current_scanner^.asmgetchar;
  293. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  294. begin
  295. actasmpattern:=actasmpattern + c;
  296. c:=current_scanner^.asmgetchar;
  297. end;
  298. uppervar(actasmpattern);
  299. { after prefix we allow also a new opcode }
  300. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  301. Begin
  302. { if we are not in a constant }
  303. { expression than this is an }
  304. { opcode. }
  305. if not inexpression then
  306. exit;
  307. end;
  308. { support st(X) for fpu registers }
  309. if (actasmpattern = 'ST') and (c='(') then
  310. Begin
  311. actasmpattern:=actasmpattern+c;
  312. c:=current_scanner^.asmgetchar;
  313. if c in ['0'..'7'] then
  314. actasmpattern:=actasmpattern + c
  315. else
  316. Message(asmr_e_invalid_fpu_register);
  317. c:=current_scanner^.asmgetchar;
  318. if c <> ')' then
  319. Message(asmr_e_invalid_fpu_register)
  320. else
  321. Begin
  322. actasmpattern:=actasmpattern + c;
  323. c:=current_scanner^.asmgetchar;
  324. end;
  325. end;
  326. if is_register(actasmpattern) then
  327. exit;
  328. if is_asmdirective(actasmpattern) then
  329. exit;
  330. if is_asmoperator(actasmpattern) then
  331. exit;
  332. actasmtoken:=AS_ID;
  333. exit;
  334. end;
  335. '&' : { override operator... not supported }
  336. begin
  337. Message(asmr_w_override_op_not_supported);
  338. c:=current_scanner^.asmgetchar;
  339. actasmtoken:=AS_NONE;
  340. end;
  341. '''' : { string or character }
  342. begin
  343. actasmpattern:='';
  344. repeat
  345. if c = '''' then
  346. begin
  347. c:=current_scanner^.asmgetchar;
  348. if c=newline then
  349. begin
  350. Message(scan_f_string_exceeds_line);
  351. break;
  352. end;
  353. repeat
  354. if c='''' then
  355. begin
  356. c:=current_scanner^.asmgetchar;
  357. if c='''' then
  358. begin
  359. actasmpattern:=actasmpattern+'''';
  360. c:=current_scanner^.asmgetchar;
  361. if c=newline then
  362. begin
  363. Message(scan_f_string_exceeds_line);
  364. break;
  365. end;
  366. end
  367. else
  368. break;
  369. end
  370. else
  371. begin
  372. actasmpattern:=actasmpattern+c;
  373. c:=current_scanner^.asmgetchar;
  374. if c=newline then
  375. begin
  376. Message(scan_f_string_exceeds_line);
  377. break
  378. end;
  379. end;
  380. until false; { end repeat }
  381. end
  382. else
  383. break; { end if }
  384. until false;
  385. actasmtoken:=AS_STRING;
  386. exit;
  387. end;
  388. '"' : { string or character }
  389. begin
  390. actasmpattern:='';
  391. repeat
  392. if c = '"' then
  393. begin
  394. c:=current_scanner^.asmgetchar;
  395. if c=newline then
  396. begin
  397. Message(scan_f_string_exceeds_line);
  398. break;
  399. end;
  400. repeat
  401. if c='"' then
  402. begin
  403. c:=current_scanner^.asmgetchar;
  404. if c='"' then
  405. begin
  406. actasmpattern:=actasmpattern+'"';
  407. c:=current_scanner^.asmgetchar;
  408. if c=newline then
  409. begin
  410. Message(scan_f_string_exceeds_line);
  411. break;
  412. end;
  413. end
  414. else
  415. break;
  416. end
  417. else
  418. begin
  419. actasmpattern:=actasmpattern+c;
  420. c:=current_scanner^.asmgetchar;
  421. if c=newline then
  422. begin
  423. Message(scan_f_string_exceeds_line);
  424. break
  425. end;
  426. end;
  427. until false; { end repeat }
  428. end
  429. else
  430. break; { end if }
  431. until false;
  432. actasmtoken:=AS_STRING;
  433. exit;
  434. end;
  435. '$' :
  436. begin
  437. c:=current_scanner^.asmgetchar;
  438. while c in ['0'..'9','A'..'F','a'..'f'] do
  439. begin
  440. actasmpattern:=actasmpattern + c;
  441. c:=current_scanner^.asmgetchar;
  442. end;
  443. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  444. actasmtoken:=AS_INTNUM;
  445. exit;
  446. end;
  447. ',' :
  448. begin
  449. actasmtoken:=AS_COMMA;
  450. c:=current_scanner^.asmgetchar;
  451. exit;
  452. end;
  453. '[' :
  454. begin
  455. actasmtoken:=AS_LBRACKET;
  456. c:=current_scanner^.asmgetchar;
  457. exit;
  458. end;
  459. ']' :
  460. begin
  461. actasmtoken:=AS_RBRACKET;
  462. c:=current_scanner^.asmgetchar;
  463. exit;
  464. end;
  465. '(' :
  466. begin
  467. actasmtoken:=AS_LPAREN;
  468. c:=current_scanner^.asmgetchar;
  469. exit;
  470. end;
  471. ')' :
  472. begin
  473. actasmtoken:=AS_RPAREN;
  474. c:=current_scanner^.asmgetchar;
  475. exit;
  476. end;
  477. ':' :
  478. begin
  479. actasmtoken:=AS_COLON;
  480. c:=current_scanner^.asmgetchar;
  481. exit;
  482. end;
  483. '.' :
  484. begin
  485. actasmtoken:=AS_DOT;
  486. c:=current_scanner^.asmgetchar;
  487. exit;
  488. end;
  489. '+' :
  490. begin
  491. actasmtoken:=AS_PLUS;
  492. c:=current_scanner^.asmgetchar;
  493. exit;
  494. end;
  495. '-' :
  496. begin
  497. actasmtoken:=AS_MINUS;
  498. c:=current_scanner^.asmgetchar;
  499. exit;
  500. end;
  501. '*' :
  502. begin
  503. actasmtoken:=AS_STAR;
  504. c:=current_scanner^.asmgetchar;
  505. exit;
  506. end;
  507. '/' :
  508. begin
  509. actasmtoken:=AS_SLASH;
  510. c:=current_scanner^.asmgetchar;
  511. exit;
  512. end;
  513. '0'..'9':
  514. begin
  515. actasmpattern:=c;
  516. c:=current_scanner^.asmgetchar;
  517. { Get the possible characters }
  518. while c in ['0'..'9','A'..'F','a'..'f'] do
  519. begin
  520. actasmpattern:=actasmpattern + c;
  521. c:=current_scanner^.asmgetchar;
  522. end;
  523. { Get ending character }
  524. uppervar(actasmpattern);
  525. c:=upcase(c);
  526. { possibly a binary number. }
  527. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  528. Begin
  529. { Delete the last binary specifier }
  530. delete(actasmpattern,length(actasmpattern),1);
  531. actasmpattern:=tostr(ValBinary(actasmpattern));
  532. actasmtoken:=AS_INTNUM;
  533. exit;
  534. end
  535. else
  536. Begin
  537. case c of
  538. 'O' :
  539. Begin
  540. actasmpattern:=tostr(ValOctal(actasmpattern));
  541. actasmtoken:=AS_INTNUM;
  542. c:=current_scanner^.asmgetchar;
  543. exit;
  544. end;
  545. 'H' :
  546. Begin
  547. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  548. actasmtoken:=AS_INTNUM;
  549. c:=current_scanner^.asmgetchar;
  550. exit;
  551. end;
  552. else { must be an integer number }
  553. begin
  554. actasmpattern:=tostr(ValDecimal(actasmpattern));
  555. actasmtoken:=AS_INTNUM;
  556. exit;
  557. end;
  558. end;
  559. end;
  560. end;
  561. ';','{',#13,newline :
  562. begin
  563. c:=current_scanner^.asmgetchar;
  564. firsttoken:=TRUE;
  565. actasmtoken:=AS_SEPARATOR;
  566. exit;
  567. end;
  568. else
  569. current_scanner^.illegal_char(c);
  570. end;
  571. end;
  572. end;
  573. function consume(t : tasmtoken):boolean;
  574. begin
  575. Consume:=true;
  576. if t<>actasmtoken then
  577. begin
  578. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  579. Consume:=false;
  580. end;
  581. repeat
  582. gettoken;
  583. until actasmtoken<>AS_NONE;
  584. end;
  585. procedure RecoverConsume(allowcomma:boolean);
  586. begin
  587. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  588. begin
  589. if allowcomma and (actasmtoken=AS_COMMA) then
  590. break;
  591. Consume(actasmtoken);
  592. end;
  593. end;
  594. {*****************************************************************************
  595. Parsing Helpers
  596. *****************************************************************************}
  597. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  598. { Description: This routine builds up a record offset after a AS_DOT }
  599. { token is encountered. }
  600. { On entry actasmtoken should be equal to AS_DOT }
  601. var
  602. s : string;
  603. Begin
  604. offset:=0;
  605. size:=0;
  606. s:=expr;
  607. while (actasmtoken=AS_DOT) do
  608. begin
  609. Consume(AS_DOT);
  610. if actasmtoken=AS_ID then
  611. s:=s+'.'+actasmpattern;
  612. if not Consume(AS_ID) then
  613. begin
  614. RecoverConsume(true);
  615. break;
  616. end;
  617. end;
  618. if not GetRecordOffsetSize(s,offset,size) then
  619. Message(asmr_e_building_record_offset);
  620. end;
  621. Procedure BuildConstSymbolExpression(needofs,exitreg:boolean;var value:longint;var asmsym:string);
  622. var
  623. tempstr,expr,hs : string;
  624. parenlevel,l,k : longint;
  625. errorflag : boolean;
  626. prevtok : tasmtoken;
  627. hl : PAsmLabel;
  628. sym : psym;
  629. Begin
  630. { reset }
  631. value:=0;
  632. asmsym:='';
  633. errorflag:=FALSE;
  634. tempstr:='';
  635. expr:='';
  636. inexpression:=TRUE;
  637. parenlevel:=0;
  638. Repeat
  639. Case actasmtoken of
  640. AS_LPAREN:
  641. Begin
  642. Consume(AS_LPAREN);
  643. expr:=expr + '(';
  644. inc(parenlevel);
  645. end;
  646. AS_RPAREN:
  647. Begin
  648. Consume(AS_RPAREN);
  649. expr:=expr + ')';
  650. dec(parenlevel);
  651. end;
  652. AS_SHL:
  653. Begin
  654. Consume(AS_SHL);
  655. expr:=expr + '<';
  656. end;
  657. AS_SHR:
  658. Begin
  659. Consume(AS_SHR);
  660. expr:=expr + '>';
  661. end;
  662. AS_SLASH:
  663. Begin
  664. Consume(AS_SLASH);
  665. expr:=expr + '/';
  666. end;
  667. AS_MOD:
  668. Begin
  669. Consume(AS_MOD);
  670. expr:=expr + '%';
  671. end;
  672. AS_STAR:
  673. Begin
  674. Consume(AS_STAR);
  675. if exitreg and (actasmtoken=AS_REGISTER) then
  676. break;
  677. expr:=expr + '*';
  678. end;
  679. AS_PLUS:
  680. Begin
  681. Consume(AS_PLUS);
  682. if exitreg and (actasmtoken=AS_REGISTER) then
  683. break;
  684. expr:=expr + '+';
  685. end;
  686. AS_MINUS:
  687. Begin
  688. Consume(AS_MINUS);
  689. expr:=expr + '-';
  690. end;
  691. AS_AND:
  692. Begin
  693. Consume(AS_AND);
  694. expr:=expr + '&';
  695. end;
  696. AS_NOT:
  697. Begin
  698. Consume(AS_NOT);
  699. expr:=expr + '~';
  700. end;
  701. AS_XOR:
  702. Begin
  703. Consume(AS_XOR);
  704. expr:=expr + '^';
  705. end;
  706. AS_OR:
  707. Begin
  708. Consume(AS_OR);
  709. expr:=expr + '|';
  710. end;
  711. AS_INTNUM:
  712. Begin
  713. expr:=expr + actasmpattern;
  714. Consume(AS_INTNUM);
  715. end;
  716. AS_OFFSET:
  717. begin
  718. Consume(AS_OFFSET);
  719. if actasmtoken<>AS_ID then
  720. Message(asmr_e_offset_without_identifier);
  721. end;
  722. AS_TYPE:
  723. begin
  724. l:=0;
  725. Consume(AS_TYPE);
  726. if actasmtoken<>AS_ID then
  727. Message(asmr_e_type_without_identifier)
  728. else
  729. begin
  730. tempstr:=actasmpattern;
  731. Consume(AS_ID);
  732. if actasmtoken=AS_DOT then
  733. BuildRecordOffsetSize(tempstr,k,l)
  734. else
  735. begin
  736. getsym(tempstr,false);
  737. if assigned(srsym) then
  738. begin
  739. case srsym^.typ of
  740. varsym :
  741. l:=pvarsym(srsym)^.getsize;
  742. typedconstsym :
  743. l:=ptypedconstsym(srsym)^.getsize;
  744. typesym :
  745. l:=ptypesym(srsym)^.restype.def^.size;
  746. else
  747. Message(asmr_e_wrong_sym_type);
  748. end;
  749. end
  750. else
  751. Message1(sym_e_unknown_id,tempstr);
  752. end;
  753. end;
  754. str(l, tempstr);
  755. expr:=expr + tempstr;
  756. end;
  757. AS_STRING:
  758. Begin
  759. l:=0;
  760. case Length(actasmpattern) of
  761. 1 :
  762. l:=ord(actasmpattern[1]);
  763. 2 :
  764. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  765. 3 :
  766. l:=ord(actasmpattern[3]) +
  767. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  768. 4 :
  769. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  770. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  771. else
  772. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  773. end;
  774. str(l, tempstr);
  775. expr:=expr + tempstr;
  776. Consume(AS_STRING);
  777. end;
  778. AS_ID:
  779. Begin
  780. hs:='';
  781. tempstr:=actasmpattern;
  782. prevtok:=prevasmtoken;
  783. consume(AS_ID);
  784. if SearchIConstant(tempstr,l) then
  785. begin
  786. str(l, tempstr);
  787. expr:=expr + tempstr;
  788. end
  789. else
  790. begin
  791. if is_locallabel(tempstr) then
  792. begin
  793. CreateLocalLabel(tempstr,hl,false);
  794. hs:=hl^.name
  795. end
  796. else
  797. if SearchLabel(tempstr,hl,false) then
  798. hs:=hl^.name
  799. else
  800. begin
  801. getsym(tempstr,false);
  802. sym:=srsym;
  803. if assigned(sym) then
  804. begin
  805. case srsym^.typ of
  806. varsym :
  807. begin
  808. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  809. Message(asmr_e_no_local_or_para_allowed);
  810. hs:=pvarsym(srsym)^.mangledname;
  811. end;
  812. typedconstsym :
  813. hs:=ptypedconstsym(srsym)^.mangledname;
  814. procsym :
  815. hs:=pprocsym(srsym)^.mangledname;
  816. else
  817. Message(asmr_e_wrong_sym_type);
  818. end;
  819. end
  820. else
  821. Message1(sym_e_unknown_id,tempstr);
  822. end;
  823. { symbol found? }
  824. if hs<>'' then
  825. begin
  826. if needofs and (prevtok<>AS_OFFSET) then
  827. Message(asmr_e_need_offset);
  828. if asmsym='' then
  829. asmsym:=hs
  830. else
  831. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  832. if (expr='') or (expr[length(expr)]='+') then
  833. begin
  834. { don't remove the + if there could be a record field }
  835. if actasmtoken<>AS_DOT then
  836. delete(expr,length(expr),1);
  837. end
  838. else
  839. Message(asmr_e_only_add_relocatable_symbol);
  840. end;
  841. if actasmtoken=AS_DOT then
  842. begin
  843. BuildRecordOffsetSize(tempstr,l,k);
  844. str(l, tempstr);
  845. expr:=expr + tempstr;
  846. end
  847. else
  848. begin
  849. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  850. delete(expr,length(expr),1);
  851. end;
  852. end;
  853. { check if there are wrong operator used like / or mod etc. }
  854. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  855. Message(asmr_e_only_add_relocatable_symbol);
  856. end;
  857. AS_END,
  858. AS_RBRACKET,
  859. AS_SEPARATOR,
  860. AS_COMMA:
  861. Begin
  862. break;
  863. end;
  864. else
  865. Begin
  866. { write error only once. }
  867. if not errorflag then
  868. Message(asmr_e_invalid_constant_expression);
  869. { consume tokens until we find COMMA or SEPARATOR }
  870. Consume(actasmtoken);
  871. errorflag:=TRUE;
  872. end;
  873. end;
  874. Until false;
  875. { calculate expression }
  876. if not ErrorFlag then
  877. value:=CalculateExpression(expr)
  878. else
  879. value:=0;
  880. { no longer in an expression }
  881. inexpression:=FALSE;
  882. end;
  883. Function BuildConstExpression:longint;
  884. var
  885. l : longint;
  886. hs : string;
  887. begin
  888. BuildConstSymbolExpression(false,false,l,hs);
  889. if hs<>'' then
  890. Message(asmr_e_relocatable_symbol_not_allowed);
  891. BuildConstExpression:=l;
  892. end;
  893. Function BuildRefConstExpression:longint;
  894. var
  895. l : longint;
  896. hs : string;
  897. begin
  898. BuildConstSymbolExpression(false,true,l,hs);
  899. if hs<>'' then
  900. Message(asmr_e_relocatable_symbol_not_allowed);
  901. BuildRefConstExpression:=l;
  902. end;
  903. {****************************************************************************
  904. T386IntelOperand
  905. ****************************************************************************}
  906. type
  907. P386IntelOperand=^T386IntelOperand;
  908. T386IntelOperand=object(T386Operand)
  909. Procedure BuildOperand;virtual;
  910. private
  911. Procedure BuildReference;
  912. Procedure BuildConstant;
  913. end;
  914. Procedure T386IntelOperand.BuildReference;
  915. var
  916. k,l : longint;
  917. tempstr2,
  918. tempstr,hs : string;
  919. code : integer;
  920. hreg,
  921. oldbase : tregister;
  922. GotStar,GotOffset,HadVar,
  923. GotPlus,Negative : boolean;
  924. Begin
  925. Consume(AS_LBRACKET);
  926. InitRef;
  927. GotStar:=false;
  928. GotPlus:=true;
  929. GotOffset:=false;
  930. Negative:=false;
  931. repeat
  932. if GotOffset and (actasmtoken<>AS_ID) then
  933. Message(asmr_e_invalid_reference_syntax);
  934. Case actasmtoken of
  935. AS_ID: { Constant reference expression OR variable reference expression }
  936. Begin
  937. if not GotPlus then
  938. Message(asmr_e_invalid_reference_syntax);
  939. if actasmpattern[1] = '@' then
  940. Message(asmr_e_local_label_not_allowed_as_ref);
  941. GotStar:=false;
  942. GotPlus:=false;
  943. if SearchIConstant(actasmpattern,l) or
  944. SearchRecordType(actasmpattern) then
  945. begin
  946. l:=BuildRefConstExpression;
  947. GotPlus:=(prevasmtoken=AS_PLUS);
  948. GotStar:=(prevasmtoken=AS_STAR);
  949. if GotStar then
  950. opr.ref.scalefactor:=l
  951. else
  952. begin
  953. if negative then
  954. Dec(opr.ref.offset,l)
  955. else
  956. Inc(opr.ref.offset,l);
  957. end;
  958. end
  959. else
  960. Begin
  961. if hasvar and not GotOffset then
  962. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  963. HadVar:=hasvar and GotOffset;
  964. if negative then
  965. Message(asmr_e_only_add_relocatable_symbol);
  966. oldbase:=opr.ref.base;
  967. opr.ref.base:=R_NO;
  968. tempstr:=actasmpattern;
  969. Consume(AS_ID);
  970. { typecasting? }
  971. if (actasmtoken=AS_LPAREN) and
  972. SearchType(tempstr) then
  973. begin
  974. hastype:=true;
  975. Consume(AS_LPAREN);
  976. tempstr2:=actasmpattern;
  977. Consume(AS_ID);
  978. Consume(AS_RPAREN);
  979. if not SetupVar(tempstr2,GotOffset) then
  980. Message1(sym_e_unknown_id,tempstr2);
  981. end
  982. else
  983. if not SetupVar(tempstr,GotOffset) then
  984. Message1(sym_e_unknown_id,tempstr);
  985. { record.field ? }
  986. if actasmtoken=AS_DOT then
  987. begin
  988. BuildRecordOffsetSize(tempstr,l,k);
  989. inc(opr.ref.offset,l);
  990. end;
  991. if GotOffset then
  992. begin
  993. if hasvar and (opr.ref.base=procinfo^.framepointer) then
  994. begin
  995. opr.ref.base:=R_NO;
  996. hasvar:=hadvar;
  997. end
  998. else
  999. begin
  1000. if hasvar and hadvar then
  1001. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1002. { should we allow ?? }
  1003. end;
  1004. end;
  1005. { is the base register loaded by the var ? }
  1006. if (opr.ref.base<>R_NO) then
  1007. begin
  1008. { check if we can move the old base to the index register }
  1009. if (opr.ref.index<>R_NO) then
  1010. Message(asmr_e_wrong_base_index)
  1011. else if assigned(procinfo^._class) and
  1012. (oldbase=self_pointer) and
  1013. (opr.ref.base=self_pointer) then
  1014. begin
  1015. Message(asmr_w_possible_object_field_bug);
  1016. { warn but accept... who knows what people
  1017. caninvent in assembler ! }
  1018. opr.ref.index:=oldbase;
  1019. end
  1020. else
  1021. opr.ref.index:=oldbase;
  1022. end
  1023. else
  1024. opr.ref.base:=oldbase;
  1025. { we can't have a Constant here so add the constant value to the
  1026. offset }
  1027. if opr.typ=OPR_CONSTANT then
  1028. begin
  1029. opr.typ:=OPR_REFERENCE;
  1030. inc(opr.ref.offset,opr.val);
  1031. end;
  1032. end;
  1033. GotOffset:=false;
  1034. end;
  1035. AS_PLUS :
  1036. Begin
  1037. Consume(AS_PLUS);
  1038. Negative:=false;
  1039. GotPlus:=true;
  1040. GotStar:=false;
  1041. end;
  1042. AS_MINUS :
  1043. begin
  1044. Consume(AS_MINUS);
  1045. Negative:=true;
  1046. GotPlus:=true;
  1047. GotStar:=false;
  1048. end;
  1049. AS_STAR : { Scaling, with eax*4 order }
  1050. begin
  1051. Consume(AS_STAR);
  1052. hs:='';
  1053. l:=0;
  1054. case actasmtoken of
  1055. AS_LPAREN :
  1056. l:=BuildConstExpression;
  1057. AS_INTNUM:
  1058. Begin
  1059. hs:=actasmpattern;
  1060. Consume(AS_INTNUM);
  1061. end;
  1062. AS_REGISTER :
  1063. begin
  1064. if opr.ref.scalefactor=0 then
  1065. Message(asmr_e_wrong_scale_factor);
  1066. end;
  1067. else
  1068. Message(asmr_e_invalid_reference_syntax);
  1069. end;
  1070. if actasmtoken<>AS_REGISTER then
  1071. begin
  1072. if hs<>'' then
  1073. val(hs,l,code);
  1074. opr.ref.scalefactor:=l;
  1075. if l>8 then
  1076. Message(asmr_e_wrong_scale_factor);
  1077. end;
  1078. GotPlus:=false;
  1079. GotStar:=false;
  1080. end;
  1081. AS_REGISTER :
  1082. begin
  1083. if (not GotPlus) and (not GotStar) then
  1084. Message(asmr_e_invalid_reference_syntax);
  1085. hreg:=actasmregister;
  1086. Consume(AS_REGISTER);
  1087. { this register will be the index:
  1088. 1. just read a *
  1089. 2. next token is a *
  1090. 3. base register is already used }
  1091. if (GotStar) or
  1092. (actasmtoken=AS_STAR) or
  1093. (opr.ref.base<>R_NO) then
  1094. begin
  1095. if (opr.ref.index<>R_NO) then
  1096. Message(asmr_e_multiple_index);
  1097. opr.ref.index:=hreg;
  1098. end
  1099. else
  1100. opr.ref.base:=hreg;
  1101. GotPlus:=false;
  1102. GotStar:=false;
  1103. end;
  1104. AS_OFFSET :
  1105. begin
  1106. Consume(AS_OFFSET);
  1107. GotOffset:=true;
  1108. end;
  1109. AS_TYPE,
  1110. AS_NOT,
  1111. AS_INTNUM,
  1112. AS_LPAREN : { Constant reference expression }
  1113. begin
  1114. if not GotPlus then
  1115. Message(asmr_e_invalid_reference_syntax);
  1116. BuildConstSymbolExpression(true,true,l,tempstr);
  1117. if tempstr<>'' then
  1118. begin
  1119. if GotStar then
  1120. Message(asmr_e_only_add_relocatable_symbol);
  1121. if not assigned(opr.ref.symbol) then
  1122. opr.ref.symbol:=newasmsymbol(tempstr)
  1123. else
  1124. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1125. end;
  1126. if GotStar then
  1127. opr.ref.scalefactor:=l
  1128. else
  1129. begin
  1130. if negative then
  1131. Dec(opr.ref.offset,l)
  1132. else
  1133. Inc(opr.ref.offset,l);
  1134. end;
  1135. GotPlus:=(prevasmtoken=AS_PLUS);
  1136. GotStar:=(prevasmtoken=AS_STAR);
  1137. end;
  1138. AS_RBRACKET :
  1139. begin
  1140. if GotPlus then
  1141. Message(asmr_e_invalid_reference_syntax);
  1142. Consume(AS_RBRACKET);
  1143. break;
  1144. end;
  1145. else
  1146. Begin
  1147. Message(asmr_e_invalid_reference_syntax);
  1148. RecoverConsume(true);
  1149. break;
  1150. end;
  1151. end;
  1152. until false;
  1153. end;
  1154. Procedure T386IntelOperand.BuildConstant;
  1155. var
  1156. l : longint;
  1157. tempstr : string;
  1158. begin
  1159. BuildConstSymbolExpression(true,false,l,tempstr);
  1160. if tempstr<>'' then
  1161. begin
  1162. opr.typ:=OPR_SYMBOL;
  1163. opr.symofs:=l;
  1164. opr.symbol:=newasmsymbol(tempstr);
  1165. end
  1166. else
  1167. begin
  1168. opr.typ:=OPR_CONSTANT;
  1169. opr.val:=l;
  1170. end;
  1171. end;
  1172. Procedure T386IntelOperand.BuildOperand;
  1173. var
  1174. tempstr,
  1175. expr : string;
  1176. tempreg : tregister;
  1177. l : longint;
  1178. hl : PAsmLabel;
  1179. procedure AddLabelOperand(hl:pasmlabel);
  1180. begin
  1181. if is_calljmp(actopcode) then
  1182. begin
  1183. opr.typ:=OPR_SYMBOL;
  1184. opr.symbol:=hl;
  1185. end
  1186. else
  1187. begin
  1188. InitRef;
  1189. opr.ref.symbol:=hl;
  1190. end;
  1191. end;
  1192. procedure MaybeRecordOffset;
  1193. var
  1194. l,
  1195. toffset,
  1196. tsize : longint;
  1197. begin
  1198. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1199. exit;
  1200. l:=0;
  1201. if actasmtoken=AS_DOT then
  1202. begin
  1203. { if no type was specified before the [] then we expect the
  1204. first ID to be the type }
  1205. if expr='' then
  1206. begin
  1207. consume(AS_DOT);
  1208. if actasmtoken=AS_ID then
  1209. begin
  1210. expr:=actasmpattern;
  1211. consume(AS_ID);
  1212. { now the next one must the be the dot }
  1213. if actasmtoken<>AS_DOT then
  1214. begin
  1215. Message(asmr_e_building_record_offset);
  1216. expr:='';
  1217. end;
  1218. end
  1219. else
  1220. Message(asmr_e_no_var_type_specified)
  1221. end;
  1222. if expr<>'' then
  1223. begin
  1224. BuildRecordOffsetSize(expr,toffset,tsize);
  1225. inc(l,toffset);
  1226. SetSize(tsize,true);
  1227. end;
  1228. end;
  1229. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1230. inc(l,BuildConstExpression);
  1231. if (opr.typ=OPR_REFERENCE) then
  1232. begin
  1233. { don't allow direct access to fields of parameters, becuase that
  1234. will generate buggy code. Allow it only for explicit typecasting }
  1235. if (not hastype) then
  1236. begin
  1237. case opr.ref.options of
  1238. ref_parafixup :
  1239. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1240. ref_selffixup :
  1241. Message(asmr_e_cannot_access_object_field_directly);
  1242. end;
  1243. end;
  1244. inc(opr.ref.offset,l)
  1245. end
  1246. else
  1247. inc(opr.val,l);
  1248. end;
  1249. Begin
  1250. expr:='';
  1251. case actasmtoken of
  1252. AS_OFFSET,
  1253. AS_TYPE,
  1254. AS_INTNUM,
  1255. AS_PLUS,
  1256. AS_MINUS,
  1257. AS_NOT,
  1258. AS_LPAREN,
  1259. AS_STRING :
  1260. Begin
  1261. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1262. Message(asmr_e_invalid_operand_type);
  1263. BuildConstant;
  1264. end;
  1265. AS_ID : { A constant expression, or a Variable ref. }
  1266. Begin
  1267. { Label or Special symbol reference? }
  1268. if actasmpattern[1] = '@' then
  1269. Begin
  1270. if actasmpattern = '@RESULT' then
  1271. Begin
  1272. InitRef;
  1273. SetupResult;
  1274. Consume(AS_ID);
  1275. end
  1276. else
  1277. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1278. begin
  1279. Message(asmr_w_CODE_and_DATA_not_supported);
  1280. Consume(AS_ID);
  1281. end
  1282. else
  1283. { Local Label }
  1284. begin
  1285. CreateLocalLabel(actasmpattern,hl,false);
  1286. Consume(AS_ID);
  1287. AddLabelOperand(hl);
  1288. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1289. Message(asmr_e_syntax_error);
  1290. end;
  1291. end
  1292. else
  1293. { support result for delphi modes }
  1294. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1295. begin
  1296. InitRef;
  1297. SetUpResult;
  1298. Consume(AS_ID);
  1299. end
  1300. { probably a variable or normal expression }
  1301. { or a procedure (such as in CALL ID) }
  1302. else
  1303. Begin
  1304. { is it a constant ? }
  1305. if SearchIConstant(actasmpattern,l) then
  1306. Begin
  1307. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1308. Message(asmr_e_invalid_operand_type);
  1309. BuildConstant;
  1310. end
  1311. else
  1312. { Check for pascal label }
  1313. if SearchLabel(actasmpattern,hl,false) then
  1314. begin
  1315. Consume(AS_ID);
  1316. AddLabelOperand(hl);
  1317. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1318. Message(asmr_e_syntax_error);
  1319. end
  1320. else
  1321. { is it a normal variable ? }
  1322. Begin
  1323. InitRef;
  1324. expr:=actasmpattern;
  1325. Consume(AS_ID);
  1326. { typecasting? }
  1327. if (actasmtoken=AS_LPAREN) and
  1328. SearchType(expr) then
  1329. begin
  1330. hastype:=true;
  1331. Consume(AS_LPAREN);
  1332. tempstr:=actasmpattern;
  1333. Consume(AS_ID);
  1334. Consume(AS_RPAREN);
  1335. if SetupVar(tempstr,false) then
  1336. begin
  1337. MaybeRecordOffset;
  1338. { add a constant expression? }
  1339. if (actasmtoken=AS_PLUS) then
  1340. begin
  1341. l:=BuildConstExpression;
  1342. if opr.typ=OPR_CONSTANT then
  1343. inc(opr.val,l)
  1344. else
  1345. inc(opr.ref.offset,l);
  1346. end
  1347. end
  1348. else
  1349. Message1(sym_e_unknown_id,tempstr);
  1350. end
  1351. else
  1352. begin
  1353. if SetupVar(expr,false) then
  1354. begin
  1355. MaybeRecordOffset;
  1356. { add a constant expression? }
  1357. if (actasmtoken=AS_PLUS) then
  1358. begin
  1359. l:=BuildConstExpression;
  1360. if opr.typ=OPR_CONSTANT then
  1361. inc(opr.val,l)
  1362. else
  1363. inc(opr.ref.offset,l);
  1364. end
  1365. end
  1366. else
  1367. Begin
  1368. { not a variable, check special variables.. }
  1369. if expr = 'SELF' then
  1370. SetupSelf
  1371. else
  1372. Message1(sym_e_unknown_id,expr);
  1373. end;
  1374. end;
  1375. end;
  1376. { handle references }
  1377. if actasmtoken=AS_LBRACKET then
  1378. begin
  1379. if opr.typ=OPR_CONSTANT then
  1380. begin
  1381. l:=opr.val;
  1382. opr.typ:=OPR_REFERENCE;
  1383. reset_reference(opr.Ref);
  1384. opr.Ref.Offset:=l;
  1385. end;
  1386. BuildReference;
  1387. MaybeRecordOffset;
  1388. end;
  1389. end;
  1390. end;
  1391. AS_REGISTER : { Register, a variable reference or a constant reference }
  1392. Begin
  1393. { save the type of register used. }
  1394. tempreg:=actasmregister;
  1395. Consume(AS_REGISTER);
  1396. if actasmtoken = AS_COLON then
  1397. Begin
  1398. Consume(AS_COLON);
  1399. InitRef;
  1400. opr.ref.segment:=tempreg;
  1401. BuildReference;
  1402. end
  1403. else
  1404. { Simple register }
  1405. begin
  1406. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1407. Message(asmr_e_invalid_operand_type);
  1408. opr.typ:=OPR_REGISTER;
  1409. opr.reg:=tempreg;
  1410. size:=reg_2_opsize[opr.reg];
  1411. end;
  1412. end;
  1413. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1414. Begin
  1415. InitRef;
  1416. BuildReference;
  1417. MaybeRecordOffset;
  1418. end;
  1419. AS_SEG :
  1420. Begin
  1421. Message(asmr_e_seg_not_supported);
  1422. Consume(actasmtoken);
  1423. end;
  1424. AS_SEPARATOR,
  1425. AS_END,
  1426. AS_COMMA: ;
  1427. else
  1428. Message(asmr_e_syn_operand);
  1429. end;
  1430. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1431. begin
  1432. Message(asmr_e_syntax_error);
  1433. RecoverConsume(true);
  1434. end;
  1435. end;
  1436. {*****************************************************************************
  1437. T386IntelInstruction
  1438. *****************************************************************************}
  1439. type
  1440. P386IntelInstruction=^T386IntelInstruction;
  1441. T386IntelInstruction=object(T386Instruction)
  1442. procedure InitOperands;virtual;
  1443. procedure BuildOpcode;virtual;
  1444. end;
  1445. procedure T386IntelInstruction.InitOperands;
  1446. var
  1447. i : longint;
  1448. begin
  1449. for i:=1 to 3 do
  1450. Operands[i]:=new(P386IntelOperand,Init);
  1451. end;
  1452. Procedure T386IntelInstruction.BuildOpCode;
  1453. var
  1454. PrefixOp,OverrideOp: tasmop;
  1455. size : topsize;
  1456. operandnum : longint;
  1457. Begin
  1458. PrefixOp:=A_None;
  1459. OverrideOp:=A_None;
  1460. { prefix seg opcode / prefix opcode }
  1461. repeat
  1462. if is_prefix(actopcode) then
  1463. begin
  1464. PrefixOp:=ActOpcode;
  1465. opcode:=ActOpcode;
  1466. condition:=ActCondition;
  1467. opsize:=ActOpsize;
  1468. ConcatInstruction(curlist);
  1469. Consume(AS_OPCODE);
  1470. end
  1471. else
  1472. if is_override(actopcode) then
  1473. begin
  1474. OverrideOp:=ActOpcode;
  1475. opcode:=ActOpcode;
  1476. condition:=ActCondition;
  1477. opsize:=ActOpsize;
  1478. ConcatInstruction(curlist);
  1479. Consume(AS_OPCODE);
  1480. end
  1481. else
  1482. break;
  1483. { allow for newline after prefix or override }
  1484. while actasmtoken=AS_SEPARATOR do
  1485. Consume(AS_SEPARATOR);
  1486. until (actasmtoken<>AS_OPCODE);
  1487. { opcode }
  1488. if (actasmtoken <> AS_OPCODE) then
  1489. Begin
  1490. Message(asmr_e_invalid_or_missing_opcode);
  1491. RecoverConsume(false);
  1492. exit;
  1493. end;
  1494. { Fill the instr object with the current state }
  1495. Opcode:=ActOpcode;
  1496. condition:=ActCondition;
  1497. opsize:=ActOpsize;
  1498. { Valid combination of prefix/override and instruction ? }
  1499. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1500. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1501. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1502. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1503. { We are reading operands, so opcode will be an AS_ID }
  1504. operandnum:=1;
  1505. Consume(AS_OPCODE);
  1506. { Zero operand opcode ? }
  1507. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1508. begin
  1509. operandnum:=0;
  1510. exit;
  1511. end;
  1512. { Read Operands }
  1513. repeat
  1514. case actasmtoken of
  1515. { End of asm operands for this opcode }
  1516. AS_END,
  1517. AS_SEPARATOR :
  1518. break;
  1519. { Operand delimiter }
  1520. AS_COMMA :
  1521. Begin
  1522. if operandnum > MaxOperands then
  1523. Message(asmr_e_too_many_operands)
  1524. else
  1525. Inc(operandnum);
  1526. Consume(AS_COMMA);
  1527. end;
  1528. { Typecast, Constant Expression, Type Specifier }
  1529. AS_DWORD,
  1530. AS_BYTE,
  1531. AS_WORD,
  1532. AS_TBYTE,
  1533. AS_QWORD :
  1534. Begin
  1535. { load the size in a temp variable, so it can be set when the
  1536. operand is read }
  1537. Case actasmtoken of
  1538. AS_DWORD : size:=S_L;
  1539. AS_WORD : size:=S_W;
  1540. AS_BYTE : size:=S_B;
  1541. AS_QWORD : begin
  1542. if (opcode=A_FCOM) or
  1543. (opcode=A_FCOMP) or
  1544. (opcode=A_FDIV) or
  1545. (opcode=A_FDIVR) or
  1546. (opcode=A_FMUL) or
  1547. (opcode=A_FSUB) or
  1548. (opcode=A_FSUBR) or
  1549. (opcode=A_FLD) or
  1550. (opcode=A_FST) or
  1551. (opcode=A_FSTP) or
  1552. (opcode=A_FADD) then
  1553. size:=S_FL
  1554. else
  1555. size:=S_IQ;
  1556. end;
  1557. AS_TBYTE : size:=S_FX;
  1558. end;
  1559. Consume(actasmtoken);
  1560. if actasmtoken=AS_PTR then
  1561. begin
  1562. Consume(AS_PTR);
  1563. Operands[operandnum]^.InitRef;
  1564. end;
  1565. Operands[operandnum]^.BuildOperand;
  1566. { now set the size which was specified by the override }
  1567. Operands[operandnum]^.size:=size;
  1568. end;
  1569. { Type specifier }
  1570. AS_NEAR,
  1571. AS_FAR :
  1572. Begin
  1573. if actasmtoken = AS_NEAR then
  1574. Message(asmr_w_near_ignored)
  1575. else
  1576. Message(asmr_w_far_ignored);
  1577. Consume(actasmtoken);
  1578. if actasmtoken=AS_PTR then
  1579. begin
  1580. Consume(AS_PTR);
  1581. Operands[operandnum]^.InitRef;
  1582. end;
  1583. Operands[operandnum]^.BuildOperand;
  1584. end;
  1585. else
  1586. Operands[operandnum]^.BuildOperand;
  1587. end; { end case }
  1588. until false;
  1589. Ops:=operandnum;
  1590. end;
  1591. Procedure BuildConstant(maxvalue: longint);
  1592. var
  1593. strlength: byte;
  1594. asmsym,
  1595. expr: string;
  1596. value : longint;
  1597. Begin
  1598. strlength:=0; { assume it is a DB }
  1599. Repeat
  1600. Case actasmtoken of
  1601. AS_STRING:
  1602. Begin
  1603. if maxvalue = $ffff then
  1604. strlength:=2
  1605. else
  1606. if maxvalue = longint($ffffffff) then
  1607. strlength:=4;
  1608. { DD and DW cases }
  1609. if strlength <> 0 then
  1610. Begin
  1611. if Not PadZero(actasmpattern,strlength) then
  1612. Message(scan_f_string_exceeds_line);
  1613. end;
  1614. expr:=actasmpattern;
  1615. Consume(AS_STRING);
  1616. Case actasmtoken of
  1617. AS_COMMA:
  1618. Consume(AS_COMMA);
  1619. AS_END,
  1620. AS_SEPARATOR: ;
  1621. else
  1622. Message(asmr_e_invalid_string_expression);
  1623. end;
  1624. ConcatString(curlist,expr);
  1625. end;
  1626. AS_PLUS,
  1627. AS_MINUS,
  1628. AS_LPAREN,
  1629. AS_NOT,
  1630. AS_INTNUM,
  1631. AS_ID :
  1632. Begin
  1633. BuildConstSymbolExpression(false,false,value,asmsym);
  1634. if asmsym<>'' then
  1635. begin
  1636. if maxvalue<>longint($ffffffff) then
  1637. Message(asmr_w_const32bit_for_address);
  1638. ConcatConstSymbol(curlist,asmsym,value)
  1639. end
  1640. else
  1641. ConcatConstant(curlist,value,maxvalue);
  1642. end;
  1643. AS_COMMA:
  1644. Consume(AS_COMMA);
  1645. AS_END,
  1646. AS_SEPARATOR:
  1647. break;
  1648. else
  1649. begin
  1650. Message(asmr_e_syn_constant);
  1651. RecoverConsume(false);
  1652. end
  1653. end;
  1654. Until false;
  1655. end;
  1656. Function Assemble: Ptree;
  1657. Var
  1658. hl : PAsmLabel;
  1659. instr : T386IntelInstruction;
  1660. Begin
  1661. Message1(asmr_d_start_reading,'intel');
  1662. inexpression:=FALSE;
  1663. firsttoken:=TRUE;
  1664. if assigned(procinfo^.returntype.def) and
  1665. (is_fpu(procinfo^.returntype.def) or
  1666. ret_in_acc(procinfo^.returntype.def)) then
  1667. procinfo^.funcret_state:=vs_assigned;
  1668. { sets up all opcode and register tables in uppercase }
  1669. if not _asmsorted then
  1670. Begin
  1671. SetupTables;
  1672. _asmsorted:=TRUE;
  1673. end;
  1674. curlist:=new(paasmoutput,init);
  1675. { setup label linked list }
  1676. new(LocalLabelList,Init);
  1677. { start tokenizer }
  1678. c:=current_scanner^.asmgetchar;
  1679. gettoken;
  1680. { main loop }
  1681. repeat
  1682. case actasmtoken of
  1683. AS_LLABEL:
  1684. Begin
  1685. if CreateLocalLabel(actasmpattern,hl,true) then
  1686. ConcatLabel(curlist,hl);
  1687. Consume(AS_LLABEL);
  1688. end;
  1689. AS_LABEL:
  1690. Begin
  1691. if SearchLabel(upper(actasmpattern),hl,true) then
  1692. ConcatLabel(curlist,hl)
  1693. else
  1694. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1695. Consume(AS_LABEL);
  1696. end;
  1697. AS_DW :
  1698. Begin
  1699. inexpression:=true;
  1700. Consume(AS_DW);
  1701. BuildConstant($ffff);
  1702. inexpression:=false;
  1703. end;
  1704. AS_DB :
  1705. Begin
  1706. inexpression:=true;
  1707. Consume(AS_DB);
  1708. BuildConstant($ff);
  1709. inexpression:=false;
  1710. end;
  1711. AS_DD :
  1712. Begin
  1713. inexpression:=true;
  1714. Consume(AS_DD);
  1715. BuildConstant($ffffffff);
  1716. inexpression:=false;
  1717. end;
  1718. AS_OPCODE :
  1719. Begin
  1720. instr.init;
  1721. instr.BuildOpcode;
  1722. { We need AT&T style operands }
  1723. instr.SwapOperands;
  1724. { Must be done with args in ATT order }
  1725. instr.CheckNonCommutativeOpcodes;
  1726. instr.AddReferenceSizes;
  1727. instr.SetInstructionOpsize;
  1728. instr.CheckOperandSizes;
  1729. instr.ConcatInstruction(curlist);
  1730. instr.done;
  1731. end;
  1732. AS_SEPARATOR :
  1733. Begin
  1734. Consume(AS_SEPARATOR);
  1735. end;
  1736. AS_END :
  1737. break; { end assembly block }
  1738. else
  1739. Begin
  1740. Message(asmr_e_syntax_error);
  1741. RecoverConsume(false);
  1742. end;
  1743. end; { end case }
  1744. until false;
  1745. { Check LocalLabelList }
  1746. LocalLabelList^.CheckEmitted;
  1747. dispose(LocalLabelList,Done);
  1748. { Return the list in an asmnode }
  1749. assemble:=genasmnode(curlist);
  1750. Message1(asmr_d_finish_reading,'intel');
  1751. end;
  1752. {*****************************************************************************
  1753. Initialize
  1754. *****************************************************************************}
  1755. var
  1756. old_exit : pointer;
  1757. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1758. begin
  1759. if assigned(iasmops) then
  1760. dispose(iasmops,done);
  1761. if assigned(iasmregs) then
  1762. dispose(iasmregs);
  1763. exitproc:=old_exit;
  1764. end;
  1765. begin
  1766. old_exit:=exitproc;
  1767. exitproc:=@ra386int_exit;
  1768. end.
  1769. {
  1770. $Log$
  1771. Revision 1.75 2000-06-18 18:07:06 peter
  1772. * use new illegal_char method
  1773. Revision 1.74 2000/06/15 18:07:08 peter
  1774. * fix constant parsing which gave an error when constants were used
  1775. Revision 1.73 2000/06/14 19:02:41 peter
  1776. * fixed TYPE with records and fields
  1777. * added TYPE support for ATT reader else it wouldn't be possible to
  1778. get the size of a type/variable
  1779. Revision 1.72 2000/06/14 16:52:09 peter
  1780. * fixed reference parsing
  1781. Revision 1.71 2000/05/23 20:36:28 peter
  1782. + typecasting support for variables, but be carefull as word,byte can't
  1783. be used because they are reserved assembler keywords
  1784. Revision 1.70 2000/05/18 17:05:16 peter
  1785. * fixed size of const parameters in asm readers
  1786. Revision 1.69 2000/05/12 21:57:02 pierre
  1787. + use of a dictionary object
  1788. for faster opcode searching in assembler readers
  1789. implemented by Kovacs Attila Zoltan
  1790. Revision 1.68 2000/05/12 21:26:23 pierre
  1791. * fix the FDIV FDIVR FSUB FSUBR and popping equivalent
  1792. simply by swapping from reverse to normal and vice-versa
  1793. when passing from one syntax to the other !
  1794. Revision 1.67 2000/05/11 09:56:21 pierre
  1795. * fixed several compare problems between longints and
  1796. const > $80000000 that are treated as int64 constanst
  1797. by Delphi reported by Kovacs Attila Zoltan
  1798. Revision 1.66 2000/05/09 11:56:26 pierre
  1799. * Issue an error if opcode is not found
  1800. Revision 1.65 2000/05/08 13:23:04 peter
  1801. * fixed reference parsing
  1802. Revision 1.64 2000/04/29 12:51:34 peter
  1803. * fixed offset support intel reader, the gotoffset variable was not
  1804. always reset
  1805. * moved check for local/para to be only used for varsym
  1806. Revision 1.63 2000/03/28 22:11:48 pierre
  1807. + add a warning if esi is base and index in object assembler code
  1808. Revision 1.62 2000/03/27 21:18:55 pierre
  1809. * "segss" prefix in Intel is converted into "ss" in ATT
  1810. and vice-versa. Fixes web bug 892.
  1811. Revision 1.61 2000/03/15 23:10:01 pierre
  1812. * fix for bug 848 (that still genrated wrong code)
  1813. + better testing for variables used in assembler
  1814. (gives an error if variable is not directly reachable !)
  1815. Revision 1.60 2000/03/02 11:48:31 pierre
  1816. * fix for bug 848
  1817. Revision 1.59 2000/02/13 22:46:28 florian
  1818. * fixed an internalerror with writeln
  1819. * fixed arrayconstructor_to_set to force the generation of better code
  1820. and added a more strict type checking
  1821. Revision 1.58 2000/02/09 13:23:02 peter
  1822. * log truncated
  1823. Revision 1.57 2000/01/07 01:14:36 peter
  1824. * updated copyright to 2000
  1825. Revision 1.56 1999/12/18 20:00:33 florian
  1826. * Bug reported by Marco fixed: Intel assembler reader: fld qword ptr x
  1827. was read as fldq x but it must be fldl x
  1828. Revision 1.55 1999/12/01 12:42:32 peter
  1829. * fixed bug 698
  1830. * removed some notes about unused vars
  1831. Revision 1.54 1999/11/30 10:40:53 peter
  1832. + ttype, tsymlist
  1833. Revision 1.53 1999/11/17 17:05:03 pierre
  1834. * Notes/hints changes
  1835. Revision 1.52 1999/11/09 23:06:46 peter
  1836. * esi_offset -> selfpointer_offset to be newcg compatible
  1837. * hcogegen -> cgbase fixes for newcg
  1838. Revision 1.51 1999/11/06 14:34:24 peter
  1839. * truncated log to 20 revs
  1840. Revision 1.50 1999/10/01 07:59:21 peter
  1841. * fixed object field parsing
  1842. Revision 1.49 1999/09/27 23:44:58 peter
  1843. * procinfo is now a pointer
  1844. * support for result setting in sub procedure
  1845. Revision 1.48 1999/09/20 16:39:01 peter
  1846. * cs_create_smart instead of cs_smartlink
  1847. * -CX is create smartlink
  1848. * -CD is create dynamic, but does nothing atm.
  1849. Revision 1.47 1999/09/15 20:35:43 florian
  1850. * small fix to operator overloading when in MMX mode
  1851. + the compiler uses now fldz and fld1 if possible
  1852. + some fixes to floating point registers
  1853. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1854. * .... ???
  1855. Revision 1.46 1999/09/08 16:04:03 peter
  1856. * better support for object fields and more error checks for
  1857. field accesses which create buggy code
  1858. Revision 1.45 1999/09/07 13:03:10 peter
  1859. * better OFFSET support for reference reading
  1860. Revision 1.44 1999/09/07 07:45:41 peter
  1861. * TYPE support
  1862. Revision 1.43 1999/08/13 21:28:36 peter
  1863. * more reference types support
  1864. * arraydef size returns elementsize, also for multiple indexing array
  1865. Revision 1.42 1999/08/04 00:23:27 florian
  1866. * renamed i386asm and i386base to cpuasm and cpubase
  1867. Revision 1.41 1999/07/24 11:17:16 peter
  1868. * suffix parsing for at&t fixed for things like movsbl
  1869. * string constants are now handle correctly and also allowed in
  1870. constant expressions
  1871. }