ra386int.pas 54 KB

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