[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnet/cscc/c c_ainit.tc,1.4,1.5
From: |
Rhys Weatherley <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnet/cscc/c c_ainit.tc,1.4,1.5 |
Date: |
Fri, 27 Jun 2003 06:18:22 -0400 |
Update of /cvsroot/dotgnu-pnet/pnet/cscc/c
In directory subversions:/tmp/cvs-serv22895/cscc/c
Modified Files:
c_ainit.tc
Log Message:
Finish the implementation of array and structure initializers.
Index: c_ainit.tc
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/cscc/c/c_ainit.tc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** c_ainit.tc 27 Jun 2003 06:19:54 -0000 1.4
--- c_ainit.tc 27 Jun 2003 10:18:20 -0000 1.5
***************
*** 160,163 ****
--- 160,301 ----
}
+ /*
+ * Perform semantic analysis on an initializer, defined by an iterator.
+ */
+ static void InitializerSemAnalysis(ILGenInfo *info, ILType *type,
+ ILNode
*init, ILNode_ListIter *iter)
+ {
+ ILNode *node = IterLookAhead(iter);
+ CSemValue value;
+ ILUInt32 size;
+ ILUInt32 numElems;
+ ILType *elemType;
+ ILNode_ListIter inner;
+ ILClass *classInfo;
+ ILField *field;
+
+ if(CTypeIsArray(type))
+ {
+ /* Assign to the elements of an array */
+ numElems = CTypeGetNumElems(type);
+ elemType = CTypeGetElemType(type);
+ if(node && yyisa(node, ILNode_CString) &&
+ (ILTypeIdentical(elemType, ILType_Int8) ||
+ ILTypeIdentical(elemType, ILType_UInt8)))
+ {
+ /* Assign a string to a "char" array */
+ node = ILNode_ListIter_Next(iter);
+ size = ((ILNode_CString *)node)->len;
+ if(size > numElems)
+ {
+ CCWarningOnLine(yygetfilename(node),
yygetlinenum(node),
+ _("initializer-string for array of chars is
too long"));
+ }
+ }
+ else
+ {
+ /* Assign to an ordinary array */
+ while(numElems > 0 && node != 0)
+ {
+ if(yyisa(node, ILNode_CArrayInit))
+ {
+ /* The element is specified as a nested
array init scope */
+ ILNode_ListIter_Init
+ (&inner, ((ILNode_CArrayInit
*)node)->expr);
+ InitializerSemAnalysis
+ (info, elemType,
+ ((ILNode_CArrayInit
*)node)->expr, &inner);
+ if(ILNode_ListIter_Next(&inner) != 0)
+ {
+ CCWarningOnLine
+ (yygetfilename(init),
yygetlinenum(init),
+ _("excess elements in
array initializer"));
+ }
+ ILNode_ListIter_Next(iter);
+ }
+ else
+ {
+ /* The element is specified inline
within the parent */
+ InitializerSemAnalysis(info, elemType,
init, iter);
+ }
+ node = IterLookAhead(iter);
+ --numElems;
+ }
+ }
+ }
+ else if(CTypeIsStruct(type))
+ {
+ /* Assign to the fields of a structure */
+ classInfo = ILType_ToValueType(ILTypeStripPrefixes(type));
+ field = 0;
+ while((field = (ILField *)ILClassNextMemberByKind
+ (classInfo, (ILMember *)field,
IL_META_MEMBERKIND_FIELD)) != 0)
+ {
+ if(!node)
+ {
+ break;
+ }
+ if(!ILField_IsStatic(field))
+ {
+ InitializerSemAnalysis
+ (info,
ILFieldGetTypeWithPrefixes(field), init, iter);
+ }
+ node = IterLookAhead(iter);
+ }
+ }
+ else if(CTypeIsUnion(type))
+ {
+ CCErrorOnLine(yygetfilename(init), yygetlinenum(init),
+ _("union initializers are not
supported"));
+ }
+ else
+ {
+ /* Assign a scalar value */
+ if(!node)
+ {
+ CCErrorOnLine(yygetfilename(init), yygetlinenum(init),
+ _("missing scalar initializer
value"));
+ return;
+ }
+
+ /* Perform semantic analysis on the scalar value */
+ node = ILNode_ListIter_Next(iter);
+ value = ILNode_CSemAnalysis(node, info, iter->last, 0);
+ node = *(iter->last);
+
+ /* Check that the scalar value is an r-value */
+ if(!CSemIsRValue(value))
+ {
+ if(!CSemIsError(value))
+ {
+ CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
+ "invalid rvalue in
assignment");
+ }
+ return;
+ }
+
+ /* Check that we can coerce from "value" to "type" */
+ if(ILTypeStripPrefixes(type) == ILType_Boolean &&
+ (CSemIsBoolean(value) ||
+ ILTypeStripPrefixes(CSemGetType(value)) == ILType_Boolean))
+ {
+ /* Assigning a value to a "_Bool" variable */
+ return;
+ }
+ else
+ {
+ if(!CCanCoerceValue(value, type))
+ {
+ CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
+ "incompatible types
in assignment");
+ return;
+ }
+
+ /* Insert coercion nodes to convert the rvalue
appropriately */
+ CCoerceNode(info, node, iter->last, value, type);
+ }
+ }
+ }
+
%}
***************
*** 207,210 ****
--- 345,350 ----
ILUInt32 arraySize;
ILUInt32 initSize;
+ ILNode *init;
+ ILNode_ListIter iter;
/* Perform semantic analysis on the destination expression.
***************
*** 248,256 ****
else if(yyisa(node->expr2, ILNode_CArrayInit))
{
! /* TODO */
! #if 0
! CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
! _("array/structure assignment is not
implemented yet"));
! #endif
}
else
--- 388,400 ----
else if(yyisa(node->expr2, ILNode_CArrayInit))
{
! /* Assign the contents of an array initializer to an array */
! init = ((ILNode_CArrayInit *)(node->expr2))->expr;
! ILNode_ListIter_Init(&iter, init);
! InitializerSemAnalysis(info, node->type, init, &iter);
! if(ILNode_ListIter_Next(&iter) != 0)
! {
! CCWarningOnLine(yygetfilename(node), yygetlinenum(node),
! _("excess elements in
array initializer"));
! }
}
else
***************
*** 269,274 ****
ILNode_CSemAnalysis(ILNode_CAssignStruct)
{
! /* TODO */
! return CSemValueError;
}
--- 413,454 ----
ILNode_CSemAnalysis(ILNode_CAssignStruct)
{
! CSemValue value1;
! ILNode *init;
! ILNode_ListIter iter;
!
! /* Perform semantic analysis on the destination expression.
! We expect it to be an l-value */
! value1 = ILNode_CSemAnalysis(node->expr1, info, &(node->expr1), 0);
! if(!CSemIsLValue(value1))
! {
! if(!CSemIsError(value1))
! {
! CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
! _("invalid lvalue in
assignment"));
! }
! return CSemValueError;
! }
! node->type = CSemGetType(value1);
!
! /* We must have a structure type at this point */
! if(!CTypeIsStruct(node->type))
! {
! CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
! _("lvalue is not a structure"));
! return CSemValueError;
! }
!
! /* Perform semantic analysis on the structure value */
! init = ((ILNode_CArrayInit *)(node->expr2))->expr;
! ILNode_ListIter_Init(&iter, init);
! InitializerSemAnalysis(info, node->type, init, &iter);
! if(ILNode_ListIter_Next(&iter) != 0)
! {
! CCWarningOnLine(yygetfilename(node), yygetlinenum(node),
! _("excess elements in structure
initializer"));
! }
!
! /* Structure assignment is used at the statement level of a function */
! return CSemValueDefault;
}
***************
*** 278,294 ****
/*
* Write an initializer into memory at a specific pointer location,
! * where the pointer is on the stack.
*/
! static void WriteInitializer(ILGenInfo *info, ILType *type, ILNode *node)
{
ILUInt32 size;
! if(yyisa(node, ILNode_CString))
{
! /* Write the contents of a string to a location */
! if(CTypeIsArray(type))
{
! /* Populate an array by copying a string into it */
ILNode_GenValue(node, info);
size = CTypeGetNumElems(type);
--- 458,502 ----
/*
+ * Look ahead to the next item in a list iteration.
+ */
+ static ILNode *IterLookAhead(ILNode_ListIter *iter)
+ {
+ ILNode_ListIter copy = *iter;
+ return ILNode_ListIter_Next(©);
+ }
+
+ /*
* Write an initializer into memory at a specific pointer location,
! * where the pointer is on the stack. If "field" is non-NULL, then
! * it indicates a field within a higher-level structure.
*/
! static void WriteInitializer(ILGenInfo *info, ILType *type,
! ILNode_ListIter *iter,
ILField *field)
{
+ ILNode *node = IterLookAhead(iter);
ILUInt32 size;
+ ILUInt32 numElems;
+ ILUInt32 index;
+ ILType *elemType;
+ ILClass *classInfo;
+ ILField *nextField;
+ ILNode_ListIter inner;
+ int popped;
! if(CTypeIsArray(type))
{
! /* Write the members of an array */
! numElems = CTypeGetNumElems(type);
! elemType = CTypeGetElemType(type);
! if(node && yyisa(node, ILNode_CString) &&
! (ILTypeIdentical(elemType, ILType_Int8) ||
! ILTypeIdentical(elemType, ILType_UInt8)))
{
! /* Populate a "char" array by copying a string into it
*/
! if(field)
! {
! ILGenFieldRef(info, IL_OP_LDFLDA, field);
! }
! node = ILNode_ListIter_Next(iter);
ILNode_GenValue(node, info);
size = CTypeGetNumElems(type);
***************
*** 305,320 ****
else
{
! /* Store the string directly as a pointer */
! ILNode_GenValue(node, info);
! ILGenSimple(info, IL_OP_STIND_I);
! ILGenAdjust(info, -2);
}
}
else
{
! /* Write the contents of an array or structure to a location */
! /* TODO */
! ILGenSimple(info, IL_OP_POP);
! ILGenAdjust(info, -1);
}
}
--- 513,651 ----
else
{
! /* Write ordinary members to the array */
! if(field)
! {
! ILGenFieldRef(info, IL_OP_LDFLDA, field);
! }
! popped = 0;
! index = 0;
! size = CTypeSizeAndAlign(elemType, 0);
! while(numElems > 0 && node != 0)
! {
! if(numElems > 1)
! {
! ILGenSimple(info, IL_OP_DUP);
! ILGenAdjust(info, 1);
! }
! else
! {
! popped = 1;
! }
! if(index != 0)
! {
! /* Adjust the pointer to account for
the array index */
! if(size != CTYPE_DYNAMIC)
! {
! ILGenUIntNative(info, index *
size);
! ILGenSimple(info, IL_OP_ADD);
! ILGenExtend(info, 1);
! }
! else if(index == 1)
! {
! CGenSizeOf(info, elemType);
! ILGenSimple(info, IL_OP_CONV_U);
! ILGenSimple(info, IL_OP_ADD);
! ILGenAdjust(info, -1);
! }
! else
! {
! ILGenUInt32(info, index);
! ILGenAdjust(info, 1);
! CGenSizeOf(info, elemType);
! ILGenSimple(info, IL_OP_MUL);
! ILGenSimple(info, IL_OP_CONV_U);
! ILGenSimple(info, IL_OP_ADD);
! ILGenAdjust(info, -2);
! }
! }
! if(yyisa(node, ILNode_CArrayInit))
! {
! /* The element is specified as a nested
array init scope */
! ILNode_ListIter_Init
! (&inner, ((ILNode_CArrayInit
*)node)->expr);
! WriteInitializer(info, elemType,
&inner, 0);
! ILNode_ListIter_Next(iter);
! }
! else
! {
! /* The element is specified inline
within the parent */
! WriteInitializer(info, elemType, iter,
0);
! }
! node = IterLookAhead(iter);
! --numElems;
! ++index;
! }
! if(!popped)
! {
! ILGenSimple(info, IL_OP_POP);
! ILGenAdjust(info, -1);
! }
! }
! }
! else if(CTypeIsStruct(type))
! {
! /* Write the elements of a structure */
! if(field)
! {
! ILGenFieldRef(info, IL_OP_LDFLDA, field);
! }
! classInfo = ILType_ToValueType(ILTypeStripPrefixes(type));
! field = 0;
! popped = 0;
! while((field = (ILField *)ILClassNextMemberByKind
! (classInfo, (ILMember *)field,
IL_META_MEMBERKIND_FIELD)) != 0)
! {
! if(!node)
! {
! break;
! }
! nextField = field;
! while((nextField = (ILField *)ILClassNextMemberByKind
! (classInfo, (ILMember
*)nextField,
! IL_META_MEMBERKIND_FIELD)) !=
0 &&
! ILField_IsStatic(nextField))
! {
! /* Look for the next non-static field */
! }
! if(nextField)
! {
! /* Duplicate the pointer, because we'll need it
again */
! ILGenSimple(info, IL_OP_DUP);
! ILGenAdjust(info, 1);
! }
! else
! {
! /* We are on the last field, so don't dup the
pointer */
! popped = 1;
! }
! if(!ILField_IsStatic(field))
! {
! WriteInitializer
! (info,
ILFieldGetTypeWithPrefixes(field),
! iter, field);
! }
! node = IterLookAhead(iter);
! }
! if(!popped)
! {
! ILGenSimple(info, IL_OP_POP);
! ILGenAdjust(info, -1);
}
}
else
{
! /* Write an ordinary initializer value */
! node = ILNode_ListIter_Next(iter);
! ILNode_GenValue(node, info);
! if(field)
! {
! ILGenFieldRef(info, IL_OP_STFLD, field);
! }
! else
! {
! ILGenStoreManaged(info, ILTypeToMachineType(type),
!
ILTypeStripPrefixes(type));
! }
! ILGenAdjust(info, -2);
}
}
***************
*** 327,335 ****
ILNode_GenDiscard(ILNode_CAssignArray)
{
/* Get the address of the array */
CGenAddress(info, node->expr1);
/* Write the initializer value into the array */
! WriteInitializer(info, node->type, node->expr2);
}
JavaGenDiscard(ILNode_CAssignArray)
--- 658,688 ----
ILNode_GenDiscard(ILNode_CAssignArray)
{
+ ILNode_ListIter iter;
+ ILUInt32 size;
+
/* Get the address of the array */
CGenAddress(info, node->expr1);
/* Write the initializer value into the array */
! if(yyisa(node->expr2, ILNode_CString))
! {
! ILNode_GenValue(node->expr2, info);
! size = CTypeGetNumElems(node->type);
! if(size > (((ILNode_CString *)(node->expr2))->len + 1))
! {
! size = ((ILNode_CString *)(node->expr2))->len + 1;
! }
! ILGenUInt32(info, size);
! ILGenAdjust(info, 1);
! ILGenByteInsn(info, IL_OP_PREFIX + IL_PREFIX_OP_UNALIGNED, 1);
! ILGenSimple(info, IL_OP_PREFIX + IL_PREFIX_OP_CPBLK);
! ILGenAdjust(info, -3);
! }
! else
! {
! ILNode_ListIter_Init
! (&iter, ((ILNode_CArrayInit *)(node->expr2))->expr);
! WriteInitializer(info, node->type, &iter, 0);
! }
}
JavaGenDiscard(ILNode_CAssignArray)
***************
*** 343,347 ****
ILNode_GenDiscard(ILNode_CAssignStruct)
{
! /* TODO */
}
JavaGenDiscard(ILNode_CAssignStruct)
--- 696,708 ----
ILNode_GenDiscard(ILNode_CAssignStruct)
{
! ILNode_ListIter iter;
!
! /* Get the address of the structure */
! CGenAddress(info, node->expr1);
!
! /* Write the initializer value into the structure */
! ILNode_ListIter_Init
! (&iter, ((ILNode_CArrayInit *)(node->expr2))->expr);
! WriteInitializer(info, node->type, &iter, 0);
}
JavaGenDiscard(ILNode_CAssignStruct)
***************
*** 349,351 ****
/* Nothing to do here */
}
-
--- 710,711 ----
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnet/cscc/c c_ainit.tc,1.4,1.5,
Rhys Weatherley <address@hidden> <=