<commit_before>-----------------------------------------------------------------------------
--
-- Code generation for foreign calls.
--
-- (c) The University of Glasgow 2004-2006
--
-----------------------------------------------------------------------------

module StgCmmForeign (
  cgForeignCall, loadThreadState, saveThreadState,
  emitPrimCall, emitCCall,
  emitSaveThreadState, -- will be needed by the Cmm parser
  emitLoadThreadState, -- ditto
  emitOpenNursery,
 ) where

#include "HsVersions.h"

import StgSyn
import StgCmmProf
import StgCmmEnv
import StgCmmMonad
import StgCmmUtils
import StgCmmClosure

import BlockId
import Cmm
import CmmUtils
import MkZipCfgCmm hiding (CmmAGraph)
import Type
import TysPrim
import CLabel
import SMRep
import ForeignCall
import Constants
import StaticFlags
import Maybes
import Outputable
import ZipCfgCmmRep
import BasicTypes

import Control.Monad

-----------------------------------------------------------------------------
-- Code generation for Foreign Calls
-----------------------------------------------------------------------------

cgForeignCall :: [LocalReg]		-- r1,r2  where to put the results
	      -> [ForeignHint]
	      -> ForeignCall		-- the op
	      -> [StgArg]		-- x,y	  arguments
	      -> FCode ()
-- Emits code for an unsafe foreign call:      r1, r2 = foo( x, y, z )

cgForeignCall results result_hints (CCall (CCallSpec target cconv safety)) stg_args
  = do	{ cmm_args <- getFCallArgs stg_args
	; let (args, arg_hints) = unzip cmm_args
	      fc = ForeignConvention cconv arg_hints result_hints
	      (call_args, cmm_target)
		= case target of
		   StaticTarget lbl -> (args, CmmLit (CmmLabel 
						(mkForeignLabel lbl (call_size args) False IsFunction)))
		   DynamicTarget    ->  case args of
                                        fn:rest -> (rest, fn)
                                        [] -> panic "cgForeignCall []"
	      call_target = ForeignTarget cmm_target fc
	
	; srt <- getSRTInfo NoSRT	-- SLPJ: Not sure what SRT 
					-- is right here
                                        -- JD: Does it matter in the new codegen?
	; emitForeignCall safety results call_target call_args srt CmmMayReturn }
  where
	-- in the stdcall calling convention, the symbol needs @size appended
	-- to it, where size is the total number of bytes of arguments.  We
	-- attach this info to the CLabel here, and the CLabel pretty printer
	-- will generate the suffix when the label is printed.
      call_size args
	| StdCallConv <- cconv = Just (sum (map arg_size args))
	| otherwise            = Nothing

	-- ToDo: this might not be correct for 64-bit API
      arg_size arg = max (widthInBytes $ typeWidth $ cmmExprType arg) wORD_SIZE

cgForeignCall _ _ (DNCall _) _
  = panic "cgForeignCall: DNCall"

emitCCall :: [(CmmFormal,ForeignHint)]
	  -> CmmExpr 
	  -> [(CmmActual,ForeignHint)]
	  -> FCode ()
emitCCall hinted_results fn hinted_args<commit_msg>Inconsistent type and arguments in safe foreign calls... - The function argument was stripped from the argument list but not from the type.   Now they're both stripped.<commit_after>-----------------------------------------------------------------------------
--
-- Code generation for foreign calls.
--
-- (c) The University of Glasgow 2004-2006
--
-----------------------------------------------------------------------------

module StgCmmForeign (
  cgForeignCall, loadThreadState, saveThreadState,
  emitPrimCall, emitCCall,
  emitSaveThreadState, -- will be needed by the Cmm parser
  emitLoadThreadState, -- ditto
  emitOpenNursery,
 ) where

#include "HsVersions.h"

import StgSyn
import StgCmmProf
import StgCmmEnv
import StgCmmMonad
import StgCmmUtils
import StgCmmClosure

import BlockId
import Cmm
import CmmUtils
import MkZipCfgCmm hiding (CmmAGraph)
import Type
import TysPrim
import CLabel
import SMRep
import ForeignCall
import Constants
import StaticFlags
import Maybes
import Outputable
import ZipCfgCmmRep
import BasicTypes

import Control.Monad

-----------------------------------------------------------------------------
-- Code generation for Foreign Calls
-----------------------------------------------------------------------------

cgForeignCall :: [LocalReg]		-- r1,r2  where to put the results
	      -> [ForeignHint]
	      -> ForeignCall		-- the op
	      -> [StgArg]		-- x,y	  arguments
	      -> FCode ()
-- Emits code for an unsafe foreign call:      r1, r2 = foo( x, y, z )

cgForeignCall results result_hints (CCall (CCallSpec target cconv safety)) stg_args
  = do	{ cmm_args <- getFCallArgs stg_args
        ; let ((call_args, arg_hints), cmm_target)
                = case target of
                    StaticTarget lbl ->
                      (unzip cmm_args,
                       CmmLit (CmmLabel (mkForeignLabel lbl (call_size cmm_args) False IsFunction)))
                    DynamicTarget    ->  case cmm_args of
                                           (fn,_):rest -> (unzip rest, fn)
                                           [] -> panic "cgForeignCall []"
              fc = ForeignConvention cconv arg_hints result_hints
              call_target = ForeignTarget cmm_target fc
        
        ; srt <- getSRTInfo NoSRT        -- SLPJ: Not sure what SRT 
                                        -- is right here
                                        -- JD: Does it matter in the new codegen?
        ; emitForeignCall safety results call_target call_args srt CmmMayReturn }
  where
	-- in the stdcall calling convention, the symbol needs @size appended
	-- to it, where size is the total number of bytes of arguments.  We
	-- attach this info to the CLabel here, and the CLabel pretty printer
	-- will generate the suffix when the label is printed.
      call_size args
	| StdCallConv <- cconv = Just (sum (map arg_size args))
	| otherwise            = Nothing

	-- ToDo: this might not be correct for 64-bit API
      arg_size (arg, _) = max (widthInBytes $ typeWidth $ cmmExprType arg) wORD_SIZE

cgForeignCall _ _ (DNCall _) _
  = panic "cgForeignCall: DNCall"

emitCCall :: [(CmmFormal,ForeignHint)]
	  -> CmmExpr 
	  -> [(CmmActual,ForeignHint)]
	  -> FCode ()
emitCCall hinted_results fn hinted_args
{'fraction_non_alphanumeric': 0.1641091907607818, 'fraction_numerical': 0.004522694233564852, 'mean_word_length': 1.8209567198177676, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1987264', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1661, 'n_words': 579}