ra386int.pas 69 KB

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