rautils.pas 44 KB

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