rautils.pas 47 KB

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