Ver Fonte

Mx2cc cleanups.

Mark Sibly há 7 anos atrás
pai
commit
ad23da58c7

+ 5 - 10
src/mx2cc/builder.monkey2

@@ -141,7 +141,7 @@ Class BuilderInstance
 	
 	Method Parse()
 	
-		If opts.verbose=0 Print "Parsing..."
+		If opts.verbose>=0 Print "Parsing..."
 		
 		Local name:=StripDir( StripExt( opts.mainSource ) )
 
@@ -315,7 +315,7 @@ Class BuilderInstance
 
 	Method Semant()
 	
-		If opts.verbose=0 Print "Semanting..."
+		If opts.verbose>=0 Print "Semanting..."
 		
 		SortModules()
 		
@@ -323,12 +323,8 @@ Class BuilderInstance
 		
 			Local module:=modules[i]
 			
-'			Print ""		
-'			Print "Semanting module:"+module.srcPath
-'			Print ""
-			
 			For Local fdecl:=Eachin module.fileDecls
-			
+
 				Local fscope:=New FileScope( fdecl )
 				
 				module.fileScopes.Push( fscope )
@@ -353,10 +349,9 @@ Class BuilderInstance
 			
 			For Local fscope:=Eachin module.fileScopes
 			
-				If opts.verbose>0 Print "Semanting "+fscope.fdecl.path
+				If opts.verbose=2 Print "Semanting "+fscope.fdecl.path
 			
 				fscope.Semant()
-				
 			Next
 			
 			Repeat
@@ -492,7 +487,7 @@ Class BuilderInstance
 	
 	Method Translate()
 	
-		If opts.verbose=0 Print "Translating..."
+		If opts.verbose>=0 Print "Translating..."
 		
 		Local module:=mainModule
 		

+ 41 - 56
src/mx2cc/buildproduct.monkey2

@@ -41,7 +41,8 @@ Class BuildProduct
 
 	Method Build()
 		
-		If opts.verbose=0 Print "Compiling..."
+'		If opts.verbose=0 
+		Print "Compiling..."
 		
 		If Not CreateDir( module.cacheDir ) Throw New BuildEx( "Error creating dir '"+module.cacheDir+"'" )
 			
@@ -112,7 +113,7 @@ Class BuildProduct
 	
 	Method Exec:Bool( cmd:String,eatstdout:Bool=False )
 		
-		If opts.verbose>2 Print cmd
+		If opts.verbose=3 Print cmd
 			
 		Local fstdout:=eatstdout ? AllocTmpFile( "stdout" ) Else ""
 		
@@ -299,48 +300,40 @@ Class GccBuildProduct Extends BuildProduct
 		
 		Select opts.toolchain
 		Case "msvc"
-			
 			CC_CMD= "cl -c"
 			CXX_CMD="cl -c"
 			AS_CMD=opts.arch="x64" ? "ml64 -c" Else "ml -c"
 			AR_CMD="lib"
 			LD_CMD="link"
 			target="_MSVC"
-			
 		Case "gcc"
-			
 			Select opts.target
 			Case "emscripten"
-				
 				CC_CMD= "emcc -c"
 				CXX_CMD="em++ -c"
-				AR_CMD="emar"
 				LD_CMD="em++"
-				
+				AR_CMD="em++ -o"
 			Case "raspbian"
-
 				CC_CMD= "arm-linux-gnueabihf-gcc -c"
 				CXX_CMD="arm-linux-gnueabihf-g++ -c"
-				AS_CMD= "arm-linux-gnueabihf-as"
-				AR_CMD= "arm-linux-gnueabihf-ar"
 				LD_CMD= "arm-linux-gnueabihf-g++"
-				
+				AR_CMD= "arm-linux-gnueabihf-ar q"
+				AS_CMD= "arm-linux-gnueabihf-as"
+			Case "ios"
+				CC_CMD= "gcc -c"
+				CXX_CMD="g++ -c"
+				LD_CMD= "g++"
+				AR_CMD= "libtool"
+				AS_CMD= "as"
 			Default
-				
-				Local suffix:=GetEnv( "MX2_GCC_SUFFIX" )
-				
-				CC_CMD= "gcc"+suffix+" -c "
-				CXX_CMD="g++"+suffix+" -c "
+				CC_CMD= "gcc -c"
+				CXX_CMD="g++ -c"
+				LD_CMD= "g++"
+				AR_CMD= "ar q"
 				AS_CMD= "as"
-				AR_CMD= "ar"
-				LD_CMD= "g++"+suffix+" "
-				
 			End
-			
 		Default
-			
-			RuntimeError( "Toolchain error: '"+opts.toolchain+"'" )
-			
+			RuntimeError( "Unrecognized error: '"+opts.toolchain+"'" )
 		End
 		
 		CC_CMD+=" "+GetEnv( "MX2_CC_OPTS"+target )+" "+GetEnv( "MX2_CC_OPTS"+target+config )
@@ -348,7 +341,6 @@ Class GccBuildProduct Extends BuildProduct
 		AS_CMD+=" "+GetEnv( "MX2_AS_OPTS"+target )+" "+GetEnv( "MX2_AS_OPTS"+target+config )
 		AR_CMD+=" "+GetEnv( "MX2_AR_OPTS"+target )+" "+GetEnv( "MX2_AR_OPTS"+target+config )
 		LD_CMD+=" "+GetEnv( "MX2_LD_OPTS"+target )+" "+GetEnv( "MX2_LD_OPTS"+target+config )
-		
 	End
 	
 	Method CompileSource:String( src:String )
@@ -432,7 +424,7 @@ Class GccBuildProduct Extends BuildProduct
 			DeleteFile( deps )
 		Endif
 			
-		If opts.verbose>0 Print StripDir( src )
+		If opts.verbose=1 Print StripDir( src )
 			
 		If opts.toolchain<>"msvc"
 			
@@ -458,7 +450,7 @@ Class GccBuildProduct Extends BuildProduct
 			
 			cmd+=" -MMD -MF~q"+deps+"~q"
 			cmd+=" -o ~q"+obj+"~q ~q"+src+"~q"
-			If opts.verbose>2 Print cmd
+			
 			Exec( cmd )
 			
 			Local buf:=New StringStack
@@ -483,7 +475,6 @@ Class GccBuildProduct Extends BuildProduct
 			Local fstdout:=AllocTmpFile( "stdout" )
 			cmd+=" -showIncludes -Fo~q"+obj+"~q ~q"+src+"~q >"+fstdout
 			
-			If opts.verbose>2 Print cmd
 			Local fstderr:=AllocTmpFile( "stderr" )
 			If system( cmd+" 2>"+fstderr )
 				Local buf:=New StringStack
@@ -537,20 +528,22 @@ Class GccBuildProduct Extends BuildProduct
 		objs.AddAll( OBJ_FILES )
 		
 		If opts.productType="module"
-		
 			BuildModule( objs )
-		
 		Else
-		
 			BuildApp( objs )
 		End
 	End
 	
 	Method BuildModule( objs:StringStack )
-
+		
 		Local output:=module.afile
-		If opts.toolchain="msvc" output=StripExt(output)+".lib"
-
+		
+		If opts.toolchain="msvc" 
+			output=StripExt( output )+".lib"
+		Else if opts.target="emscripten"
+			output=StripExt( output )+".bc"
+		Endif
+		
 		Local maxObjTime:Long
 		For Local obj:=Eachin objs
 			maxObjTime=Max( maxObjTime,GetFileTime( obj ) )
@@ -566,33 +559,21 @@ Class GccBuildProduct Extends BuildProduct
 			args+=" ~q"+objs.Get( i )+"~q"
 		Next
 		
-		If opts.target="ios"
+		cmd=AR_CMD
+#If __TARGET__="windows"
+		Local tmp:=AllocTmpFile( "libFiles" )
+		SaveString( args,tmp )
 		
-			cmd="libtool -o ~q"+output+"~q"+args
-			
-		Else If opts.toolchain="msvc"
-			
-			Local tmp:=AllocTmpFile( "libFiles" )
-			SaveString( args,tmp )
-			
-			cmd="lib -out:~q"+output+"~q @~q"+tmp+"~q"
-
+		If opts.toolchain="msvc"
+			cmd+=" -out:~q"+output+"~q @~q"+tmp+"~q"
 		Else
-
-#If __TARGET__="windows"
-
-			Local tmp:=AllocTmpFile( "libFiles" )
-			SaveString( args,tmp )
-			
-			cmd=AR_CMD+" q ~q"+output+"~q @~q"+tmp+"~q"
-			
+			cmd+=" ~q"+output+"~q @~q"+tmp+"~q"
+		Endif
 #Else
-			cmd=AR_CMD+" q ~q"+output+"~q"+args
+		cmd+=" ~q"+output+"~q"+args
 #Endif
-		Endif
 		
 		Exec( cmd,opts.toolchain="msvc" )
-			
 	End
 	
 	Method BuildApp( objs:StringStack ) Virtual
@@ -695,7 +676,11 @@ Class GccBuildProduct Extends BuildProduct
 		
 		For Local imp:=Eachin imports
 			Local afile:=imp.afile
-			If opts.toolchain="msvc" afile=StripExt(afile)+".lib"
+			If opts.target="emscripten"
+				afile=StripExt(afile)+".bc"
+			Else If opts.toolchain="msvc" 
+				afile=StripExt(afile)+".lib"
+			Endif
 			lnkFiles+=" ~q"+afile+"~q"
 		Next
 

+ 2 - 1
src/mx2cc/mx2cc.monkey2

@@ -22,7 +22,8 @@ Global opts_time:Bool
 
 Global StartDir:String
 
-Const TestArgs:="mx2cc makemods"
+'Const TestArgs:="mx2cc makemods -verbose=1 -clean -target=emscripten pyro-framework"
+Const TestArgs:="mx2cc makemods -verbose -target=desktop pyro-framework"
  
 'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 

+ 18 - 9
src/mx2cc/test.monkey2

@@ -1,15 +1,24 @@
 
+Class C
+
+	Method Update()
+		Print "Update!"
+	End
+		
+End
+
+Class D
+
+	Method NewC:C()
+		
+		Return New C
+	End
+End
+
 Function Main()
 	
-	Local t:=New Int[3,3]( 
-	1,2,3,
-	4,5,6,
-	7,8,9 )
 	
-	For Local y:=0 Until 3
-		For Local x:=0 Until 3
-			Print t[x,y]
-		Next
-	Next
+	(New D).NewC().Update()
 	
 End
+

+ 42 - 51
src/mx2cc/translator_cpp.monkey2

@@ -22,7 +22,7 @@ Class Translator_CPP Extends Translator
 		
 		For Local fdecl:=Eachin _module.fileDecls
 			
-			If Builder.opts.verbose>0 Print "Translating "+fdecl.path
+			If Builder.opts.verbose=2 Print "Translating "+fdecl.path
 		
 			Try
 			
@@ -2046,9 +2046,8 @@ Class Translator_CPP Extends Translator
 		Return "static_cast<"+cname+"*>(this)"
 	End
 	
-	Method TransMember:String( instance:Value,member:Value )
+	Method TransMember:String( instance:Value,member:Value,invoking:Bool )
 	
-		'Uses( instance.type )
 		UsesType( instance.type )
 		
 		Local supr:=Cast<SuperValue>( instance )
@@ -2057,18 +2056,17 @@ Class Translator_CPP Extends Translator
 		Local tinst:=Trans( instance )
 		Local tmember:=Trans( member )
 		
-		Local func:=Cast<FuncValue>( member )
-		If func And Not func.simpleGetter And IsVolatile( instance )
-			If _gcframe
-				tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
-			Else
-				If TCast<ArrayType>( instance.type ) Throw New TransEx( "Mark TODO 1")
-				tinst="bbGC::tmp("+tinst+")"
-				
-				'Uses( instance.type )
-				UsesType( instance.type )
-				
-				_gctmps+=1
+		If invoking And IsVolatile( instance )
+			Local func:=Cast<FuncValue>( member )
+			If Not func Or Not func.simpleGetter
+				If _gcframe
+					tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
+				Else
+					If TCast<ArrayType>( instance.type ) Throw New TransEx( "Mark TODO 1")
+					tinst="bbGC::tmp("+tinst+")"
+					UsesType( instance.type )
+					_gctmps+=1
+				Endif
 			Endif
 		Endif
 
@@ -2079,12 +2077,8 @@ Class Translator_CPP Extends Translator
 	
 	Method TransInvokeMember:String( instance:Value,member:FuncValue,args:Value[] )
 
-		'Uses( instance.type )
 		UsesType( instance.type )
 			
-'		Local atype:=TCast<ArrayType>( instance.type )
-'		If atype UsesType( atype.elemType )
-			
 		If member.IsExtension
 			
 			Local tinst:=Trans( instance )
@@ -2094,7 +2088,7 @@ Class Translator_CPP Extends Translator
 					If _gcframe
 						tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
 					Else
-						Throw New TransEx( "Mark TODO 3" )
+						Throw New TransEx( "Mark TODO 2" )
 					Endif
 				Endif
 				tinst="&"+tinst
@@ -2103,7 +2097,7 @@ Class Translator_CPP Extends Translator
 					If _gcframe
 						tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
 					Else
-						Throw New TransEx( "Mark TODO 4" )
+						Throw New TransEx( "Mark TODO 3" )
 					Endif
 				Endif
 						
@@ -2114,7 +2108,7 @@ Class Translator_CPP Extends Translator
 			Return Trans( member )+"("+tinst+TransArgs( args )+")"
 		Endif
 			
-		Return TransMember( instance,member )+"("+TransArgs( args )+")"
+		Return TransMember( instance,member,True )+"("+TransArgs( args )+")"
 	End
 	
 	Method Trans:String( value:InvokeValue )
@@ -2131,7 +2125,7 @@ Class Translator_CPP Extends Translator
 
 	Method Trans:String( value:MemberVarValue )
 	
-		Return TransMember( value.instance,value.member )
+		Return TransMember( value.instance,value.member,False )
 	End
 
 	Method Trans:String( value:MemberFuncValue )
@@ -2429,33 +2423,34 @@ Class Translator_CPP Extends Translator
 
 	'***** Args *****
 	
-	Method IsVolatile:Bool( arg:Value )
+	Method IsVolatileGCType:Bool( arg:Value )
 	
-		If Not IsGCType( arg.type ) Return False
+		Local ucast:=Cast<UpCastValue>( arg )
+		If ucast Return IsVolatileGCType( ucast.value )
 		
-		If arg.IsAssignable
-			
-			Local kind:="?????"
-			Local vvar:=Cast<VarValue>( arg )
-			If vvar kind=vvar.vdecl.kind
-			Local mvar:=Cast<MemberVarValue>( arg )
-			If mvar kind=mvar.member.vdecl.kind
-			Local arr:=Cast<ArrayIndexValue>( arg )
-			If arr kind="element"
-			Local piv:=Cast<PointerIndexValue>( arg )
-			If piv kind="pointer element"
-			Local slf:=Cast<SelfValue>( arg )
-			If slf kind="self"
-			
-			If kind="global" Or kind="field" Or kind="element" 
-				Return True
-			Endif
-			
-			Return False
+		Local ecast:=Cast<ExplicitCastValue>( arg )
+		If ecast Return IsVolatileGCType( ecast.value )
 		
-		Endif
+		Local vvar:=Cast<VarValue>( arg )
+		If vvar Return vvar.vdecl.kind="global" Or vvar.vdecl.kind="field"
+		
+		Local mvar:=Cast<MemberVarValue>( arg )
+		If mvar Return mvar.member.vdecl.kind="global" Or mvar.member.vdecl.kind="field"
 		
-		Return arg.HasSideEffects
+		If Cast<LiteralValue>( arg ) Return False
+		
+		If Cast<SuperValue>( arg ) Return False
+		
+		If Cast<SelfValue>( arg ) Return False
+		
+		Return True
+	End
+	
+	Method IsVolatile:Bool( arg:Value )
+
+		If IsGCType( arg.type ) Return IsVolatileGCType( arg )
+		
+		Return False
 	End
 	
 	Method TransArgs:String( args:Value[] )
@@ -2464,7 +2459,6 @@ Class Translator_CPP Extends Translator
 		
 		For Local arg:=Eachin args
 
-'			Decls( arg.type )
 			DeclsVar( arg.type )
 		
 			Local t:=Trans( arg )
@@ -2473,12 +2467,9 @@ Class Translator_CPP Extends Translator
 				If _gcframe
 					t=AllocGCTmp( arg.type )+"="+t
 				Else
-					If TCast<ArrayType>( arg.type ) Throw New TransEx( "Mark TODO 2" )
+					If TCast<ArrayType>( arg.type ) Throw New TransEx( "Mark TODO 4" )
 					t="bbGC::tmp("+t+")"
-					
-					'Uses( arg.type )
 					UsesType( arg.type )
-					
 					_gctmps+=1
 				Endif
 			Endif