rautils.pas 45 KB

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