From 5e20e02d064a462b4d7c619d8aa447c9f8a223d3 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 19 Jun 2022 17:55:08 -0500 Subject: [PATCH] Add a function to make a pointer type. This allows us to refactor out code that was doing this in several places. --- Expression.pas | 77 ++++++++++---------------------------------------- Parser.pas | 11 ++------ Symbol.pas | 34 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 71 deletions(-) diff --git a/Expression.pas b/Expression.pas index c735638..1b9e479 100644 --- a/Expression.pas +++ b/Expression.pas @@ -2870,13 +2870,7 @@ var private: Gen1Name(pc_lao, 0, tname); otherwise: ; end; {case} - eType := pointer(Malloc(sizeof(typeRecord))); - eType^.size := cgPointerSize; - eType^.saveDisp := 0; - eType^.qualifiers := []; - eType^.kind := pointerType; - eType^.pType := iType; - expressionType := eType; + expressionType := MakePointerTo(iType); end; {with} end {if} else if tree^.token.kind = compoundliteral then begin @@ -2914,13 +2908,7 @@ var GenLdcLong(size); Gen0(pc_adl); end; {else} - eType := pointer(Malloc(sizeof(typeRecord))); - eType^.size := cgPointerSize; - eType^.saveDisp := 0; - eType^.qualifiers := []; - eType^.kind := pointerType; - eType^.pType := expressionType; - expressionType := eType; + expressionType := MakePointerTo(expressionType); end {if} else Error(79); @@ -2930,15 +2918,8 @@ var {load the address of a field of a record} LoadAddress(tree^.left); expressionType := tree^.castType; - if expressionType^.kind <> arrayType then begin - eType := pointer(Malloc(sizeof(typeRecord))); - eType^.size := cgPointerSize; - eType^.saveDisp := 0; - eType^.qualifiers := []; - eType^.kind := pointerType; - eType^.pType := expressionType; - expressionType := eType; - end; {if} + if expressionType^.kind <> arrayType then + expressionType := MakePointerTo(expressionType); end {else if} else if ExpressionKind(tree) in [arrayType,pointerType,structType,unionType] @@ -4233,15 +4214,8 @@ case tree^.token.kind of else lType := lType^.pType; ChangePointer(pc_adl, lType^.size, et); - if expressionType^.kind = arrayType then begin - tType := pointer(Malloc(sizeof(typeRecord))); - tType^.size := cgPointerSize; - tType^.saveDisp := 0; - tType^.qualifiers := []; - tType^.kind := pointerType; - tType^.pType := expressionType^.aType; - expressionType := tType; - end; {if} + if expressionType^.kind = arrayType then + expressionType := MakePointerTo(expressionType^.aType); end {if} else begin @@ -4289,15 +4263,8 @@ case tree^.token.kind of {subtract a scalar from a pointer} ChangePointer(pc_sbl, size, UsualUnaryConversions); expressionType := lType; - if expressionType^.kind = arrayType then begin - tType := pointer(Malloc(sizeof(typeRecord))); - tType^.size := cgPointerSize; - tType^.saveDisp := 0; - tType^.qualifiers := []; - tType^.kind := pointerType; - tType^.pType := expressionType^.aType; - expressionType := tType; - end; {if} + if expressionType^.kind = arrayType then + expressionType := MakePointerTo(expressionType^.aType); end {if} else begin @@ -4517,28 +4484,14 @@ case tree^.token.kind of if tree^.left^.token.kind = stringconst then begin {build pointer-to-array type for address of string constant} tType := pointer(Malloc(sizeof(typeRecord))); - with tType^ do begin - size := cgPointerSize; - saveDisp := 0; - qualifiers := []; - kind := pointerType; - pType := pointer(Malloc(sizeof(typeRecord))); - pType^ := StringType(tree^.left^.token.prefix)^; - pType^.size := tree^.left^.token.sval^.length; - pType^.saveDisp := 0; - pType^.elements := pType^.size div pType^.aType^.size; - end; {with} - expressionType := tType; - end {if} - else if expressionType^.kind = arrayType then begin - tType := pointer(Malloc(sizeof(typeRecord))); - tType^.size := cgPointerSize; + tType^ := StringType(tree^.left^.token.prefix)^; + tType^.size := tree^.left^.token.sval^.length; tType^.saveDisp := 0; - tType^.qualifiers := []; - tType^.kind := pointerType; - tType^.pType := expressionType^.aType; - expressionType := tType; - end; {else if} + tType^.elements := tType^.size div tType^.aType^.size; + expressionType := MakePointerTo(tType); + end {if} + else if expressionType^.kind = arrayType then + expressionType := MakePointerTo(expressionType^.aType); end; {case uand} uasterisk: begin {unary * (indirection)} diff --git a/Parser.pas b/Parser.pas index 901b75b..e9291fd 100644 --- a/Parser.pas +++ b/Parser.pas @@ -1698,15 +1698,8 @@ while typeStack <> nil do begin {reverse the type stack} end; {while} if doingParameters then {adjust array parameters to pointers} - if tPtr^.kind = arrayType then begin - tPtr2 := pointer(Calloc(sizeof(typeRecord))); - tPtr2^.size := cgPointerSize; - {tPtr2^.qualifiers := [];} - {tPtr2^.saveDisp := 0;} - tPtr2^.kind := pointerType; - tPtr2^.pType := tPtr^.aType; - tPtr := tPtr2; - end; {if} + if tPtr^.kind = arrayType then + tPtr := MakePointerTo(tPtr^.aType); if checkParms then begin {check for parameter type conflicts} with variable^ do begin diff --git a/Symbol.pas b/Symbol.pas index e2a90b9..d5fccf6 100644 --- a/Symbol.pas +++ b/Symbol.pas @@ -182,6 +182,16 @@ function MakePascalType (origType: typePtr): typePtr; { returns: pointer to the pascal-qualified type } +function MakePointerTo (pType: typePtr): typePtr; + +{ make a pointer type } +{ } +{ parameters: } +{ pType - the type pointed to } +{ } +{ returns: the pointer type } + + function MakeQualifiedType (origType: typePtr; qualifiers: typeQualifierSet): typePtr; @@ -1749,6 +1759,30 @@ while tp <> nil do end; {MakePascalType} +function MakePointerTo {pType: typePtr): typePtr}; + +{ make a pointer type } +{ } +{ parameters: } +{ pType - the type pointed to } +{ } +{ returns: the pointer type } + +var + tp: typePtr; {the pointer type} + + +begin {MakePointerTo} +tp := pointer(Malloc(sizeof(typeRecord))); +tp^.size := cgPointerSize; +tp^.saveDisp := 0; +tp^.qualifiers := []; +tp^.kind := pointerType; +tp^.pType := pType; +MakePointerTo := tp; +end; {MakePointerTo} + + function MakeQualifiedType {origType: typePtr; qualifiers: typeQualifierSet): typePtr};