mirror of
https://github.com/byteworksinc/ORCA-C.git
synced 2025-02-17 17:30:29 +00:00
1 line
6.5 KiB
ObjectPascal
1 line
6.5 KiB
ObjectPascal
|
{$optimize 7}
{---------------------------------------------------------------}
{ }
{ DAG Creation }
{ }
{ Places intermediate codes into DAGs and trees. }
{ }
{---------------------------------------------------------------}
unit DAG;
interface
{$segment 'cg'}
{$LibPrefix '0/obj/'}
uses CCommon, CGI, CGC, Gen;
{---------------------------------------------------------------}
procedure DAG (code: icptr);
{ place an op code in a DAG or tree }
{ }
{ parameters: }
{ code - opcode }
{---------------------------------------------------------------}
implementation
var
maxLoc: integer; {max local label number used by compiler}
{-- External unsigned math routines; imported from Expression.pas --}
function udiv (x,y: longint): longint; extern;
function umod (x,y: longint): longint; extern;
function umul (x,y: longint): longint; extern;
{---------------------------------------------------------------}
procedure DAG {code: icptr};
{ place an op code in a DAG or tree }
{ }
{ parameters: }
{ code - opcode }
var
temp: icptr; {temp node}
procedure Generate;
{ generate the code for the current procedure }
var
op: icptr; {temp opcode pointers}
procedure BasicBlocks;
{ Break the code up into basic blocks }
var
blast: blockPtr; {last block pointer}
bp: blockPtr; {current block pointer}
cb: icptr; {last code in block pointer}
cp: icptr; {current code pointer}
begin {BasicBlocks}
cp := DAGhead;
DAGblocks := nil;
if cp <> nil then begin
bp := pointer(Calloc(sizeof(block)));
DAGblocks := bp;
blast := bp;
bp^.code := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
while cp <> nil do
{labels start a new block}
if cp^.opcode = dc_lab then begin
Spin;
bp := pointer(Calloc(sizeof(block)));
bp^.last := blast;
blast^.next := bp;
blast := bp;
bp^.code := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
end {if}
{conditionals are followed by a new block}
else if cp^.opcode in [pc_fjp, pc_tjp, pc_ujp, pc_ret, pc_xjp] then
begin
Spin;
while cp^.next^.opcode = pc_add do begin
cb^.next := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
end; {while}
cb^.next := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
bp := pointer(Calloc(sizeof(block)));
bp^.last := blast;
blast^.next := bp;
blast := bp;
bp^.code := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
end {else if}
else begin {all other statements get added to a block}
cb^.next := cp;
cb := cp;
cp := cp^.next;
cb^.next := nil;
end; {else}
end; {if}
end; {BasicBlocks}
begin {Generate}
BasicBlocks; {build the basic blocks}
Gen(DAGblocks); {generate native code}
DAGhead := nil; {reset the DAG pointers}
end; {Generate}
procedure Push (code: icptr);
{ place a node on the operation stack }
{ }
{ parameters: }
{ code - node }
begin {Push}
code^.next := DAGhead;
DAGhead := code;
end; {Push}
|