ra386int.pas 56 KB

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