summaryrefslogtreecommitdiffstats
path: root/src/base/wlc/wlcWin.c
blob: 4dc748f4e001ca1bc7130fcb20ada01a1a6eaa1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**CFile****************************************************************

  FileName    [wlcWin.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Parses several flavors of word-level Verilog.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 22, 2014.]

  Revision    [$Id: wlcWin.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

***********************************************************************/

#include "wlc.h"
#include "base/abc/abc.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Collect arithmetic nodes.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Wlc_ObjIsArithm( Wlc_Obj_t * pObj )
{
    return pObj->Type == WLC_OBJ_CONST       || 
           pObj->Type == WLC_OBJ_BUF         || pObj->Type == WLC_OBJ_BIT_NOT     ||
           pObj->Type == WLC_OBJ_BIT_ZEROPAD || pObj->Type == WLC_OBJ_BIT_SIGNEXT ||
//           pObj->Type == WLC_OBJ_BIT_SELECT  || pObj->Type == WLC_OBJ_BIT_CONCAT  ||
           pObj->Type == WLC_OBJ_ARI_ADD     || pObj->Type == WLC_OBJ_ARI_SUB     || 
           pObj->Type == WLC_OBJ_ARI_MULTI   || pObj->Type == WLC_OBJ_ARI_MINUS;
}
int Wlc_ObjIsArithmReal( Wlc_Obj_t * pObj )
{
    return pObj->Type == WLC_OBJ_BIT_NOT     ||
           pObj->Type == WLC_OBJ_ARI_ADD     || pObj->Type == WLC_OBJ_ARI_SUB     || 
           pObj->Type == WLC_OBJ_ARI_MULTI   || pObj->Type == WLC_OBJ_ARI_MINUS;
}
int Wlc_ManCountArithmReal( Wlc_Ntk_t * p, Vec_Int_t * vNodes )
{
    Wlc_Obj_t * pObj; 
    int i, Counter = 0;
    Wlc_NtkForEachObjVec( vNodes, p, pObj, i )
        Counter += Wlc_ObjIsArithmReal( pObj );
    return Counter;
}
int Wlc_ObjHasArithm_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    if ( pObj->Type == WLC_OBJ_CONST )
        return 0;
    if ( pObj->Type == WLC_OBJ_BUF         || pObj->Type == WLC_OBJ_BIT_NOT ||
         pObj->Type == WLC_OBJ_BIT_ZEROPAD || pObj->Type == WLC_OBJ_BIT_SIGNEXT )
         return Wlc_ObjHasArithm_rec( p, Wlc_ObjFanin0(p, pObj) );
    return pObj->Type == WLC_OBJ_ARI_ADD   || pObj->Type == WLC_OBJ_ARI_SUB || 
           pObj->Type == WLC_OBJ_ARI_MULTI || pObj->Type == WLC_OBJ_ARI_MINUS;
}
int Wlc_ObjHasArithmFanins( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    Wlc_Obj_t * pFanin;  int i;
    assert( !Wlc_ObjHasArithm_rec(p, pObj) );
    Wlc_ObjForEachFaninObj( p, pObj, pFanin, i )
        if ( Wlc_ObjHasArithm_rec(p, pFanin) )
            return 1;
    return 0;
}
void Wlc_WinCompute_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vLeaves, Vec_Int_t * vNodes )
{
    Wlc_Obj_t * pFanin;  int i;
    if ( pObj->Mark )
        return;
    pObj->Mark = 1;
    if ( !Wlc_ObjIsArithm(pObj) )
    {
        Vec_IntPush( vLeaves, Wlc_ObjId(p, pObj) );
        return;
    }
    Wlc_ObjForEachFaninObj( p, pObj, pFanin, i )
        Wlc_WinCompute_rec( p, pFanin, vLeaves, vNodes );
    Vec_IntPush( vNodes, Wlc_ObjId(p, pObj) );
}
void Wlc_WinCleanMark_rec( Wlc_Ntk_t * p, Wlc_Obj_t * pObj )
{
    Wlc_Obj_t * pFanin;  int i;
    if ( !pObj->Mark )
        return;
    pObj->Mark = 0;
    Wlc_ObjForEachFaninObj( p, pObj, pFanin, i )
        Wlc_WinCleanMark_rec( p, pFanin );
}
void Wlc_WinCompute( Wlc_Ntk_t * p, Wlc_Obj_t * pObj, Vec_Int_t * vLeaves, Vec_Int_t * vNodes )
{
    Vec_IntClear( vLeaves );
    Vec_IntClear( vNodes );
    if ( Wlc_ObjHasArithm_rec(p, pObj) )
    {
        Wlc_WinCompute_rec( p, pObj, vLeaves, vNodes );
        Wlc_WinCleanMark_rec( p, pObj );
    }
    else if ( Wlc_ObjHasArithmFanins(p, pObj) )
    {
        Wlc_Obj_t * pFanin;  int i;
        Wlc_ObjForEachFaninObj( p, pObj, pFanin, i )
            if ( Wlc_ObjHasArithm_rec(p, pFanin) )
                Wlc_WinCompute_rec( p, pFanin, vLeaves, vNodes );
        Wlc_ObjForEachFaninObj( p, pObj, pFanin, i )
            if ( Wlc_ObjHasArithm_rec(p, pFanin) )
                Wlc_WinCleanMark_rec( p, pFanin );
    }
    else assert( 0 );
}
void Wlc_WinProfileArith( Wlc_Ntk_t * p )
{
    Vec_Int_t * vLeaves = Vec_IntAlloc( 1000 );
    Vec_Int_t * vNodes  = Vec_IntAlloc( 1000 );
    Wlc_Obj_t * pObj; int i, Count = 0;
    Wlc_NtkForEachObj( p, pObj, i )
        pObj->Mark = 0;
    Wlc_NtkForEachObj( p, pObj, i )
        if ( Wlc_ObjHasArithm_rec(p, pObj) ? Wlc_ObjIsCo(pObj) : Wlc_ObjHasArithmFanins(p, pObj) )
        {
            Wlc_WinCompute( p, pObj, vLeaves, vNodes ); 
            if ( Wlc_ManCountArithmReal(p, vNodes) < 2 )
                continue;

            printf( "Arithmetic cone of node %d (%s):\n", Wlc_ObjId(p, pObj), Wlc_ObjName(p, Wlc_ObjId(p, pObj)) );
            Wlc_NtkPrintNode( p, pObj );
            Vec_IntReverseOrder( vNodes );
            Wlc_NtkPrintNodeArray( p, vNodes );
            printf( "\n" );
            Count++;
        }
    Wlc_NtkForEachObj( p, pObj, i )
        assert( pObj->Mark == 0 );
    printf( "Finished printing %d arithmetic cones.\n", Count );
    Vec_IntFree( vLeaves );
    Vec_IntFree( vNodes );
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END
); alert.setView(view); mKeyserverEditText = (EditText) view.findViewById(R.id.keyserver_url_edit_text); mVerifyKeyserverCheckBox = (CheckBox) view.findViewById(R.id.verify_keyserver_checkbox); switch (mDialogAction) { case ADD: { alert.setTitle(R.string.add_keyserver_dialog_title); break; } case EDIT: { alert.setTitle(R.string.edit_keyserver_dialog_title); mKeyserverEditText.setText(getArguments().getString(ARG_KEYSERVER)); break; } } // we don't want dialog to be dismissed on click for keyserver addition or edit, // thereby requiring the hack seen below and in onStart alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // we need to have an empty listener to prevent errors on some devices as mentioned // at http://stackoverflow.com/q/13746412/3000919 // actual listener set in onStart for adding keyservers or editing them } }); alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dismiss(); } }); switch (mDialogAction) { case EDIT: { alert.setNeutralButton(R.string.label_keyserver_dialog_delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { deleteKeyserver(mPosition); } }); break; } case ADD: { // do nothing break; } } // Hack to open keyboard. // This is the only method that I found to work across all Android versions // http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/ // Notes: * onCreateView can't be used because we want to add buttons to the dialog // * opening in onActivityCreated does not work on Android 4.4 mKeyserverEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { mKeyserverEditText.post(new Runnable() { @Override public void run() { InputMethodManager imm = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mKeyserverEditText, InputMethodManager.SHOW_IMPLICIT); } }); } }); mKeyserverEditText.requestFocus(); mKeyserverEditText.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); mKeyserverEditText.setOnEditorActionListener(this); return alert.show(); } @Override public void onStart() { super.onStart(); AlertDialog addKeyserverDialog = (AlertDialog) getDialog(); if (addKeyserverDialog != null) { Button positiveButton = addKeyserverDialog.getButton(Dialog.BUTTON_POSITIVE); positiveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // behaviour same for edit and add final String keyserverUrl = mKeyserverEditText.getText().toString(); if (mVerifyKeyserverCheckBox.isChecked()) { final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()) .getProxyPrefs(); Runnable ignoreTor = new Runnable() { @Override public void run() { verifyConnection(keyserverUrl, null); } }; if (OrbotHelper.putOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, getActivity())) { verifyConnection(keyserverUrl, proxyPrefs.parcelableProxy.getProxy()); } } else { dismiss(); // return unverified keyserver back to activity keyserverEdited(keyserverUrl, false); } } }); } } public void keyserverEdited(String keyserver, boolean verified) { dismiss(); Bundle data = new Bundle(); data.putSerializable(MESSAGE_DIALOG_ACTION, mDialogAction); data.putString(MESSAGE_KEYSERVER, keyserver); data.putBoolean(MESSAGE_VERIFIED, verified); if (mDialogAction == DialogAction.EDIT) { data.putInt(MESSAGE_EDIT_POSITION, mPosition); } sendMessageToHandler(MESSAGE_OKAY, data); } public void deleteKeyserver(int position) { dismiss(); Bundle data = new Bundle(); data.putSerializable(MESSAGE_DIALOG_ACTION, DialogAction.EDIT); data.putInt(MESSAGE_EDIT_POSITION, position); data.putBoolean(MESSAGE_KEYSERVER_DELETED, true); sendMessageToHandler(MESSAGE_OKAY, data); } public void verificationFailed(FailureReason reason) { Bundle data = new Bundle(); data.putSerializable(MESSAGE_FAILURE_REASON, reason); sendMessageToHandler(MESSAGE_VERIFICATION_FAILED, data); } public void verifyConnection(String keyserver, final Proxy proxy) { new AsyncTask<String, Void, FailureReason>() { ProgressDialog mProgressDialog; String mKeyserver; @Override protected void onPreExecute() { mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.setMessage(getString(R.string.progress_verifying_keyserver_url)); mProgressDialog.setCancelable(false); mProgressDialog.show(); } @Override protected FailureReason doInBackground(String... keyservers) { mKeyserver = keyservers[0]; FailureReason reason = null; try { // replace hkps/hkp scheme and reconstruct Uri Uri keyserverUri = Uri.parse(mKeyserver); String scheme = keyserverUri.getScheme(); String schemeSpecificPart = keyserverUri.getSchemeSpecificPart(); String fragment = keyserverUri.getFragment(); if (scheme == null) { throw new MalformedURLException(); } if ("hkps".equalsIgnoreCase(scheme)) { scheme = "https"; } else if ("hkp".equalsIgnoreCase(scheme)) { scheme = "http"; } URI newKeyserver = new URI(scheme, schemeSpecificPart, fragment); Log.d("Converted URL", newKeyserver.toString()); OkHttpClient client = HkpKeyserver.getClient(newKeyserver.toURL(), proxy); TlsHelper.pinCertificateIfNecessary(client, newKeyserver.toURL()); client.newCall(new Request.Builder().url(newKeyserver.toURL()).build()).execute(); } catch (TlsHelper.TlsHelperException e) { reason = FailureReason.CONNECTION_FAILED; } catch (MalformedURLException | URISyntaxException e) { Log.w(Constants.TAG, "Invalid keyserver URL entered by user."); reason = FailureReason.INVALID_URL; } catch (IOException e) { Log.w(Constants.TAG, "Could not connect to entered keyserver url"); reason = FailureReason.CONNECTION_FAILED; } return reason; } @Override protected void onPostExecute(FailureReason failureReason) { mProgressDialog.dismiss(); if (failureReason == null) { keyserverEdited(mKeyserver, true); } else { verificationFailed(failureReason); } } }.execute(keyserver); } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); // hide keyboard on dismiss hideKeyboard(); } private void hideKeyboard() { if (getActivity() == null) { return; } InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); //check if no view has focus: View v = getActivity().getCurrentFocus(); if (v == null) return; inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); } /** * Associate the "done" button on the soft keyboard with the okay button in the view */ @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (EditorInfo.IME_ACTION_DONE == actionId) { AlertDialog dialog = ((AlertDialog) getDialog()); Button bt = dialog.getButton(AlertDialog.BUTTON_POSITIVE); bt.performClick(); return true; } return false; } /** * Send message back to handler which is initialized in a activity * * @param what Message integer you want to send */ private void sendMessageToHandler(Integer what, Bundle data) { Message msg = Message.obtain(); msg.what = what; if (data != null) { msg.setData(data); } try { mMessenger.send(msg); } catch (RemoteException e) { Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); } catch (NullPointerException e) { Log.w(Constants.TAG, "Messenger is null!", e); } } }