Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a44dd51
Reverted null function wrapper changes.
woollybah Jun 9, 2024
b74caed
Merge pull request #667 from bmx-ng/task/null-funcwrapper-revert
woollybah Jun 9, 2024
6ac0e70
Add struct-specific array creation and slicing.
woollybah Nov 2, 2024
5f9cd02
Merge pull request #671 from bmx-ng/task/struct-arrays-etc
woollybah Nov 3, 2024
f8721cf
Fixed incorrect cast on calls to ObjectNew.
woollybah Nov 4, 2024
a7d4c0a
Merge pull request #672 from bmx-ng/task/objectnew-cast
woollybah Nov 4, 2024
91e8675
Updated for zlib changes to use ULongInt
woollybah Nov 7, 2024
ebf573d
Merge pull request #673 from bmx-ng/task/zlib-ulongint
woollybah Nov 7, 2024
a01e1cc
Added new casts where appropriate
woollybah Nov 7, 2024
8de073f
Merge pull request #674 from bmx-ng/task/casts-and-tostring
woollybah Nov 7, 2024
a537785
Compare and SendMessage protos now reflect implementations.
woollybah Nov 8, 2024
d80c374
Merge pull request #675 from bmx-ng/task/compare-sendmessage-proto
woollybah Nov 8, 2024
71cec54
Add own copy of compatible zlib.
woollybah Nov 10, 2024
67d0408
Merge pull request #676 from bmx-ng/task/use-own-zlib
woollybah Nov 10, 2024
a46325f
Also cast function pointers.
woollybah Nov 10, 2024
2c3ac9d
Merge pull request #677 from bmx-ng/task/fptr-cast
woollybah Nov 10, 2024
093bbe9
Added more casts for code generation.
woollybah Feb 7, 2025
93d2c79
Merge pull request #687 from bmx-ng/task/more-casts
woollybah Feb 7, 2025
6281ec1
Ensure global function pointer init is cast.
woollybah Feb 8, 2025
b403852
Merge pull request #688 from bmx-ng/task/global-init-fptr-cast
woollybah Feb 8, 2025
51fbf4a
Merge branch 'master' into bcc_reflect
GWRon Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bcc.bmx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
' Copyright (c) 2013-2023 Bruce A Henderson
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
Expand Down
20 changes: 8 additions & 12 deletions config.bmx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
' Copyright (c) 2013-2023 Bruce A Henderson
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
Expand Down Expand Up @@ -26,7 +26,6 @@ SuperStrict
Import BRL.LinkedList
Import BRL.Map
Import BRL.FileSystem
Import Pub.zlib
Import BRL.Math

Import "options.bmx"
Expand All @@ -35,6 +34,7 @@ Import "base64.bmx"
Import "enums.c"
Import "hash.c"
Import "math.c"
Import "zlib/zlib.bmx"

' debugging help
Const DEBUG:Int = False
Expand Down Expand Up @@ -623,15 +623,13 @@ Type TTemplateRecord
?Not bmxng
Local slen:Int = strlen_(s)
?bmxng
Local slen:UInt = strlen_(s)
Local slen:ULongInt = strlen_(s)
?

?Not bmxng
Local dlen:Int = slen + 12
?bmxng And (win32 Or ptr32)
Local dlen:UInt = slen + 12
?bmxng And ptr64 And Not win32
Local dlen:ULong = slen + 12
?bmxng
Local dlen:ULongInt = slen + 12
?
Local data:Byte[dlen]

Expand All @@ -651,18 +649,16 @@ Type TTemplateRecord

?Not bmxng
Local dlen:Int = size + 1
?bmxng And (win32 Or ptr32)
Local dlen:UInt = size + 1
?bmxng And ptr64 And Not win32
Local dlen:ULong = size + 1
?bmxng
Local dlen:ULongInt = size + 1
?
Local data:Byte[dlen]

Local s:Byte[] = TBase64.Decode(source)
?Not bmxng
uncompress(data, dlen, s, s.length)
?bmxng
uncompress(data, dlen, s, UInt(s.length))
uncompress(data, dlen, s, ULongInt(s.length))
?
Return New TTemplateRecord.Create(start, file, String.FromUTF8String(data))
End Function
Expand Down
114 changes: 87 additions & 27 deletions ctranslator.bmx
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ Type TCTranslator Extends TTranslator
End If
End If
End If
If TFunctionPtrType( ty) Return "(&brl_blitz_NullFunctionError)" ' todo ??
If TFunctionPtrType( ty) Return Bra(TransType(ty, "")) + "(&brl_blitz_NullFunctionError)"
If TEnumType( ty ) Then
If TEnumType( ty ).decl.isFlags Then
Return "0"
Expand All @@ -609,14 +609,18 @@ Type TCTranslator Extends TTranslator
InternalErr "TCTranslator.TransValue"
End Method

Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null )
Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null, objectNew:Int = False )
'If decl.ident="AddS" DebugStop

Local t$
If objParam And (decl.IsMethod() Or decl.isCtor()) And ((Not decl.IsExtern()) Or (decl.IsExtern() And TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct())) Then
' object cast to match param type
If TClassDecl(decl.scope) Then
t :+ Bra(TransObject(TClassDecl(decl.scope).GetLatestFuncDecl(decl).scope, TClassDecl(decl.scope).IsStruct()))
If objectNew Then
t :+ Bra("BBClass *")
Else
If TClassDecl(decl.scope) Then
t :+ Bra(TransObject(TClassDecl(decl.scope).GetLatestFuncDecl(decl).scope, TClassDecl(decl.scope).IsStruct()))
End If
End If
t:+ objParam
End If
Expand All @@ -629,7 +633,7 @@ Type TCTranslator Extends TTranslator
Local arg:TExpr = args[i]

' object cast to match param type
If TObjectType(ty) And Not TObjectType(ty).classDecl.IsStruct() Then
If TObjectType(ty) And Not TObjectType(ty).classDecl.IsStruct() And Not argDecl.castTo Then
Local fdecl:TFuncDecl = decl
If TClassDecl(decl.scope) Then
fdecl = TClassDecl(decl.scope).GetOriginalFuncDecl(decl)
Expand Down Expand Up @@ -707,6 +711,12 @@ Type TCTranslator Extends TTranslator
Continue
End If

If TCastExpr(arg) And TVarExpr(TCastExpr(arg).expr) Then
t:+ varRef
t:+ TransCast(TFunctionPtrType(ty)) + Bra(arg.Trans())
Continue
End If

' Object -> Byte Ptr
If IsPointerType(ty, TType.T_BYTE) And TObjectType(arg.exprType) Then
t:+ varRef + Bra("bbObjectToFieldOffset" + Bra("(BBObject*)" + arg.Trans()))
Expand Down Expand Up @@ -781,6 +791,30 @@ t:+"NULLNULLNULL"
Return Bra(t)
End Method

' translate to C cast
Method TransCast:String(funcPtr:TFunctionPtrType)
If Not funcPtr Then
Return ""
End If

Local s:String = "("
s:+ TransType(funcPtr.func.retType, "")
s:+ "(*)("

For Local i:Int=0 Until funcPtr.func.argDecls.Length
If i Then
s:+ ","
End If

s:+ TransType(funcPtr.func.argDecls[i].ty, "")

Next

s:+ ")"
s:+ ")"
Return s
End Method

Method TransArgsTypes$( args:TExpr[],declArgTypes:TType[])
Local t$
For Local i:Int=0 Until args.Length
Expand Down Expand Up @@ -865,7 +899,7 @@ t:+"NULLNULLNULL"
Local initTrans:String
If outputInit Then
Local cast:String
If TObjectType(decl.ty) And Not TObjectType(decl.ty).classDecl.IsStruct() Then
If (TObjectType(decl.ty) And Not TObjectType(decl.ty).classDecl.IsStruct()) Or TFunctionPtrType(decl.ty) Then
cast = Bra(TransType(decl.ty, ""))
End If

Expand Down Expand Up @@ -1017,9 +1051,9 @@ t:+"NULLNULLNULL"
If gdecl.init Then
If TFunctionPtrType(gdecl.ty) Then
If TInvokeExpr(gdecl.init) And Not TInvokeExpr(gdecl.init).invokedWithBraces Then
glob :+ TInvokeExpr(gdecl.init).decl.munged
glob :+ TransCast(TFunctionPtrType(gdecl.ty)) + TInvokeExpr(gdecl.init).decl.munged
Else
glob :+ gdecl.init.Trans()
glob :+ TransCast(TFunctionPtrType(gdecl.ty)) + gdecl.init.Trans()
End If
Else If Not TConstExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
' for non const, we need to add an initialiser
Expand Down Expand Up @@ -1890,12 +1924,12 @@ t:+"NULLNULLNULL"
End If
Else
If ClassHasObjectField(expr.classDecl) And Not expr.classDecl.IsStruct() Then
t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged )
t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged, True )
Else
If expr.classDecl.IsStruct() Then
t = ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor)
Else
t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged)
t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged, True)
End If
End If
End If
Expand All @@ -1909,10 +1943,7 @@ t:+"NULLNULLNULL"

If expr.expr.length = 1 Then
If TObjectType(expr.ty) And TObjectType(expr.ty).classdecl.IsStruct() And Not IsPointerType(expr.ty) Then
Return "bbArrayNew1DStruct" + Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans() + ", sizeof" + ..
Bra(TransObject(TObjectType(expr.ty).classdecl)) + ", _" + TObjectType(expr.ty).classdecl.munged + "_New")
Else If TEnumType(expr.ty) Then
Return "bbArrayNew1DEnum" + Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans() + ", " + TEnumType(expr.ty).decl.munged + "_BBEnum_impl")
Return "bbArrayNew1DStruct_" + TObjectType(expr.ty).classdecl.munged + Bra(expr.expr[0].Trans())
Else
Return "bbArrayNew1D" + Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans())
End If
Expand Down Expand Up @@ -2010,7 +2041,7 @@ t:+"NULLNULLNULL"

If TArrayType(src) Then
If TArrayType(src).isStatic Then
Return Bra("&" + Bra(t))
Return Bra(t)
Else
Return Bra(Bra(TransType(dst, "")) + "BBARRAYDATA(" + t + ",1)")
End If
Expand Down Expand Up @@ -2466,12 +2497,12 @@ t:+"NULLNULLNULL"
' if we are casting to Object[], don't actually cast.
Return Bra(t)
Else
Return "bbArrayCastFromObject" + Bra(t + "," + TransArrayType(TArrayType( dst ).elemType))
Return "bbArrayCastFromObject" + Bra("(BBOBJECT)" + t + "," + TransArrayType(TArrayType( dst ).elemType))
End If
End If

If TObjectType( src) And (TObjectType( src ).classDecl.ident = "___Array" Or TObjectType( src ).classDecl.ident = "Object") Then
Return "bbArrayCastFromObject" + Bra(t + "," + TransArrayType(TArrayType( dst ).elemType))
Return "bbArrayCastFromObject" + Bra("(BBOBJECT)" + t + "," + TransArrayType(TArrayType( dst ).elemType))
End If
Else If TObjectType( dst )
'If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
Expand Down Expand Up @@ -2761,8 +2792,7 @@ t:+"NULLNULLNULL"
If TArrayType(expr.exprType) Then
Local ty:TType = TArrayType(expr.exprType).elemType
If TObjectType(ty) And TObjectType(ty).classDecl.IsStruct() Then
Return "bbArraySliceStruct" + Bra(TransArrayType(ty) + "," + t_expr + "," + t_args + ", sizeof" + ..
Bra(TransObject(TObjectType(ty).classdecl)) + ", _" + TObjectType(ty).classdecl.munged + "_New")
Return "bbArraySliceStruct_" + TObjectType(ty).classdecl.munged + Bra( t_expr + "," + t_args )
Else
Return "bbArraySlice" + Bra(TransArrayType(ty) + "," + t_expr + "," + t_args)
End If
Expand Down Expand Up @@ -3219,11 +3249,18 @@ t:+"NULLNULLNULL"
s :+ lhs+TransAssignOp( stmt.op )+cast+rhs
End If
Else If (TFunctionPtrType(stmt.lhs.exprType) <> Null Or IsPointerType(stmt.lhs.exprType, TType.T_BYTE)) And TInvokeExpr(stmt.rhs) And Not TInvokeExpr(stmt.rhs).invokedWithBraces Then

If Not cast And TFunctionPtrType(stmt.lhs.exprType) Then
Local fp:TFunctionPtrType = TFunctionPtrType(stmt.lhs.exprType)
If fp.func.cdets Then
cast = fp.func.cdets.TransCast()
End If
End If
rhs = TInvokeExpr(stmt.rhs).decl.munged
s :+ lhs+TransAssignOp( stmt.op )+cast+rhs
Else If TObjectType(stmt.lhs.exprType) And TObjectType(stmt.lhs.exprType).classDecl.IsStruct() And rhs = "&bbNullObject" Then
s :+ lhs+TransAssignOp( stmt.op )+cast+"{}"
Else
Else
s :+ lhs+TransAssignOp( stmt.op )+cast+rhs
End If

Expand Down Expand Up @@ -4080,9 +4117,21 @@ End Rem
Emit "unsigned int instance_size;"
Emit "void (*ctor)( BBOBJECT o );"
Emit "void (*dtor)( BBOBJECT o );"
Emit "BBSTRING (*ToString)( BBOBJECT x );"
Emit "int (*Compare)( BBOBJECT x,BBOBJECT y );"
Emit "BBOBJECT (*SendMessage)( BBOBJECT o,BBOBJECT m,BBOBJECT s );"
If classHierarchyGetFunction(classDecl, "ToString") Then
Emit "BBSTRING (*ToString)( struct " + classidForFunction(classDecl, "ToString") + "_obj* x );"
Else
Emit "BBSTRING (*ToString)( BBOBJECT x );"
End If
If classHierarchyGetFunction(classDecl, "Compare") Then
Emit "BBINT (*Compare)( struct " + classidForFunction(classDecl, "Compare") + "_obj* x, BBOBJECT y );"
Else
Emit "int (*Compare)( BBOBJECT x,BBOBJECT y );"
End If
If classHierarchyGetFunction(classDecl, "SendMessage") Then
Emit "BBOBJECT (*SendMessage)( struct " + classidForFunction(classDecl, "SendMessage") + "_obj* x, BBOBJECT m, BBOBJECT s );"
Else
Emit "BBOBJECT (*SendMessage)( BBOBJECT o,BBOBJECT m,BBOBJECT s );"
End If
Emit "BBINTERFACETABLE itable;"
Emit "void* extra;"
Emit "unsigned int obj_size;"
Expand Down Expand Up @@ -4261,6 +4310,10 @@ End Rem

EmitClassGlobalsProto(classDecl);

' struct arrays
Emit "BBArray *bbArrayNew1DStruct_" + classDecl.munged + "(int length);"
Emit "BBArray *bbArraySliceStruct_" + classDecl.munged + "(BBArray *inarr, int beg, int end);"

End Method

Method classGetFunction:TDecl(classDecl:TClassDecl, func:String)
Expand Down Expand Up @@ -4790,6 +4843,11 @@ End Rem

reserved = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()

If (classDecl.attrs & CLASS_STRUCT) Then
Emit "BBARRAYNEW1DSTRUCT_FUNC" + Bra(classid + "," + classid + ", _" + classid + "_New" + ", " + EnQuote("@" + classDecl.ident))
Emit "BBARRAYSLICESTRUCT_FUNC" + Bra(classid + "," + classid + ", _" + classid + "_New" + ", " + EnQuote("@" + classDecl.ident))
End If

End If

' cif defs
Expand Down Expand Up @@ -4887,6 +4945,8 @@ End Rem
For Local func:TFuncDecl = EachIn ifc.GetImplementedFuncs()

If func.IsMethod() Then

Local cast:String = Bra( func.munged + "_m" )

For Local f:TFuncDecl = EachIn fdecls

Expand All @@ -4901,7 +4961,7 @@ End Rem

If Not dups.ValueForKey(id) Then

Emit "_" + f.munged + ","
Emit cast + "_" + f.munged + ","

dups.Insert(id, "")
End If
Expand Down Expand Up @@ -4943,19 +5003,19 @@ End Rem
End If

If classHierarchyGetFunction(classDecl, "ToString") Then
Emit "(BBSTRING (*)(BBOBJECT))_" + classidForFunction(classDecl, "ToString") + "_ToString,"
Emit "_" + classidForFunction(classDecl, "ToString") + "_ToString,"
Else
Emit "bbObjectToString,"
End If

If classHierarchyGetFunction(classDecl, "Compare") Then
Emit "(int (*)(BBOBJECT, BBOBJECT))_" + classidForFunction(classDecl, "Compare") + "_Compare,"
Emit "_" + classidForFunction(classDecl, "Compare") + "_Compare,"
Else
Emit "bbObjectCompare,"
End If

If classHierarchyGetFunction(classDecl, "SendMessage") Then
Emit "(BBOBJECT (*)(BBOBJECT, BBOBJECT, BBOBJECT))_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage,"
Emit "_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage,"
Else
Emit "bbObjectSendMessage,"
End If
Expand Down
29 changes: 28 additions & 1 deletion decl.bmx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
' Copyright (c) 2013-2023 Bruce A Henderson
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
Expand Down Expand Up @@ -1930,6 +1930,7 @@ Type TFuncDecl Extends TBlockDecl

Field castTo:String
Field noCastGen:Int
Field cdets:TCastDets

Field maybeFunctionPtr:Int

Expand Down Expand Up @@ -4486,3 +4487,29 @@ Type TGenProcessor Abstract
End Method

End Type

Type TCastDets

Field name:String
Field retType:String
Field noGen:Int
Field args:String[0]
Field api:String

Method TransCast:String()
Local s:String = "(" + retType + " (*)"

s :+ "("
For Local i:Int = 0 Until args.length
s :+ args[i]
If i < args.length - 1 Then
s :+ ","
End If
Next
s :+ ")"

s :+ ")"
Return s
End Method

End Type
2 changes: 1 addition & 1 deletion expr.bmx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
' Copyright (c) 2013-2023 Bruce A Henderson
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
Expand Down
2 changes: 1 addition & 1 deletion iparser.bmx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
' Copyright (c) 2013-2023 Bruce A Henderson
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
Expand Down
Loading