rautils.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  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. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. {---------------------------------------------------------------------
  28. Local Label Management
  29. ---------------------------------------------------------------------}
  30. Type
  31. { Each local label has this structure associated with it }
  32. TLocalLabel = class(TFPHashObject)
  33. Emitted : boolean;
  34. constructor Create(AList:TFPHashObjectList;const n:string);
  35. function Gettasmlabel:tasmlabel;
  36. private
  37. lab : tasmlabel;
  38. end;
  39. TLocalLabelList = class(TFPHashObjectList)
  40. procedure CheckEmitted;
  41. end;
  42. var
  43. LocalLabelList : TLocalLabelList;
  44. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  45. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  46. {---------------------------------------------------------------------
  47. Instruction management
  48. ---------------------------------------------------------------------}
  49. type
  50. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  51. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET,OPR_SHIFTEROP);
  52. TOprRec = record
  53. case typ:TOprType of
  54. OPR_NONE : ();
  55. OPR_CONSTANT : (val:aint);
  56. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  57. OPR_REFERENCE : (ref:treference);
  58. OPR_LOCAL : (localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  59. OPR_REGISTER : (reg:tregister);
  60. {$ifdef m68k}
  61. OPR_REGLIST : (regset : tcpuregisterset);
  62. {$endif m68k}
  63. {$ifdef powerpc}
  64. OPR_COND : (cond : tasmcond);
  65. {$endif powerpc}
  66. {$ifdef POWERPC64}
  67. OPR_COND : (cond : tasmcond);
  68. {$endif POWERPC64}
  69. {$ifdef arm}
  70. OPR_REGSET : (regset : tcpuregisterset);
  71. OPR_SHIFTEROP : (shifterop : tshifterop);
  72. {$endif arm}
  73. end;
  74. TOperand = class
  75. typesize : aint;
  76. hastype, { if the operand has typecasted variable }
  77. hasvar : boolean; { if the operand is loaded with a variable }
  78. size : TCGSize;
  79. opr : TOprRec;
  80. constructor create;virtual;
  81. destructor destroy;override;
  82. Procedure SetSize(_size:longint;force:boolean);virtual;
  83. Procedure SetCorrectSize(opcode:tasmop);virtual;
  84. Function SetupResult:boolean;virtual;
  85. Function SetupSelf:boolean;
  86. Function SetupOldEBP:boolean;
  87. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  88. Procedure CheckOperand; virtual;
  89. Procedure InitRef;
  90. end;
  91. TCOperand = class of TOperand;
  92. TInstruction = class
  93. opcode : tasmop;
  94. condition : tasmcond;
  95. ops : byte;
  96. labeled : boolean;
  97. operands : array[1..max_operands] of toperand;
  98. constructor create(optype : tcoperand);virtual;
  99. destructor destroy;override;
  100. { converts the instruction to an instruction how it's used by the assembler writer
  101. and concats it to the passed list, the newly created item is returned }
  102. function ConcatInstruction(p:TAsmList) : tai;virtual;
  103. Procedure Swapoperands;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. {**********************************************************************}
  113. { The following operators are supported: }
  114. { '+' : addition }
  115. { '-' : subtraction }
  116. { '*' : multiplication }
  117. { '/' : modulo division }
  118. { '^' : exclusive or }
  119. { '<' : shift left }
  120. { '>' : shift right }
  121. { '&' : bitwise and }
  122. { '|' : bitwise or }
  123. { '~' : bitwise complement }
  124. { '%' : modulo division }
  125. { nnn: longint numbers }
  126. { ( and ) parenthesis }
  127. {**********************************************************************}
  128. TExprParse = class
  129. public
  130. Constructor create;
  131. Destructor Destroy;override;
  132. Function Evaluate(Expr: String): aint;
  133. Function Priority(_Operator: Char): aint;
  134. private
  135. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  136. RPNTop : aint;
  137. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  138. OpTop : aint;
  139. Procedure RPNPush(Num: aint);
  140. Function RPNPop: aint;
  141. Procedure RPNCalc(const token: String; prefix: boolean);
  142. Procedure OpPush(_Operator: char; prefix: boolean);
  143. { In reality returns TExprOperaotr }
  144. Procedure OpPop(var _Operator:TExprOperator);
  145. end;
  146. { Evaluate an expression string to a aint }
  147. Function CalculateExpression(const expression: string): aint;
  148. {---------------------------------------------------------------------}
  149. { String routines }
  150. {---------------------------------------------------------------------}
  151. Function ParseVal(const S:String;base:byte):aint;
  152. Function PadZero(Var s: String; n: byte): Boolean;
  153. Function EscapeToPascal(const s:string): string;
  154. {---------------------------------------------------------------------
  155. Symbol helper routines
  156. ---------------------------------------------------------------------}
  157. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  158. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  159. Function SearchType(const hs:string;var size:aint): Boolean;
  160. Function SearchRecordType(const s:string): boolean;
  161. Function SearchIConstant(const s:string; var l:aint): boolean;
  162. {---------------------------------------------------------------------
  163. Instruction generation routines
  164. ---------------------------------------------------------------------}
  165. Procedure ConcatPasString(p : TAsmList;s:string);
  166. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  167. Procedure ConcatConstant(p : TAsmList;value: aint; constsize:byte);
  168. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  169. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  170. Procedure ConcatString(p : TAsmList;s:string);
  171. procedure ConcatAlign(p:TAsmList;l:aint);
  172. Procedure ConcatPublic(p:TAsmList;const s : string);
  173. Procedure ConcatLocal(p:TAsmList;const s : string);
  174. Implementation
  175. uses
  176. SysUtils,
  177. defutil,systems,verbose,globals,
  178. symtable,paramgr,
  179. aasmcpu,
  180. procinfo;
  181. {*************************************************************************
  182. TExprParse
  183. *************************************************************************}
  184. Constructor TExprParse.create;
  185. Begin
  186. end;
  187. Procedure TExprParse.RPNPush(Num : aint);
  188. { Add an operand to the top of the RPN stack }
  189. begin
  190. if RPNTop < RPNMax then
  191. begin
  192. Inc(RPNTop);
  193. RPNStack[RPNTop]:=Num;
  194. end
  195. else
  196. Message(asmr_e_expr_illegal);
  197. end;
  198. Function TExprParse.RPNPop : aint;
  199. { Get the operand at the top of the RPN stack }
  200. begin
  201. if RPNTop > 0 then
  202. begin
  203. RPNPop:=RPNStack[RPNTop];
  204. Dec(RPNTop);
  205. end
  206. else
  207. Message(asmr_e_expr_illegal);
  208. end;
  209. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  210. Var
  211. Temp : aint;
  212. n1,n2 : aint;
  213. LocalError : Integer;
  214. begin
  215. { Handle operators }
  216. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  217. Case Token[1] of
  218. '+' :
  219. Begin
  220. if not prefix then
  221. RPNPush(RPNPop + RPNPop);
  222. end;
  223. '-' :
  224. Begin
  225. if prefix then
  226. RPNPush(-(RPNPop))
  227. else
  228. begin
  229. n1:=RPNPop;
  230. n2:=RPNPop;
  231. RPNPush(n2 - n1);
  232. end;
  233. end;
  234. '*' : RPNPush(RPNPop * RPNPop);
  235. '&' :
  236. begin
  237. n1:=RPNPop;
  238. n2:=RPNPop;
  239. RPNPush(n2 and n1);
  240. end;
  241. '|' :
  242. begin
  243. n1:=RPNPop;
  244. n2:=RPNPop;
  245. RPNPush(n2 or n1);
  246. end;
  247. '~' : RPNPush(NOT RPNPop);
  248. '<' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 SHL n1);
  253. end;
  254. '>' :
  255. begin
  256. n1:=RPNPop;
  257. n2:=RPNPop;
  258. RPNPush(n2 SHR n1);
  259. end;
  260. '%' :
  261. begin
  262. Temp:=RPNPop;
  263. if Temp <> 0 then
  264. RPNPush(RPNPop mod Temp)
  265. else
  266. begin
  267. Message(asmr_e_expr_zero_divide);
  268. { push 1 for error recovery }
  269. RPNPush(1);
  270. end;
  271. end;
  272. '^' : RPNPush(RPNPop XOR RPNPop);
  273. '/' :
  274. begin
  275. Temp:=RPNPop;
  276. if Temp <> 0 then
  277. RPNPush(RPNPop div Temp)
  278. else
  279. begin
  280. Message(asmr_e_expr_zero_divide);
  281. { push 1 for error recovery }
  282. RPNPush(1);
  283. end;
  284. end;
  285. end
  286. else
  287. begin
  288. { Convert String to number and add to stack }
  289. Val(Token, Temp, LocalError);
  290. if LocalError = 0 then
  291. RPNPush(Temp)
  292. else
  293. begin
  294. Message(asmr_e_expr_illegal);
  295. { push 1 for error recovery }
  296. RPNPush(1);
  297. end;
  298. end;
  299. end;
  300. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  301. { Add an operator onto top of the stack }
  302. begin
  303. if OpTop < OpMax then
  304. begin
  305. Inc(OpTop);
  306. OpStack[OpTop].ch:=_Operator;
  307. OpStack[OpTop].is_prefix:=prefix;
  308. end
  309. else
  310. Message(asmr_e_expr_illegal);
  311. end;
  312. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  313. { Get operator at the top of the stack }
  314. begin
  315. if OpTop > 0 then
  316. begin
  317. _Operator:=OpStack[OpTop];
  318. Dec(OpTop);
  319. end
  320. else
  321. Message(asmr_e_expr_illegal);
  322. end;
  323. Function TExprParse.Priority(_Operator : Char) : aint;
  324. { Return priority of operator }
  325. { The greater the priority, the higher the precedence }
  326. begin
  327. Case _Operator OF
  328. '(' :
  329. Priority:=0;
  330. '+', '-' :
  331. Priority:=1;
  332. '*', '/','%','<','>' :
  333. Priority:=2;
  334. '|','&','^','~' :
  335. Priority:=0;
  336. else
  337. Message(asmr_e_expr_illegal);
  338. end;
  339. end;
  340. Function TExprParse.Evaluate(Expr : String):aint;
  341. Var
  342. I : longint;
  343. Token : String;
  344. opr : TExprOperator;
  345. begin
  346. Evaluate:=0;
  347. { Reset stacks }
  348. OpTop :=0;
  349. RPNTop:=0;
  350. Token :='';
  351. { nothing to do ? }
  352. if Expr='' then
  353. exit;
  354. For I:=1 to Length(Expr) DO
  355. begin
  356. if Expr[I] in ['0'..'9'] then
  357. begin { Build multi-digit numbers }
  358. Token:=Token + Expr[I];
  359. if I = Length(Expr) then { Send last one to calculator }
  360. RPNCalc(Token,false);
  361. end
  362. else
  363. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  364. begin
  365. if Token <> '' then
  366. begin { Send last built number to calc. }
  367. RPNCalc(Token,false);
  368. Token:='';
  369. end;
  370. Case Expr[I] OF
  371. '(' : OpPush('(',false);
  372. ')' : begin
  373. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  374. Begin
  375. OpPop(opr);
  376. RPNCalc(opr.ch,opr.is_prefix);
  377. end;
  378. OpPop(opr); { Pop off and ignore the '(' }
  379. end;
  380. '+','-','~' : Begin
  381. { workaround for -2147483648 }
  382. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  383. begin
  384. token:='-';
  385. expr[i]:='+';
  386. end;
  387. { if start of expression then surely a prefix }
  388. { or if previous char was also an operator }
  389. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  390. OpPush(Expr[I],true)
  391. else
  392. Begin
  393. { Evaluate all higher priority operators }
  394. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  395. Begin
  396. OpPop(opr);
  397. RPNCalc(opr.ch,opr.is_prefix);
  398. end;
  399. OpPush(Expr[I],false);
  400. End;
  401. end;
  402. '*', '/',
  403. '^','|','&',
  404. '%','<','>' : begin
  405. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  406. Begin
  407. OpPop(opr);
  408. RPNCalc(opr.ch,opr.is_prefix);
  409. end;
  410. OpPush(Expr[I],false);
  411. end;
  412. end; { Case }
  413. end
  414. else
  415. Message(asmr_e_expr_illegal); { Handle bad input error }
  416. end;
  417. { Pop off the remaining operators }
  418. While OpTop > 0 do
  419. Begin
  420. OpPop(opr);
  421. RPNCalc(opr.ch,opr.is_prefix);
  422. end;
  423. { The result is stored on the top of the stack }
  424. Evaluate:=RPNPop;
  425. end;
  426. Destructor TExprParse.Destroy;
  427. Begin
  428. end;
  429. Function CalculateExpression(const expression: string): aint;
  430. var
  431. expr: TExprParse;
  432. Begin
  433. expr:=TExprParse.create;
  434. CalculateExpression:=expr.Evaluate(expression);
  435. expr.Free;
  436. end;
  437. {*************************************************************************}
  438. { String conversions/utils }
  439. {*************************************************************************}
  440. Function EscapeToPascal(const s:string): string;
  441. { converts a C styled string - which contains escape }
  442. { characters to a pascal style string. }
  443. var
  444. i,len : aint;
  445. hs : string;
  446. temp : string;
  447. c : char;
  448. Begin
  449. hs:='';
  450. len:=0;
  451. i:=0;
  452. while (i<length(s)) and (len<255) do
  453. begin
  454. Inc(i);
  455. if (s[i]='\') and (i<length(s)) then
  456. Begin
  457. inc(i);
  458. case s[i] of
  459. '\' :
  460. c:='\';
  461. 'b':
  462. c:=#8;
  463. 'f':
  464. c:=#12;
  465. 'n':
  466. c:=#10;
  467. 'r':
  468. c:=#13;
  469. 't':
  470. c:=#9;
  471. '"':
  472. c:='"';
  473. '0'..'7':
  474. Begin
  475. temp:=s[i];
  476. temp:=temp+s[i+1];
  477. temp:=temp+s[i+2];
  478. inc(i,2);
  479. c:=chr(ParseVal(temp,8));
  480. end;
  481. 'x':
  482. Begin
  483. temp:=s[i+1];
  484. temp:=temp+s[i+2];
  485. inc(i,2);
  486. c:=chr(ParseVal(temp,16));
  487. end;
  488. else
  489. Begin
  490. Message1(asmr_e_escape_seq_ignored,s[i]);
  491. c:=s[i];
  492. end;
  493. end;
  494. end
  495. else
  496. c:=s[i];
  497. inc(len);
  498. hs[len]:=c;
  499. end;
  500. hs[0]:=chr(len);
  501. EscapeToPascal:=hs;
  502. end;
  503. Function ParseVal(const S:String;base:byte):aint;
  504. { Converts a decimal string to aint }
  505. var
  506. code : integer;
  507. errmsg : word;
  508. prefix : string[2];
  509. Begin
  510. case base of
  511. 2 :
  512. begin
  513. errmsg:=asmr_e_error_converting_binary;
  514. prefix:='%';
  515. end;
  516. 8 :
  517. begin
  518. errmsg:=asmr_e_error_converting_octal;
  519. prefix:='&';
  520. end;
  521. 10 :
  522. begin
  523. errmsg:=asmr_e_error_converting_decimal;
  524. prefix:='';
  525. end;
  526. 16 :
  527. begin
  528. errmsg:=asmr_e_error_converting_hexadecimal;
  529. prefix:='$';
  530. end;
  531. else
  532. internalerror(200501202);
  533. end;
  534. val(prefix+s,result,code);
  535. if code<>0 then
  536. begin
  537. val(prefix+s,aword(result),code);
  538. if code<>0 then
  539. begin
  540. Message1(errmsg,s);
  541. result:=0;
  542. end;
  543. end;
  544. end;
  545. Function PadZero(Var s: String; n: byte): Boolean;
  546. Begin
  547. PadZero:=TRUE;
  548. { Do some error checking first }
  549. if Length(s) = n then
  550. exit
  551. else
  552. if Length(s) > n then
  553. Begin
  554. PadZero:=FALSE;
  555. delete(s,n+1,length(s));
  556. exit;
  557. end
  558. else
  559. PadZero:=TRUE;
  560. { Fill it up with the specified character }
  561. fillchar(s[length(s)+1],n-1,#0);
  562. s[0]:=chr(n);
  563. end;
  564. {****************************************************************************
  565. TOperand
  566. ****************************************************************************}
  567. constructor TOperand.Create;
  568. begin
  569. size:=OS_NO;
  570. hastype:=false;
  571. hasvar:=false;
  572. FillChar(Opr,sizeof(Opr),0);
  573. end;
  574. destructor TOperand.destroy;
  575. begin
  576. end;
  577. Procedure TOperand.SetSize(_size:longint;force:boolean);
  578. begin
  579. if force or
  580. ((size = OS_NO) and (_size<=16)) then
  581. Begin
  582. case _size of
  583. 1 : size:=OS_8;
  584. 2 : size:=OS_16{ could be S_IS};
  585. 4 : size:=OS_32{ could be S_IL or S_FS};
  586. 8 : size:=OS_64{ could be S_D or S_FL};
  587. 10 : size:=OS_F80;
  588. 16 : size:=OS_128;
  589. end;
  590. end;
  591. end;
  592. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  593. begin
  594. end;
  595. function TOperand.SetupResult:boolean;
  596. begin
  597. SetupResult:=false;
  598. { replace by correct offset. }
  599. with current_procinfo.procdef do
  600. if (not is_void(returndef)) then
  601. begin
  602. if (m_tp7 in current_settings.modeswitches) and
  603. (not paramanager.ret_in_param(returndef,proccalloption)) then
  604. begin
  605. message(asmr_e_cannot_use_RESULT_here);
  606. exit;
  607. end;
  608. SetupResult:=setupvar('result',false)
  609. end
  610. else
  611. message(asmr_e_void_function);
  612. end;
  613. Function TOperand.SetupSelf:boolean;
  614. Begin
  615. SetupSelf:=false;
  616. if assigned(current_procinfo.procdef._class) then
  617. SetupSelf:=setupvar('self',false)
  618. else
  619. Message(asmr_e_cannot_use_SELF_outside_a_method);
  620. end;
  621. Function TOperand.SetupOldEBP:boolean;
  622. Begin
  623. SetupOldEBP:=false;
  624. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  625. SetupOldEBP:=setupvar('parentframe',false)
  626. else
  627. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  628. end;
  629. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  630. function symtable_has_localvarsyms(st:TSymtable):boolean;
  631. var
  632. sym : tsym;
  633. i : longint;
  634. begin
  635. result:=false;
  636. for i:=0 to st.SymList.Count-1 do
  637. begin
  638. sym:=tsym(st.SymList[i]);
  639. if sym.typ=localvarsym then
  640. begin
  641. result:=true;
  642. exit;
  643. end;
  644. end;
  645. end;
  646. procedure setconst(l:aint);
  647. begin
  648. { We return the address of the field, just like Delphi/TP }
  649. case opr.typ of
  650. OPR_NONE :
  651. begin
  652. opr.typ:=OPR_CONSTANT;
  653. opr.val:=l;
  654. end;
  655. OPR_CONSTANT :
  656. inc(opr.val,l);
  657. OPR_REFERENCE :
  658. inc(opr.ref.offset,l);
  659. OPR_LOCAL :
  660. inc(opr.localsymofs,l);
  661. else
  662. Message(asmr_e_invalid_operand_type);
  663. end;
  664. end;
  665. { search and sets up the correct fields in the Instr record }
  666. { for the NON-constant identifier passed to the routine. }
  667. { if not found returns FALSE. }
  668. var
  669. sym : tsym;
  670. srsymtable : TSymtable;
  671. harrdef : tarraydef;
  672. indexreg : tregister;
  673. l : aint;
  674. plist : ppropaccesslistitem;
  675. Begin
  676. SetupVar:=false;
  677. asmsearchsym(s,sym,srsymtable);
  678. if sym = nil then
  679. exit;
  680. if sym.typ=absolutevarsym then
  681. begin
  682. if (tabsolutevarsym(sym).abstyp=tovar) then
  683. begin
  684. { Only support simple loads }
  685. plist:=tabsolutevarsym(sym).ref.firstsym;
  686. if assigned(plist) and
  687. (plist^.sltype=sl_load) then
  688. sym:=plist^.sym
  689. else
  690. begin
  691. Message(asmr_e_unsupported_symbol_type);
  692. exit;
  693. end;
  694. end
  695. else
  696. begin
  697. Message(asmr_e_unsupported_symbol_type);
  698. exit;
  699. end;
  700. end;
  701. case sym.typ of
  702. fieldvarsym :
  703. begin
  704. setconst(tfieldvarsym(sym).fieldoffset);
  705. hasvar:=true;
  706. SetupVar:=true;
  707. end;
  708. staticvarsym,
  709. localvarsym,
  710. paravarsym :
  711. begin
  712. { we always assume in asm statements that }
  713. { that the variable is valid. }
  714. tabstractvarsym(sym).varstate:=vs_readwritten;
  715. inc(tabstractvarsym(sym).refs);
  716. { variable can't be placed in a register }
  717. tabstractvarsym(sym).varregable:=vr_none;
  718. case sym.typ of
  719. staticvarsym :
  720. begin
  721. initref;
  722. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname);
  723. end;
  724. paravarsym,
  725. localvarsym :
  726. begin
  727. if opr.typ=OPR_REFERENCE then
  728. begin
  729. indexreg:=opr.ref.base;
  730. if opr.ref.index<>NR_NO then
  731. begin
  732. if indexreg=NR_NO then
  733. indexreg:=opr.ref.index
  734. else
  735. Message(asmr_e_multiple_index);
  736. end;
  737. end
  738. else
  739. indexreg:=NR_NO;
  740. opr.typ:=OPR_LOCAL;
  741. if assigned(current_procinfo.parent) and
  742. not(po_inline in current_procinfo.procdef.procoptions) and
  743. (sym.owner<>current_procinfo.procdef.localst) and
  744. (sym.owner<>current_procinfo.procdef.parast) and
  745. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  746. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  747. message1(asmr_e_local_para_unreachable,s);
  748. opr.localsym:=tabstractnormalvarsym(sym);
  749. opr.localsymofs:=0;
  750. opr.localindexreg:=indexreg;
  751. opr.localscale:=0;
  752. opr.localgetoffset:=GetOffset;
  753. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  754. SetSize(sizeof(aint),false);
  755. end;
  756. end;
  757. case tabstractvarsym(sym).vardef.typ of
  758. orddef,
  759. enumdef,
  760. pointerdef,
  761. floatdef :
  762. SetSize(tabstractvarsym(sym).getsize,false);
  763. arraydef :
  764. begin
  765. { for arrays try to get the element size, take care of
  766. multiple indexes }
  767. harrdef:=tarraydef(tabstractvarsym(sym).vardef);
  768. while assigned(harrdef.elementdef) and
  769. (harrdef.elementdef.typ=arraydef) do
  770. harrdef:=tarraydef(harrdef.elementdef);
  771. if not is_packed_array(harrdef) then
  772. SetSize(harrdef.elesize,false)
  773. else
  774. begin
  775. if (harrdef.elepackedbitsize mod 8) <> 0 then
  776. Message(asmr_e_packed_element);
  777. SetSize((harrdef.elepackedbitsize + 7) div 8,false);
  778. end;
  779. end;
  780. end;
  781. hasvar:=true;
  782. SetupVar:=true;
  783. Exit;
  784. end;
  785. constsym :
  786. begin
  787. if tconstsym(sym).consttyp=constord then
  788. begin
  789. setconst(tconstsym(sym).value.valueord);
  790. SetupVar:=true;
  791. Exit;
  792. end;
  793. end;
  794. typesym :
  795. begin
  796. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  797. begin
  798. setconst(0);
  799. SetupVar:=TRUE;
  800. Exit;
  801. end;
  802. end;
  803. procsym :
  804. begin
  805. if opr.typ<>OPR_NONE then
  806. Message(asmr_e_invalid_operand_type);
  807. if Tprocsym(sym).ProcdefList.Count>1 then
  808. Message(asmr_w_calling_overload_func);
  809. l:=opr.ref.offset;
  810. opr.typ:=OPR_SYMBOL;
  811. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname);
  812. opr.symofs:=l;
  813. hasvar:=true;
  814. SetupVar:=TRUE;
  815. Exit;
  816. end;
  817. else
  818. begin
  819. Message(asmr_e_unsupported_symbol_type);
  820. exit;
  821. end;
  822. end;
  823. end;
  824. procedure TOperand.InitRef;
  825. {*********************************************************************}
  826. { Description: This routine first check if the opcode is of }
  827. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  828. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  829. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  830. { to point to the default segment. }
  831. {*********************************************************************}
  832. var
  833. l : aint;
  834. hsymofs : aint;
  835. hsymbol : tasmsymbol;
  836. reg : tregister;
  837. Begin
  838. case opr.typ of
  839. OPR_REFERENCE :
  840. exit;
  841. OPR_CONSTANT :
  842. begin
  843. l:=opr.val;
  844. opr.typ:=OPR_REFERENCE;
  845. Fillchar(opr.ref,sizeof(treference),0);
  846. opr.Ref.Offset:=l;
  847. end;
  848. OPR_NONE :
  849. begin
  850. opr.typ:=OPR_REFERENCE;
  851. Fillchar(opr.ref,sizeof(treference),0);
  852. end;
  853. OPR_REGISTER :
  854. begin
  855. reg:=opr.reg;
  856. opr.typ:=OPR_REFERENCE;
  857. Fillchar(opr.ref,sizeof(treference),0);
  858. opr.Ref.base:=reg;
  859. end;
  860. OPR_SYMBOL :
  861. begin
  862. hsymbol:=opr.symbol;
  863. hsymofs:=opr.symofs;
  864. opr.typ:=OPR_REFERENCE;
  865. Fillchar(opr.ref,sizeof(treference),0);
  866. opr.ref.symbol:=hsymbol;
  867. opr.ref.offset:=hsymofs;
  868. end;
  869. else
  870. begin
  871. Message(asmr_e_invalid_operand_type);
  872. { Recover }
  873. opr.typ:=OPR_REFERENCE;
  874. Fillchar(opr.ref,sizeof(treference),0);
  875. end;
  876. end;
  877. end;
  878. Procedure TOperand.CheckOperand;
  879. {*********************************************************************}
  880. { Description: This routine checks if the operand is of }
  881. { valid. Does nothing by default. }
  882. {*********************************************************************}
  883. begin
  884. end;
  885. {****************************************************************************
  886. TInstruction
  887. ****************************************************************************}
  888. constructor TInstruction.create(optype : tcoperand);
  889. var
  890. i : longint;
  891. Begin
  892. { these field are set to 0 anyways by the constructor helper (FK)
  893. Opcode:=A_NONE;
  894. Condition:=C_NONE;
  895. Ops:=0;
  896. }
  897. for i:=1 to max_operands do
  898. Operands[i]:=optype.create;
  899. Labeled:=false;
  900. end;
  901. destructor TInstruction.destroy;
  902. var
  903. i : longint;
  904. Begin
  905. for i:=1 to max_operands do
  906. Operands[i].free;
  907. end;
  908. Procedure TInstruction.Swapoperands;
  909. Var
  910. p : toperand;
  911. Begin
  912. case Ops of
  913. 2 :
  914. begin
  915. p:=Operands[1];
  916. Operands[1]:=Operands[2];
  917. Operands[2]:=p;
  918. end;
  919. 3 :
  920. begin
  921. p:=Operands[1];
  922. Operands[1]:=Operands[3];
  923. Operands[3]:=p;
  924. end;
  925. end;
  926. end;
  927. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  928. var
  929. ai : taicpu;
  930. i : longint;
  931. begin
  932. for i:=1 to Ops do
  933. operands[i].CheckOperand;
  934. ai:=taicpu.op_none(opcode);
  935. ai.Ops:=Ops;
  936. ai.Allocate_oper(Ops);
  937. for i:=1 to Ops do
  938. with operands[i].opr do
  939. begin
  940. case typ of
  941. OPR_CONSTANT :
  942. ai.loadconst(i-1,val);
  943. OPR_REGISTER:
  944. ai.loadreg(i-1,reg);
  945. OPR_SYMBOL:
  946. ai.loadsymbol(i-1,symbol,symofs);
  947. OPR_LOCAL :
  948. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  949. localscale,localgetoffset,localforceref);
  950. OPR_REFERENCE:
  951. ai.loadref(i-1,ref);
  952. {$ifdef ARM}
  953. OPR_REGSET:
  954. ai.loadregset(i-1,regset);
  955. OPR_SHIFTEROP:
  956. ai.loadshifterop(i-1,shifterop);
  957. {$endif ARM}
  958. else
  959. internalerror(200501051);
  960. end;
  961. end;
  962. ai.SetCondition(condition);
  963. { Concat the opcode or give an error }
  964. if assigned(ai) then
  965. p.concat(ai)
  966. else
  967. Message(asmr_e_invalid_opcode_and_operand);
  968. result:=ai;
  969. end;
  970. {***************************************************************************
  971. TLocalLabel
  972. ***************************************************************************}
  973. constructor TLocalLabel.create(AList:TFPHashObjectList;const n:string);
  974. begin
  975. inherited Create(AList,n);
  976. lab:=nil;
  977. emitted:=false;
  978. end;
  979. function TLocalLabel.Gettasmlabel:tasmlabel;
  980. begin
  981. if not assigned(lab) then
  982. begin
  983. current_asmdata.getjumplabel(lab);
  984. { this label is forced to be used so it's always written }
  985. lab.increfs;
  986. end;
  987. Gettasmlabel:=lab;
  988. end;
  989. {***************************************************************************
  990. TLocalLabelList
  991. ***************************************************************************}
  992. procedure TLocalLabelList.CheckEmitted;
  993. var
  994. i : longint;
  995. lab : TLocalLabel;
  996. begin
  997. for i:=0 to LocalLabelList.Count-1 do
  998. begin
  999. lab:=TLocalLabel(LocalLabelList[i]);
  1000. if not lab.emitted then
  1001. Message1(asmr_e_unknown_label_identifier,lab.name);
  1002. end;
  1003. end;
  1004. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1005. var
  1006. lab : TLocalLabel;
  1007. Begin
  1008. CreateLocalLabel:=true;
  1009. { Check if it already is defined }
  1010. lab:=TLocalLabel(LocalLabellist.Find(s));
  1011. if not assigned(lab) then
  1012. lab:=TLocalLabel.Create(LocalLabellist,s);
  1013. { set emitted flag and check for dup syms }
  1014. if emit then
  1015. begin
  1016. if lab.Emitted then
  1017. begin
  1018. Message1(asmr_e_dup_local_sym,lab.Name);
  1019. CreateLocalLabel:=false;
  1020. end;
  1021. lab.Emitted:=true;
  1022. end;
  1023. hl:=lab.Gettasmlabel;
  1024. end;
  1025. {****************************************************************************
  1026. Symbol table helper routines
  1027. ****************************************************************************}
  1028. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  1029. var
  1030. i : integer;
  1031. begin
  1032. i:=pos('.',s);
  1033. { allow unit.identifier }
  1034. if i>0 then
  1035. begin
  1036. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1037. if assigned(srsym) then
  1038. begin
  1039. if (srsym.typ=unitsym) and
  1040. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1041. srsym.owner.iscurrentunit then
  1042. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1043. else
  1044. begin
  1045. srsym:=nil;
  1046. srsymtable:=nil;
  1047. end;
  1048. end;
  1049. end
  1050. else
  1051. searchsym(s,srsym,srsymtable);
  1052. end;
  1053. Function SearchType(const hs:string;var size:aint): Boolean;
  1054. var
  1055. srsym : tsym;
  1056. srsymtable : TSymtable;
  1057. begin
  1058. result:=false;
  1059. size:=0;
  1060. asmsearchsym(hs,srsym,srsymtable);
  1061. if assigned(srsym) and
  1062. (srsym.typ=typesym) then
  1063. begin
  1064. size:=ttypesym(srsym).typedef.size;
  1065. result:=true;
  1066. end;
  1067. end;
  1068. Function SearchRecordType(const s:string): boolean;
  1069. var
  1070. srsym : tsym;
  1071. srsymtable : TSymtable;
  1072. Begin
  1073. SearchRecordType:=false;
  1074. { Check the constants in symtable }
  1075. asmsearchsym(s,srsym,srsymtable);
  1076. if srsym <> nil then
  1077. Begin
  1078. case srsym.typ of
  1079. typesym :
  1080. begin
  1081. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1082. begin
  1083. SearchRecordType:=true;
  1084. exit;
  1085. end;
  1086. end;
  1087. fieldvarsym :
  1088. begin
  1089. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1090. begin
  1091. SearchRecordType:=true;
  1092. exit;
  1093. end;
  1094. end;
  1095. end;
  1096. end;
  1097. end;
  1098. Function SearchIConstant(const s:string; var l:aint): boolean;
  1099. {**********************************************************************}
  1100. { Description: Searches for a CONSTANT of name s in either the local }
  1101. { symbol list, then in the global symbol list, and returns the value }
  1102. { of that constant in l. Returns TRUE if successfull, if not found, }
  1103. { or if the constant is not of correct type, then returns FALSE }
  1104. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1105. { respectively. }
  1106. {**********************************************************************}
  1107. var
  1108. srsym : tsym;
  1109. srsymtable : TSymtable;
  1110. Begin
  1111. SearchIConstant:=false;
  1112. { check for TRUE or FALSE reserved words first }
  1113. if s = 'TRUE' then
  1114. Begin
  1115. SearchIConstant:=TRUE;
  1116. l:=1;
  1117. exit;
  1118. end;
  1119. if s = 'FALSE' then
  1120. Begin
  1121. SearchIConstant:=TRUE;
  1122. l:=0;
  1123. exit;
  1124. end;
  1125. { Check the constants in symtable }
  1126. asmsearchsym(s,srsym,srsymtable);
  1127. if srsym <> nil then
  1128. Begin
  1129. case srsym.typ of
  1130. constsym :
  1131. begin
  1132. if tconstsym(srsym).consttyp=constord then
  1133. Begin
  1134. l:=tconstsym(srsym).value.valueord;
  1135. SearchIConstant:=TRUE;
  1136. exit;
  1137. end;
  1138. end;
  1139. enumsym:
  1140. Begin
  1141. l:=tenumsym(srsym).value;
  1142. SearchIConstant:=TRUE;
  1143. exit;
  1144. end;
  1145. end;
  1146. end;
  1147. end;
  1148. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  1149. { search and returns the offset and size of records/objects of the base }
  1150. { with field name setup in field. }
  1151. { returns FALSE if not found. }
  1152. { used when base is a variable or a typed constant name. }
  1153. var
  1154. st : TSymtable;
  1155. harrdef : tarraydef;
  1156. sym : tsym;
  1157. srsymtable : TSymtable;
  1158. i : longint;
  1159. base : string;
  1160. procdef: tprocdef;
  1161. Begin
  1162. GetRecordOffsetSize:=FALSE;
  1163. Offset:=0;
  1164. Size:=0;
  1165. mangledname:='';
  1166. i:=pos('.',s);
  1167. if i=0 then
  1168. i:=255;
  1169. base:=Copy(s,1,i-1);
  1170. delete(s,1,i);
  1171. if base='SELF' then
  1172. st:=current_procinfo.procdef._class.symtable
  1173. else
  1174. begin
  1175. asmsearchsym(base,sym,srsymtable);
  1176. st:=nil;
  1177. { we can start with a var,type,typedconst }
  1178. if assigned(sym) then
  1179. case sym.typ of
  1180. staticvarsym,
  1181. localvarsym,
  1182. paravarsym :
  1183. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1184. typesym :
  1185. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1186. end
  1187. else
  1188. s:='';
  1189. end;
  1190. { now walk all recordsymtables }
  1191. while assigned(st) and (s<>'') do
  1192. begin
  1193. { load next field in base }
  1194. i:=pos('.',s);
  1195. if i=0 then
  1196. i:=255;
  1197. base:=Copy(s,1,i-1);
  1198. delete(s,1,i);
  1199. if st.symtabletype=ObjectSymtable then
  1200. sym:=search_class_member(tobjectdef(st.defowner),base)
  1201. else
  1202. sym:=tsym(st.Find(base));
  1203. if not assigned(sym) then
  1204. begin
  1205. GetRecordOffsetSize:=false;
  1206. exit;
  1207. end;
  1208. st:=nil;
  1209. case sym.typ of
  1210. fieldvarsym :
  1211. with Tfieldvarsym(sym) do
  1212. begin
  1213. inc(Offset,fieldoffset);
  1214. size:=getsize;
  1215. case vardef.typ of
  1216. arraydef :
  1217. begin
  1218. { for arrays try to get the element size, take care of
  1219. multiple indexes }
  1220. harrdef:=tarraydef(vardef);
  1221. while assigned(harrdef.elementdef) and
  1222. (harrdef.elementdef.typ=arraydef) do
  1223. harrdef:=tarraydef(harrdef.elementdef);
  1224. if not is_packed_array(harrdef) then
  1225. size:=harrdef.elesize
  1226. else
  1227. begin
  1228. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1229. Message(asmr_e_packed_element);
  1230. size := (harrdef.elepackedbitsize + 7) div 8;
  1231. end;
  1232. end;
  1233. recorddef :
  1234. st:=trecorddef(vardef).symtable;
  1235. objectdef :
  1236. st:=tobjectdef(vardef).symtable;
  1237. end;
  1238. end;
  1239. procsym:
  1240. begin
  1241. st:=nil;
  1242. if Tprocsym(sym).ProcdefList.Count>1 then
  1243. Message(asmr_w_calling_overload_func);
  1244. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1245. if (not needvmtofs) then
  1246. begin
  1247. mangledname:=procdef.mangledname;
  1248. end
  1249. else
  1250. begin
  1251. { can only get the vmtoffset of virtual methods }
  1252. if not(po_virtualmethod in procdef.procoptions) then
  1253. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1254. else
  1255. begin
  1256. { size = sizeof(target_system_pointer) }
  1257. size:=sizeof(aint);
  1258. offset:=procdef._class.vmtmethodoffset(procdef.extnumber)
  1259. end;
  1260. end;
  1261. { if something comes after the procsym, it's invalid assembler syntax }
  1262. GetRecordOffsetSize:=(s='');
  1263. exit;
  1264. end;
  1265. end;
  1266. end;
  1267. { Support Field.Type as typecasting }
  1268. if (st=nil) and (s<>'') then
  1269. begin
  1270. asmsearchsym(s,sym,srsymtable);
  1271. if assigned(sym) and (sym.typ=typesym) then
  1272. begin
  1273. size:=ttypesym(sym).typedef.size;
  1274. s:=''
  1275. end;
  1276. end;
  1277. GetRecordOffsetSize:=(s='');
  1278. end;
  1279. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1280. var
  1281. sym : tsym;
  1282. srsymtable : TSymtable;
  1283. hs : string;
  1284. Begin
  1285. hl:=nil;
  1286. SearchLabel:=false;
  1287. { Check for pascal labels, which are case insensetive }
  1288. hs:=upper(s);
  1289. asmsearchsym(hs,sym,srsymtable);
  1290. if sym=nil then
  1291. exit;
  1292. case sym.typ of
  1293. labelsym :
  1294. begin
  1295. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1296. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1297. hl:=tlabelsym(sym).asmblocklabel;
  1298. if emit then
  1299. tlabelsym(sym).defined:=true
  1300. else
  1301. tlabelsym(sym).used:=true;
  1302. SearchLabel:=true;
  1303. end;
  1304. end;
  1305. end;
  1306. {*************************************************************************}
  1307. { Instruction Generation Utilities }
  1308. {*************************************************************************}
  1309. Procedure ConcatString(p : TAsmList;s:string);
  1310. {*********************************************************************}
  1311. { PROCEDURE ConcatString(s:string); }
  1312. { Description: This routine adds the character chain pointed to in }
  1313. { s to the instruction linked list. }
  1314. {*********************************************************************}
  1315. Var
  1316. pc: PChar;
  1317. Begin
  1318. getmem(pc,length(s)+1);
  1319. p.concat(Tai_string.Create_pchar(strpcopy(pc,s),length(s)));
  1320. end;
  1321. Procedure ConcatPasString(p : TAsmList;s:string);
  1322. {*********************************************************************}
  1323. { PROCEDURE ConcatPasString(s:string); }
  1324. { Description: This routine adds the character chain pointed to in }
  1325. { s to the instruction linked list, contrary to ConcatString it }
  1326. { uses a pascal style string, so it conserves null characters. }
  1327. {*********************************************************************}
  1328. Begin
  1329. p.concat(Tai_string.Create(s));
  1330. end;
  1331. Procedure ConcatConstant(p: TAsmList; value: aint; constsize:byte);
  1332. {*********************************************************************}
  1333. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1334. { Description: This routine adds the value constant to the current }
  1335. { instruction linked list. }
  1336. { maxvalue -> indicates the size of the data to initialize: }
  1337. { $ff -> create a byte node. }
  1338. { $ffff -> create a word node. }
  1339. { $ffffffff -> create a dword node. }
  1340. {*********************************************************************}
  1341. var
  1342. rangelo,rangehi : int64;
  1343. Begin
  1344. case constsize of
  1345. 1 :
  1346. begin
  1347. p.concat(Tai_const.Create_8bit(byte(value)));
  1348. rangelo:=low(shortint);
  1349. rangehi:=high(byte);
  1350. end;
  1351. 2 :
  1352. begin
  1353. p.concat(Tai_const.Create_16bit(word(value)));
  1354. rangelo:=low(smallint);
  1355. rangehi:=high(word);
  1356. end;
  1357. 4 :
  1358. begin
  1359. p.concat(Tai_const.Create_32bit(longint(value)));
  1360. rangelo:=low(longint);
  1361. rangehi:=high(cardinal);
  1362. end;
  1363. 8 :
  1364. begin
  1365. p.concat(Tai_const.Create_64bit(int64(value)));
  1366. rangelo:=0;
  1367. rangehi:=0;
  1368. end;
  1369. else
  1370. internalerror(200405011);
  1371. end;
  1372. { check for out of bounds }
  1373. if (rangelo<>0) and
  1374. ((value>rangehi) or (value<rangelo)) then
  1375. Message(asmr_e_constant_out_of_bounds);
  1376. end;
  1377. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  1378. begin
  1379. p.concat(Tai_const.Createname(sym,l));
  1380. end;
  1381. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1382. {***********************************************************************}
  1383. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1384. { Description: This routine adds the value constant to the current }
  1385. { instruction linked list. }
  1386. { real_typ -> indicates the type of the real data to initialize: }
  1387. { s32real -> create a single node. }
  1388. { s64real -> create a double node. }
  1389. { s80real -> create an extended node. }
  1390. { s64bit -> create a comp node. }
  1391. { f32bit -> create a fixed node. (not used normally) }
  1392. {***********************************************************************}
  1393. Begin
  1394. case real_typ of
  1395. s32real : p.concat(Tai_real_32bit.Create(value));
  1396. s64real :
  1397. {$ifdef ARM}
  1398. if (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1399. not(cs_fp_emulation in current_settings.moduleswitches) then
  1400. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1401. else
  1402. {$endif ARM}
  1403. p.concat(Tai_real_64bit.Create(value));
  1404. s80real : p.concat(Tai_real_80bit.Create(value));
  1405. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1406. end;
  1407. end;
  1408. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1409. {*********************************************************************}
  1410. { PROCEDURE ConcatLabel }
  1411. { Description: This routine either emits a label or a labeled }
  1412. { instruction to the linked list of instructions. }
  1413. {*********************************************************************}
  1414. begin
  1415. p.concat(Tai_label.Create(l));
  1416. end;
  1417. procedure ConcatAlign(p:TAsmList;l:aint);
  1418. {*********************************************************************}
  1419. { PROCEDURE ConcatPublic }
  1420. { Description: This routine emits an global definition to the }
  1421. { linked list of instructions.(used by AT&T styled asm) }
  1422. {*********************************************************************}
  1423. begin
  1424. p.concat(Tai_align.Create(l));
  1425. end;
  1426. procedure ConcatPublic(p:TAsmList;const s : string);
  1427. {*********************************************************************}
  1428. { PROCEDURE ConcatPublic }
  1429. { Description: This routine emits an global definition to the }
  1430. { linked list of instructions.(used by AT&T styled asm) }
  1431. {*********************************************************************}
  1432. begin
  1433. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0));
  1434. end;
  1435. procedure ConcatLocal(p:TAsmList;const s : string);
  1436. {*********************************************************************}
  1437. { PROCEDURE ConcatLocal }
  1438. { Description: This routine emits an local definition to the }
  1439. { linked list of instructions. }
  1440. {*********************************************************************}
  1441. begin
  1442. p.concat(Tai_symbol.Createname(s,AT_LABEL,0));
  1443. end;
  1444. end.