Compare commits
51 Commits
v1.0.1-alp
...
qt5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b58f315c98 | ||
|
|
7687c2b7d2 | ||
|
|
0abbce6f4e | ||
|
|
8ffdf657ec | ||
|
|
2232a76033 | ||
|
|
d6af9c1555 | ||
|
|
d7acc8cd4d | ||
|
|
a5f1d17e83 | ||
|
|
29efcd5be1 | ||
|
|
4656db9484 | ||
|
|
b33d7239e5 | ||
|
|
e1f0c084f8 | ||
|
|
e2a6b25345 | ||
|
|
f210ed78c2 | ||
|
|
ccc8cc526b | ||
|
|
cd6797499f | ||
|
|
b60903306f | ||
|
|
1df7cb3be4 | ||
|
|
a7265f06b0 | ||
|
|
58532f4402 | ||
|
|
7d986ef661 | ||
|
|
7f4197acc1 | ||
|
|
e71c8051c3 | ||
|
|
73cf949e25 | ||
|
|
b2be1cf2da | ||
|
|
94e3016a5b | ||
|
|
145a50369e | ||
|
|
d77927c608 | ||
|
|
3bb72987a6 | ||
|
|
c782892db4 | ||
|
|
a944ea5da8 | ||
|
|
d1738ea630 | ||
|
|
5f39236ba2 | ||
|
|
ede09ddae3 | ||
|
|
34b1f4f4fe | ||
|
|
d6249916e1 | ||
|
|
9cd3226536 | ||
|
|
3f217e83da | ||
|
|
652cfb67c3 | ||
|
|
c0e9ba2fb3 | ||
|
|
5963f5fd4d | ||
|
|
12ee08f87e | ||
|
|
5c85c92d1a | ||
|
|
b509d0fcf0 | ||
|
|
bb007ddefc | ||
|
|
9129d48429 | ||
|
|
d105182051 | ||
|
|
157a968372 | ||
|
|
bae2a582f1 | ||
|
|
19191876e2 | ||
|
|
fcfe3c1f4b |
6
.gitattributes
vendored
Normal file
6
.gitattributes
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
* text=auto
|
||||
*.c text
|
||||
*.cpp text
|
||||
*.ui text
|
||||
*.qrc text
|
||||
*.h text
|
||||
3
3rd_party/libdisasm/libdis.h
vendored
3
3rd_party/libdisasm/libdis.h
vendored
@ -317,6 +317,9 @@ struct x86_op_t{
|
||||
case op_offset:
|
||||
return data.offset;
|
||||
case op_relative_far:
|
||||
if (data.relative_far & 0x8000)
|
||||
return (data.relative_far & 0xFFFF) | 0xFFFF0000;
|
||||
else
|
||||
return (int32_t)data.relative_far;
|
||||
default:
|
||||
assert(false);
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
PROJECT(dcc_original)
|
||||
cmake_minimum_required(VERSION 2.8.9)
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
find_package(Qt5Core)
|
||||
@ -7,15 +7,15 @@ find_package(Qt5Core)
|
||||
OPTION(dcc_build_tests "Enable unit tests." OFF)
|
||||
#SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
|
||||
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS)
|
||||
IF(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
|
||||
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D_CRT_NONSTDC_NO_DEPRECATE)
|
||||
IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D_CRT_NONSTDC_NO_DEPRECATE -DNOMINMAX)
|
||||
ADD_DEFINITIONS(/W4)
|
||||
ELSE()
|
||||
#-D_GLIBCXX_DEBUG
|
||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
|
||||
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} " ) #--coverage
|
||||
ENDIF()
|
||||
|
||||
SET(CMAKE_CXX_STANDARD 11)
|
||||
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeScripts;${CMAKE_MODULE_PATH})
|
||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
|
||||
include(cotire)
|
||||
@ -26,17 +26,12 @@ enable_testing()
|
||||
ENDIF()
|
||||
|
||||
|
||||
|
||||
find_package(LLVM REQUIRED CONFIG)
|
||||
llvm_map_components_to_libnames(REQ_LLVM_LIBRARIES native mc support tablegen)
|
||||
|
||||
INCLUDE_DIRECTORIES(
|
||||
3rd_party/libdisasm
|
||||
include
|
||||
include/idioms
|
||||
common
|
||||
${Boost_INCLUDE_DIRS}
|
||||
${LLVM_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
|
||||
@ -95,6 +90,7 @@ set(dcc_HEADERS
|
||||
include/bundle.h
|
||||
include/BinaryImage.h
|
||||
include/DccFrontend.h
|
||||
include/Enums.h
|
||||
include/dcc.h
|
||||
include/disassem.h
|
||||
include/dosdcc.h
|
||||
@ -135,8 +131,10 @@ qt5_use_modules(dcc_lib Core)
|
||||
|
||||
ADD_EXECUTABLE(dcc_original ${dcc_SOURCES} ${dcc_HEADERS})
|
||||
ADD_DEPENDENCIES(dcc_original dcc_lib)
|
||||
TARGET_LINK_LIBRARIES(dcc_original dcc_lib dcc_hash disasm_s ${REQ_LLVM_LIBRARIES} LLVMSupport)
|
||||
TARGET_LINK_LIBRARIES(dcc_original dcc_lib dcc_hash disasm_s)
|
||||
qt5_use_modules(dcc_original Core)
|
||||
SET_PROPERTY(TARGET dcc_original PROPERTY CXX_STANDARD 11)
|
||||
SET_PROPERTY(TARGET dcc_original PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
#ADD_SUBDIRECTORY(gui)
|
||||
if(dcc_build_tests)
|
||||
ADD_SUBDIRECTORY(src)
|
||||
|
||||
339
LICENSE
Normal file
339
LICENSE
Normal file
@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
@ -6,6 +6,8 @@ To reflect those fixes, I've edited the original readme a bit.
|
||||
dcc Distribution
|
||||
================
|
||||
|
||||
[](https://gitter.im/nemerle/dcc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
The code provided in this distribution is (C) by their authors:
|
||||
- Cristina Cifuentes (most of dcc code)
|
||||
- Mike van Emmerik (signatures and prototype code)
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
*/
|
||||
#include "perfhlib.h"
|
||||
#include "PatternCollector.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <cassert>
|
||||
@ -38,9 +39,6 @@ static bool *visited; /* Array of bools: whether visited */
|
||||
static bool *deleted; /* Array of bools: whether deleted */
|
||||
|
||||
/* Private prototypes */
|
||||
static void initGraph(void);
|
||||
static void addToGraph(int e, int v1, int v2);
|
||||
static bool isCycle(void);
|
||||
static void duplicateKeys(int v1, int v2);
|
||||
|
||||
void PerfectHash::setHashParams(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,
|
||||
@ -157,7 +155,7 @@ void PerfectHash::map(PatternCollector *collector)
|
||||
}
|
||||
addToGraph(numEdges++, f1, f2);
|
||||
}
|
||||
if (cycle || (cycle = isCycle())) /* OK - is there a cycle? */
|
||||
if (cycle or (cycle = isCycle())) /* OK - is there a cycle? */
|
||||
{
|
||||
printf("Iteration %d\n", ++c);
|
||||
}
|
||||
@ -314,7 +312,7 @@ bool PerfectHash::isCycle(void)
|
||||
}
|
||||
for (v=1; v <= NumVert; v++)
|
||||
{
|
||||
if (!visited[v])
|
||||
if (not visited[v])
|
||||
{
|
||||
if (DFS(-32767, v))
|
||||
{
|
||||
@ -335,7 +333,7 @@ void PerfectHash::traverse(int u)
|
||||
while (e)
|
||||
{
|
||||
w = graphNode[NumEntry+e]-1;
|
||||
if (!visited[w])
|
||||
if (not visited[w])
|
||||
{
|
||||
g[w] = (abs(e)-1 - g[u]) % NumEntry;
|
||||
if (g[w] < 0) g[w] += NumEntry; /* Keep these positive */
|
||||
@ -359,7 +357,7 @@ void PerfectHash::assign(void)
|
||||
|
||||
for (v=0; v < NumVert; v++)
|
||||
{
|
||||
if (!visited[v])
|
||||
if (not visited[v])
|
||||
{
|
||||
g[v] = 0;
|
||||
traverse(v);
|
||||
@ -415,7 +413,7 @@ duplicateKeys(int v1, int v2)
|
||||
u += T1[keys[j] - SetMin];
|
||||
}
|
||||
u %= NumVert;
|
||||
if ((u != v1) && (u != v2)) continue;
|
||||
if ((u != v1) and (u != v2)) continue;
|
||||
|
||||
v = 0;
|
||||
for (j=0; j < EntryLen; j++)
|
||||
@ -425,7 +423,7 @@ duplicateKeys(int v1, int v2)
|
||||
}
|
||||
v %= NumVert;
|
||||
|
||||
if ((v == v2) || (v == v1))
|
||||
if ((v == v2) or (v == v1))
|
||||
{
|
||||
printf("Entry #%d key: ", i+1);
|
||||
for (j=0; j < EntryLen; j++) printf("%02X ", keys[j]);
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
/** Perfect hashing function library. Contains functions to generate perfect
|
||||
hashing functions */
|
||||
|
||||
@ -3,8 +3,6 @@
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
#include <string>
|
||||
#include <llvm/ADT/ilist.h>
|
||||
#include <llvm/ADT/ilist_node.h>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include "icode.h"
|
||||
#include "types.h"
|
||||
@ -27,7 +25,7 @@ struct TYPEADR_TYPE
|
||||
TYPEADR_TYPE(interval *v) : ip(0),BBptr(nullptr),intPtr(v)
|
||||
{}
|
||||
};
|
||||
struct BB : public llvm::ilist_node<BB>
|
||||
struct BB
|
||||
{
|
||||
friend struct Function;
|
||||
private:
|
||||
@ -111,7 +109,7 @@ public:
|
||||
///
|
||||
const Function *getParent() const { return Parent; }
|
||||
Function *getParent() { return Parent; }
|
||||
void writeBB(std::ostream &ostr, int lev, Function *pProc, int *numLoc);
|
||||
void writeBB(QTextStream & ostr, int lev, Function *pProc, int *numLoc);
|
||||
BB * rmJMP(int marker, BB *pBB);
|
||||
void genDU1();
|
||||
void findBBExps(LOCAL_ID &locals, Function *f);
|
||||
|
||||
@ -3,21 +3,21 @@
|
||||
#include <vector>
|
||||
struct PROG /* Loaded program image parameters */
|
||||
{
|
||||
int16_t initCS;
|
||||
int16_t initIP; /* These are initial load values */
|
||||
int16_t initSS; /* Probably not of great interest */
|
||||
uint16_t initSP;
|
||||
bool fCOM; /* Flag set if COM program (else EXE)*/
|
||||
int cReloc; /* No. of relocation table entries */
|
||||
uint16_t initCS=0;
|
||||
uint16_t initIP=0; /* These are initial load values */
|
||||
uint16_t initSS=0; /* Probably not of great interest */
|
||||
uint16_t initSP=0;
|
||||
bool fCOM=false; /* Flag set if COM program (else EXE)*/
|
||||
int cReloc=0; /* No. of relocation table entries */
|
||||
std::vector<uint32_t> relocTable; /* Ptr. to relocation table */
|
||||
uint8_t * map; /* Memory bitmap ptr */
|
||||
int cProcs; /* Number of procedures so far */
|
||||
int offMain; /* The offset of the main() proc */
|
||||
uint16_t segMain; /* The segment of the main() proc */
|
||||
bool bSigs; /* True if signatures loaded */
|
||||
int cbImage; /* Length of image in bytes */
|
||||
uint8_t * Imagez; /* Allocated by loader to hold entire program image */
|
||||
int addressingMode;
|
||||
uint8_t * map=nullptr; /* Memory bitmap ptr */
|
||||
int cProcs=0; /* Number of procedures so far */
|
||||
int offMain=0; /* The offset of the main() proc */
|
||||
uint16_t segMain=0; /* The segment of the main() proc */
|
||||
bool bSigs=false; /* True if signatures loaded */
|
||||
int cbImage=0; /* Length of image in bytes */
|
||||
uint8_t * Imagez=nullptr; /* Allocated by loader to hold entire program image */
|
||||
int addressingMode=0;
|
||||
public:
|
||||
const uint8_t *image() const {return Imagez;}
|
||||
void displayLoadInfo();
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
#pragma once
|
||||
#include "ast.h"
|
||||
|
||||
class QTextStream;
|
||||
|
||||
struct CConv {
|
||||
enum Type {
|
||||
UNKNOWN=0,
|
||||
C,
|
||||
PASCAL
|
||||
eUnknown=0,
|
||||
eCdecl,
|
||||
ePascal
|
||||
};
|
||||
virtual void processHLI(Function *func, Expr *_exp, iICODE picode)=0;
|
||||
virtual void writeComments(std::ostream &)=0;
|
||||
virtual void writeComments(QTextStream &)=0;
|
||||
static CConv * create(Type v);
|
||||
protected:
|
||||
|
||||
@ -16,16 +18,16 @@ protected:
|
||||
|
||||
struct C_CallingConvention : public CConv {
|
||||
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
|
||||
virtual void writeComments(std::ostream &);
|
||||
virtual void writeComments(QTextStream &);
|
||||
|
||||
private:
|
||||
int processCArg(Function *callee, Function *pProc, ICODE *picode, size_t numArgs);
|
||||
};
|
||||
struct Pascal_CallingConvention : public CConv {
|
||||
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
|
||||
virtual void writeComments(std::ostream &);
|
||||
virtual void writeComments(QTextStream &);
|
||||
};
|
||||
struct Unknown_CallingConvention : public CConv {
|
||||
void processHLI(Function *func, Expr *_exp, iICODE picode) {}
|
||||
virtual void writeComments(std::ostream &);
|
||||
virtual void writeComments(QTextStream &);
|
||||
};
|
||||
|
||||
@ -90,16 +90,16 @@ enum eLLFlags
|
||||
/* Types of icodes */
|
||||
enum icodeType
|
||||
{
|
||||
NOT_SCANNED = 0, // not even scanned yet
|
||||
LOW_LEVEL, // low-level icode
|
||||
HIGH_LEVEL // high-level icode
|
||||
NOT_SCANNED_ICODE = 0, // not even scanned yet
|
||||
LOW_LEVEL_ICODE, // low-level icode
|
||||
HIGH_LEVEL_ICODE // high-level icode
|
||||
};
|
||||
|
||||
|
||||
/* LOW_LEVEL icode opcodes */
|
||||
enum llIcode
|
||||
{
|
||||
//iINVALID,
|
||||
iINVALID=-1,
|
||||
iCBW, /* 0 */
|
||||
iAAA,
|
||||
iAAD,
|
||||
|
||||
@ -1,14 +1,18 @@
|
||||
#pragma once
|
||||
#include <llvm/ADT/ilist.h>
|
||||
//#include <llvm/ADT/ilist_node.h>
|
||||
#include <bitset>
|
||||
#include <map>
|
||||
#include "BasicBlock.h"
|
||||
#include "locident.h"
|
||||
#include "state.h"
|
||||
#include "icode.h"
|
||||
#include "StackFrame.h"
|
||||
#include "CallConvention.h"
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <bitset>
|
||||
#include <map>
|
||||
|
||||
class QIODevice;
|
||||
class QTextStream;
|
||||
|
||||
/* PROCEDURE NODE */
|
||||
struct CALL_GRAPH;
|
||||
struct Expr;
|
||||
@ -19,29 +23,6 @@ struct PROG;
|
||||
|
||||
struct Function;
|
||||
|
||||
namespace llvm
|
||||
{
|
||||
// Traits for intrusive list of basic blocks...
|
||||
template<>
|
||||
struct ilist_traits<BB> : public ilist_default_traits<BB>
|
||||
{
|
||||
|
||||
// createSentinel is used to get hold of the node that marks the end of the
|
||||
// list... (same trick used here as in ilist_traits<Instruction>)
|
||||
BB *createSentinel() const {
|
||||
return static_cast<BB*>(&Sentinel);
|
||||
}
|
||||
static void destroySentinel(BB*) {}
|
||||
|
||||
BB *provideInitialHead() const { return createSentinel(); }
|
||||
BB *ensureHead(BB*) const { return createSentinel(); }
|
||||
static void noteHead(BB*, BB*) {}
|
||||
|
||||
//static ValueSymbolTable *getSymTab(Function *ItemParent);
|
||||
private:
|
||||
mutable ilist_half_node<BB> Sentinel;
|
||||
};
|
||||
}
|
||||
/* Procedure FLAGS */
|
||||
enum PROC_FLAGS
|
||||
{
|
||||
@ -107,9 +88,9 @@ public:
|
||||
}
|
||||
void push_back(BB *v) { m_listBB.push_back(v);}
|
||||
};
|
||||
struct Function : public llvm::ilist_node<Function>
|
||||
struct Function
|
||||
{
|
||||
typedef llvm::iplist<BB> BasicBlockListType;
|
||||
typedef std::list<BB *> BasicBlockListType;
|
||||
// BasicBlock iterators...
|
||||
typedef BasicBlockListType::iterator iterator;
|
||||
typedef BasicBlockListType::const_iterator const_iterator;
|
||||
@ -119,14 +100,14 @@ protected:
|
||||
hasCase(false),liveAnal(0)
|
||||
{
|
||||
type = new FunctionType;
|
||||
callingConv(CConv::UNKNOWN);
|
||||
callingConv(CConv::eUnknown);
|
||||
}
|
||||
|
||||
public:
|
||||
FunctionType * type;
|
||||
CConv * m_call_conv;
|
||||
uint32_t procEntry; /* label number */
|
||||
std::string name; /* Meaningful name for this proc */
|
||||
QString name; /* Meaningful name for this proc */
|
||||
STATE state; /* Entry state */
|
||||
int depth; /* Depth at which we found it - for printing */
|
||||
uint32_t flg; /* Combination of Icode & Proc flags */
|
||||
@ -153,7 +134,7 @@ public:
|
||||
delete type;
|
||||
}
|
||||
public:
|
||||
static Function *Create(FunctionType *ty=0,int /*Linkage*/=0,const std::string &nm="",void */*module*/=0)
|
||||
static Function *Create(FunctionType *ty=0,int /*Linkage*/=0,const QString &nm="",void */*module*/=0)
|
||||
{
|
||||
Function *r=new Function(ty);
|
||||
r->name = nm;
|
||||
@ -185,14 +166,14 @@ public:
|
||||
bool process_JMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
|
||||
bool process_CALL(ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
|
||||
void freeCFG();
|
||||
void codeGen(std::ostream &fs);
|
||||
void codeGen(QIODevice & fs);
|
||||
void mergeFallThrough(BB *pBB);
|
||||
void structIfs();
|
||||
void structLoops(derSeq *derivedG);
|
||||
void buildCFG(Disassembler &ds);
|
||||
void controlFlowAnalysis();
|
||||
void newRegArg(iICODE picode, iICODE ticode);
|
||||
void writeProcComments(std::ostream &ostr);
|
||||
void writeProcComments(QTextStream & ostr);
|
||||
|
||||
void displayCFG();
|
||||
void displayStats();
|
||||
@ -200,7 +181,7 @@ public:
|
||||
|
||||
void preprocessReturnDU(LivenessSet &_liveOut);
|
||||
Expr * adjustActArgType(Expr *_exp, hlType forType);
|
||||
std::string writeCall(Function *tproc, STKFRAME &args, int *numLoc);
|
||||
QString writeCall(Function *tproc, STKFRAME &args, int *numLoc);
|
||||
void processDosInt(STATE *pstate, PROG &prog, bool done);
|
||||
ICODE *translate_DIV(LLInst *ll, ICODE &_Icode);
|
||||
ICODE *translate_XCHG(LLInst *ll, ICODE &_Icode);
|
||||
@ -233,26 +214,12 @@ protected:
|
||||
void genLiveKtes();
|
||||
bool findDerivedSeq(derSeq &derivedGi);
|
||||
bool nextOrderGraph(derSeq &derivedGi);
|
||||
};
|
||||
namespace llvm {
|
||||
template<> struct ilist_traits<typename ::Function>
|
||||
: public ilist_default_traits<typename ::Function> {
|
||||
|
||||
// createSentinel is used to get hold of the node that marks the end of the
|
||||
// list... (same trick used here as in ilist_traits<Instruction>)
|
||||
typename ::Function *createSentinel() const {
|
||||
return static_cast<typename ::Function*>(&Sentinel);
|
||||
}
|
||||
static void destroySentinel(typename ::Function*) {}
|
||||
|
||||
typename ::Function *provideInitialHead() const { return createSentinel(); }
|
||||
typename ::Function *ensureHead(::Function*) const { return createSentinel(); }
|
||||
static void noteHead(typename ::Function*, typename ::Function*) {}
|
||||
void addOutEdgesForConditionalJump(BB* pBB, int next_ip, LLInst *ll);
|
||||
|
||||
private:
|
||||
mutable ilist_node<typename ::Function> Sentinel;
|
||||
bool decodeIndirectJMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
|
||||
bool decodeIndirectJMP2(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
|
||||
};
|
||||
}
|
||||
typedef llvm::iplist<Function> FunctionListType;
|
||||
typedef std::list<Function> FunctionListType;
|
||||
typedef FunctionListType lFunction;
|
||||
typedef lFunction::iterator ilFunction;
|
||||
|
||||
@ -5,11 +5,15 @@
|
||||
* (C) Cristina Cifuentes
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Enums.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <stdint.h>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include "Enums.h"
|
||||
|
||||
static const int operandSize=20;
|
||||
/* The following definitions and types define the Conditional Expression
|
||||
* attributed syntax tree, as defined by the following EBNF:
|
||||
@ -54,7 +58,7 @@ public:
|
||||
/** Recursively deallocates the abstract syntax tree rooted at *exp */
|
||||
virtual ~Expr() {}
|
||||
public:
|
||||
virtual std::string walkCondExpr (Function * pProc, int* numLoc) const=0;
|
||||
virtual QString walkCondExpr (Function * pProc, int* numLoc) const=0;
|
||||
virtual Expr *inverse() const=0; // return new COND_EXPR that is invarse of this
|
||||
virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId)=0;
|
||||
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)=0;
|
||||
@ -96,10 +100,12 @@ struct UnaryOperator : public Expr
|
||||
}
|
||||
public:
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
virtual QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
|
||||
virtual hlType expType(Function *pproc) const;
|
||||
virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
|
||||
private:
|
||||
QString wrapUnary(Function *pProc, int *numLoc, QChar op) const;
|
||||
};
|
||||
|
||||
struct BinaryOperator : public Expr
|
||||
@ -120,7 +126,7 @@ struct BinaryOperator : public Expr
|
||||
}
|
||||
~BinaryOperator()
|
||||
{
|
||||
assert(m_lhs!=m_rhs || m_lhs==nullptr);
|
||||
assert(m_lhs!=m_rhs or m_lhs==nullptr);
|
||||
delete m_lhs;
|
||||
delete m_rhs;
|
||||
m_lhs=m_rhs=nullptr;
|
||||
@ -184,7 +190,7 @@ struct BinaryOperator : public Expr
|
||||
condOp op() const { return m_op;}
|
||||
/* Changes the boolean conditional operator at the root of this expression */
|
||||
void op(condOp o) { m_op=o;}
|
||||
std::string walkCondExpr (Function * pProc, int* numLoc) const;
|
||||
QString walkCondExpr(Function * pProc, int* numLoc) const;
|
||||
public:
|
||||
hlType expType(Function *pproc) const;
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
@ -211,13 +217,10 @@ struct AstIdent : public UnaryOperator
|
||||
virtual int hlTypeSize(Function *pproc) const;
|
||||
virtual hlType expType(Function *pproc) const;
|
||||
virtual Expr * performLongRemoval(eReg regi, LOCAL_ID *locId);
|
||||
virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
virtual QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
|
||||
virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
|
||||
virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
|
||||
protected:
|
||||
eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl);
|
||||
|
||||
};
|
||||
struct GlobalVariable : public AstIdent
|
||||
{
|
||||
@ -228,7 +231,7 @@ struct GlobalVariable : public AstIdent
|
||||
return new GlobalVariable(*this);
|
||||
}
|
||||
GlobalVariable(int16_t segValue, int16_t off);
|
||||
std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
hlType expType(Function *pproc) const;
|
||||
};
|
||||
@ -242,7 +245,7 @@ struct GlobalVariableIdx : public AstIdent
|
||||
return new GlobalVariableIdx(*this);
|
||||
}
|
||||
GlobalVariableIdx(int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym);
|
||||
std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
hlType expType(Function *pproc) const;
|
||||
};
|
||||
@ -264,9 +267,9 @@ struct Constant : public AstIdent
|
||||
{
|
||||
return new Constant(*this);
|
||||
}
|
||||
std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
hlType expType(Function *pproc) const;
|
||||
hlType expType(Function *pproc) const { return TYPE_CONST; }
|
||||
};
|
||||
struct FuncNode : public AstIdent
|
||||
{
|
||||
@ -284,7 +287,7 @@ struct FuncNode : public AstIdent
|
||||
{
|
||||
return new FuncNode(*this);
|
||||
}
|
||||
std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
int hlTypeSize(Function *pproc) const;
|
||||
hlType expType(Function *pproc) const;
|
||||
};
|
||||
@ -310,7 +313,7 @@ struct RegisterNode : public AstIdent
|
||||
{
|
||||
return new RegisterNode(*this);
|
||||
}
|
||||
std::string walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
QString walkCondExpr(Function *pProc, int *numLoc) const;
|
||||
int hlTypeSize(Function *) const;
|
||||
hlType expType(Function *pproc) const;
|
||||
bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
|
||||
|
||||
@ -7,8 +7,10 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
struct strTable : std::vector<std::string>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QIODevice>
|
||||
|
||||
struct strTable : std::vector<QString>
|
||||
{
|
||||
/* Returns the next available index into the table */
|
||||
size_t nextIdx() {return size();}
|
||||
@ -20,9 +22,9 @@ struct bundle
|
||||
{
|
||||
public:
|
||||
void appendCode(const char *format, ...);
|
||||
void appendCode(const std::string &s);
|
||||
void appendCode(const QString &s);
|
||||
void appendDecl(const char *format, ...);
|
||||
void appendDecl(const std::string &);
|
||||
void appendDecl(const QString &);
|
||||
void init()
|
||||
{
|
||||
decl.clear();
|
||||
@ -37,6 +39,6 @@ extern bundle cCode;
|
||||
#define lineSize 360 /* 3 lines in the mean time */
|
||||
|
||||
//void newBundle (bundle *procCode);
|
||||
void writeBundle (std::ostream &ios, bundle procCode);
|
||||
void writeBundle (QIODevice & ios, bundle procCode);
|
||||
void freeBundle (bundle *procCode);
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
#pragma once
|
||||
//TODO: Remove boolT
|
||||
|
||||
#include <llvm/ADT/ilist.h>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
@ -29,24 +28,25 @@ extern bundle cCode; /* Output C procedure's declaration and code */
|
||||
|
||||
extern QString asm1_name, asm2_name; /* Assembler output filenames */
|
||||
|
||||
typedef struct { /* Command line option flags */
|
||||
unsigned verbose : 1;
|
||||
unsigned VeryVerbose : 1;
|
||||
unsigned asm1 : 1; /* Early disassembly listing */
|
||||
unsigned asm2 : 1; /* Disassembly listing after restruct */
|
||||
unsigned Map : 1;
|
||||
unsigned Stats : 1;
|
||||
unsigned Interact : 1; /* Interactive mode */
|
||||
unsigned Calls : 1; /* Follow register indirect calls */
|
||||
/** Command line option flags */
|
||||
struct OPTION
|
||||
{
|
||||
bool verbose;
|
||||
bool VeryVerbose;
|
||||
bool asm1; /* Early disassembly listing */
|
||||
bool asm2; /* Disassembly listing after restruct */
|
||||
bool Map;
|
||||
bool Stats;
|
||||
bool Interact; /* Interactive mode */
|
||||
bool Calls; /* Follow register indirect calls */
|
||||
QString filename; /* The input filename */
|
||||
} OPTION;
|
||||
uint32_t CustomEntryPoint;
|
||||
};
|
||||
|
||||
extern OPTION option; /* Command line options */
|
||||
|
||||
#include "BinaryImage.h"
|
||||
|
||||
|
||||
|
||||
/* Memory map states */
|
||||
enum eAreaType
|
||||
{
|
||||
@ -87,20 +87,18 @@ void interactDis(Function *, int initIC); /* disassem.c */
|
||||
bool JmpInst(llIcode opcode); /* idioms.c */
|
||||
queue::iterator appendQueue(queue &Q, BB *node); /* reducible.c */
|
||||
|
||||
void SetupLibCheck(void); /* chklib.c */
|
||||
bool SetupLibCheck(void); /* chklib.c */
|
||||
void CleanupLibCheck(void); /* chklib.c */
|
||||
bool LibCheck(Function &p); /* chklib.c */
|
||||
|
||||
|
||||
/* Exported functions from hlicode.c */
|
||||
const char *writeJcond(const HLTYPE &, Function *, int *);
|
||||
const char *writeJcondInv (HLTYPE, Function *, int *);
|
||||
QString writeJcond(const HLTYPE &, Function *, int *);
|
||||
QString writeJcondInv(HLTYPE, Function *, int *);
|
||||
|
||||
|
||||
/* Exported funcions from locident.c */
|
||||
bool checkLongEq(LONG_STKID_TYPE, iICODE, int, Function *, Assignment &asgn, LLInst &atOffset);
|
||||
bool checkLongRegEq(LONGID_TYPE, iICODE, int, Function *, Assignment &asgn, LLInst &);
|
||||
eReg otherLongRegi(eReg, int, LOCAL_ID *);
|
||||
|
||||
|
||||
extern const char *indentStr(int level);
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QDir>
|
||||
#include <llvm/ADT/ilist.h>
|
||||
|
||||
class IXmlTarget;
|
||||
|
||||
|
||||
@ -5,17 +5,22 @@
|
||||
***************************************************************************
|
||||
*/
|
||||
#pragma once
|
||||
#include "bundle.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include "bundle.h"
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
struct LLInst;
|
||||
struct Function;
|
||||
struct Disassembler
|
||||
{
|
||||
protected:
|
||||
int pass;
|
||||
int g_lab;
|
||||
//bundle &cCode;
|
||||
std::ofstream m_fp;
|
||||
QIODevice *m_disassembly_target;
|
||||
QTextStream m_fp;
|
||||
std::vector<std::string> m_decls;
|
||||
std::vector<std::string> m_code;
|
||||
|
||||
|
||||
105
include/icode.h
105
include/icode.h
@ -3,6 +3,16 @@
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
#pragma once
|
||||
#include "msvc_fixes.h"
|
||||
#include "BinaryImage.h"
|
||||
#include "libdis.h"
|
||||
#include "Enums.h"
|
||||
#include "state.h" // State depends on INDEXBASE, but later need STATE
|
||||
#include "CallConvention.h"
|
||||
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include <QtCore/QString>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
@ -10,15 +20,6 @@
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <initializer_list>
|
||||
#include <llvm/ADT/ilist.h>
|
||||
#include <llvm/ADT/ilist_node.h>
|
||||
#include <llvm/MC/MCInst.h>
|
||||
#include <llvm/IR/Instruction.h>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
#include "libdis.h"
|
||||
#include "Enums.h"
|
||||
#include "state.h" // State depends on INDEXBASE, but later need STATE
|
||||
#include "CallConvention.h"
|
||||
|
||||
//enum condId;
|
||||
|
||||
@ -58,12 +59,7 @@ public:
|
||||
}
|
||||
friend void swap(LivenessSet& first, LivenessSet& second) // nothrow
|
||||
{
|
||||
// enable ADL (not necessary in our case, but good practice)
|
||||
using std::swap;
|
||||
|
||||
// by swapping the members of two classes,
|
||||
// the two classes are effectively swapped
|
||||
swap(first.registers, second.registers);
|
||||
std::swap(first.registers, second.registers);
|
||||
}
|
||||
LivenessSet &operator|=(const LivenessSet &other)
|
||||
{
|
||||
@ -142,7 +138,7 @@ struct HlTypeSupport
|
||||
{
|
||||
//hlIcode opcode; /* hlIcode opcode */
|
||||
virtual bool removeRegFromLong(eReg regi, LOCAL_ID *locId)=0;
|
||||
virtual std::string writeOut(Function *pProc, int *numLoc) const=0;
|
||||
virtual QString writeOut(Function *pProc, int *numLoc) const=0;
|
||||
protected:
|
||||
Expr * performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree);
|
||||
};
|
||||
@ -159,10 +155,10 @@ struct CallType : public HlTypeSupport
|
||||
public:
|
||||
bool removeRegFromLong(eReg /*regi*/, LOCAL_ID * /*locId*/)
|
||||
{
|
||||
printf("CallType : removeRegFromLong not supproted");
|
||||
printf("CallType : removeRegFromLong not supproted\n");
|
||||
return false;
|
||||
}
|
||||
std::string writeOut(Function *pProc, int *numLoc) const;
|
||||
QString writeOut(Function *pProc, int *numLoc) const;
|
||||
};
|
||||
struct AssignType : public HlTypeSupport
|
||||
{
|
||||
@ -170,24 +166,24 @@ struct AssignType : public HlTypeSupport
|
||||
protected:
|
||||
public:
|
||||
Expr * m_lhs;
|
||||
Expr *rhs;
|
||||
Expr * m_rhs;
|
||||
AssignType() {}
|
||||
Expr *lhs() const {return m_lhs;}
|
||||
void lhs(Expr *l);
|
||||
bool removeRegFromLong(eReg regi, LOCAL_ID *locId);
|
||||
std::string writeOut(Function *pProc, int *numLoc) const;
|
||||
QString writeOut(Function *pProc, int *numLoc) const;
|
||||
};
|
||||
struct ExpType : public HlTypeSupport
|
||||
{
|
||||
/* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
|
||||
Expr * v;
|
||||
ExpType() : v(0) {}
|
||||
ExpType() : v(nullptr) {}
|
||||
bool removeRegFromLong(eReg regi, LOCAL_ID *locId)
|
||||
{
|
||||
v=performLongRemoval(regi,locId,v);
|
||||
return true;
|
||||
}
|
||||
std::string writeOut(Function *pProc, int *numLoc) const;
|
||||
QString writeOut(Function *pProc, int *numLoc) const;
|
||||
};
|
||||
|
||||
struct HLTYPE
|
||||
@ -241,7 +237,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
public:
|
||||
std::string write1HlIcode(Function *pProc, int *numLoc) const;
|
||||
QString write1HlIcode(Function *pProc, int *numLoc) const;
|
||||
void setAsgn(Expr *lhs, Expr *rhs);
|
||||
} ;
|
||||
/* LOW_LEVEL icode operand record */
|
||||
@ -275,12 +271,12 @@ struct LLOperand
|
||||
}
|
||||
bool operator==(const LLOperand &with) const
|
||||
{
|
||||
return (seg==with.seg) &&
|
||||
(segOver==with.segOver) &&
|
||||
(segValue==with.segValue) &&
|
||||
(regi == with.regi) &&
|
||||
(off == with.off) &&
|
||||
(opz==with.opz) &&
|
||||
return (seg==with.seg) and
|
||||
(segOver==with.segOver) and
|
||||
(segValue==with.segValue) and
|
||||
(regi == with.regi) and
|
||||
(off == with.off) and
|
||||
(opz==with.opz) and
|
||||
(proc.proc==with.proc.proc);
|
||||
}
|
||||
int64_t getImm2() const {return opz;}
|
||||
@ -314,9 +310,10 @@ struct LLOperand
|
||||
bool compound() const {return is_compound;} // dx:ax pair
|
||||
size_t byteWidth() const { assert(width<=4); return width;}
|
||||
};
|
||||
struct LLInst : public llvm::MCInst //: public llvm::ilist_node<LLInst>
|
||||
struct LLInst
|
||||
{
|
||||
protected:
|
||||
llIcode m_opcode; // Low level opcode identifier
|
||||
uint32_t flg; /* icode flags */
|
||||
LLOperand m_src; /* source operand */
|
||||
public:
|
||||
@ -328,9 +325,12 @@ public:
|
||||
int caseEntry;
|
||||
std::vector<uint32_t> caseTbl2;
|
||||
int hllLabNum; /* label # for hll codegen */
|
||||
|
||||
llIcode getOpcode() const { return m_opcode;}
|
||||
void setOpcode(uint32_t op) { m_opcode=(llIcode)op; }
|
||||
bool conditionalJump()
|
||||
{
|
||||
return (getOpcode() >= iJB) && (getOpcode() < iJCXZ);
|
||||
return (getOpcode() >= iJB) and (getOpcode() < iJCXZ);
|
||||
}
|
||||
bool testFlags(uint32_t x) const { return (flg & x)!=0;}
|
||||
void setFlags(uint32_t flag) {flg |= flag;}
|
||||
@ -366,11 +366,11 @@ public:
|
||||
}
|
||||
bool match(llIcode op,eReg dest,eReg src_reg)
|
||||
{
|
||||
return (getOpcode()==op)&&(m_dst.regi==dest)&&(m_src.regi==src_reg);
|
||||
return (getOpcode()==op) and (m_dst.regi==dest) and (m_src.regi==src_reg);
|
||||
}
|
||||
bool match(eReg dest,eReg src_reg)
|
||||
{
|
||||
return (m_dst.regi==dest)&&(m_src.regi==src_reg);
|
||||
return (m_dst.regi==dest) and (m_src.regi==src_reg);
|
||||
}
|
||||
bool match(eReg dest)
|
||||
{
|
||||
@ -400,11 +400,11 @@ public:
|
||||
}
|
||||
void emitGotoLabel(int indLevel);
|
||||
void findJumpTargets(CIcodeRec &_pc);
|
||||
void writeIntComment(std::ostringstream &s);
|
||||
void writeIntComment(QTextStream & s);
|
||||
void dis1Line(int loc_ip, int pass);
|
||||
std::ostringstream &strSrc(std::ostringstream &os,bool skip_comma=false);
|
||||
QTextStream & strSrc(QTextStream & os, bool skip_comma=false);
|
||||
|
||||
void flops(std::ostringstream &out);
|
||||
void flops(QTextStream & out);
|
||||
bool isJmpInst();
|
||||
HLTYPE createCall();
|
||||
LLInst(ICODE *container) : flg(0),codeIdx(0),numBytes(0),m_link(container)
|
||||
@ -460,17 +460,17 @@ public:
|
||||
template<int TYPE>
|
||||
struct TypeFilter
|
||||
{
|
||||
bool operator()(ICODE *ic) {return ic->type==HIGH_LEVEL;}
|
||||
bool operator()(ICODE &ic) {return ic.type==HIGH_LEVEL;}
|
||||
bool operator()(ICODE *ic) {return ic->type==TYPE;}
|
||||
bool operator()(ICODE &ic) {return ic.type==TYPE;}
|
||||
};
|
||||
template<int TYPE>
|
||||
struct TypeAndValidFilter
|
||||
{
|
||||
bool operator()(ICODE *ic) {return (ic->type==HIGH_LEVEL)&&(ic->valid());}
|
||||
bool operator()(ICODE &ic) {return (ic.type==HIGH_LEVEL)&&ic.valid();}
|
||||
bool operator()(ICODE *ic) {return (ic->type==TYPE) and (ic->valid());}
|
||||
bool operator()(ICODE &ic) {return (ic.type==TYPE) and ic.valid();}
|
||||
};
|
||||
static TypeFilter<HIGH_LEVEL> select_high_level;
|
||||
static TypeAndValidFilter<HIGH_LEVEL> select_valid_high_level;
|
||||
static TypeFilter<HIGH_LEVEL_ICODE> select_high_level;
|
||||
static TypeAndValidFilter<HIGH_LEVEL_ICODE> select_valid_high_level;
|
||||
/* Def/Use of registers and stack variables */
|
||||
struct DU_ICODE
|
||||
{
|
||||
@ -506,7 +506,7 @@ public:
|
||||
if(iter==uses.end())
|
||||
return;
|
||||
uses.erase(iter);
|
||||
assert("Same user more then once!" && uses.end()==std::find(uses.begin(),uses.end(),us));
|
||||
assert("Same user more then once!" and uses.end()==std::find(uses.begin(),uses.end(),us));
|
||||
}
|
||||
|
||||
};
|
||||
@ -572,7 +572,7 @@ public:
|
||||
// set this icode to be an assign
|
||||
void setAsgn(Expr *lhs, Expr *rhs)
|
||||
{
|
||||
type=HIGH_LEVEL;
|
||||
type=HIGH_LEVEL_ICODE;
|
||||
hlU()->setAsgn(lhs,rhs);
|
||||
}
|
||||
void setUnary(hlIcode op, Expr *_exp);
|
||||
@ -589,7 +589,7 @@ public:
|
||||
{
|
||||
return hlU()->call.newStkArg(exp,opcode,pproc);
|
||||
}
|
||||
ICODE() : m_ll(this),Parent(0),invalid(false),type(NOT_SCANNED),loc_ip(0)
|
||||
ICODE() : m_ll(this),Parent(0),invalid(false),type(NOT_SCANNED_ICODE),loc_ip(0)
|
||||
{
|
||||
}
|
||||
public:
|
||||
@ -600,14 +600,13 @@ public:
|
||||
};
|
||||
/** Map n low level instructions to m high level instructions
|
||||
*/
|
||||
struct MappingLLtoML
|
||||
{
|
||||
typedef llvm::iplist<llvm::Instruction> InstListType;
|
||||
typedef boost::iterator_range<iICODE> rSourceRange;
|
||||
typedef boost::iterator_range<InstListType::iterator> rTargetRange;
|
||||
rSourceRange m_low_level;
|
||||
rTargetRange m_middle_level;
|
||||
};
|
||||
//struct MappingLLtoML
|
||||
//{
|
||||
// typedef boost::iterator_range<iICODE> rSourceRange;
|
||||
// typedef boost::iterator_range<InstListType::iterator> rTargetRange;
|
||||
// rSourceRange m_low_level;
|
||||
// rTargetRange m_middle_level;
|
||||
//};
|
||||
// This is the icode array object.
|
||||
class CIcodeRec : public std::list<ICODE>
|
||||
{
|
||||
|
||||
@ -6,14 +6,17 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "msvc_fixes.h"
|
||||
#include "types.h"
|
||||
#include "Enums.h"
|
||||
#include "machine_x86.h"
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include "types.h"
|
||||
#include "Enums.h"
|
||||
#include "machine_x86.h"
|
||||
|
||||
/* Type definition */
|
||||
// this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
|
||||
@ -25,7 +28,7 @@ struct LLInst;
|
||||
typedef std::list<ICODE>::iterator iICODE;
|
||||
struct IDX_ARRAY : public std::vector<iICODE>
|
||||
{
|
||||
bool inList(iICODE idx)
|
||||
bool inList(iICODE idx) const
|
||||
{
|
||||
return std::find(begin(),end(),idx)!=end();
|
||||
}
|
||||
@ -93,20 +96,19 @@ protected:
|
||||
LONGID_TYPE m_longId; /* For TYPE_LONG_(UN)SIGN registers */
|
||||
public:
|
||||
hlType type; /* Probable type */
|
||||
bool illegal; /* Boolean: not a valid field any more */
|
||||
//std::vector<iICODE> idx;
|
||||
IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */
|
||||
frameType loc; /* Frame location */
|
||||
bool illegal; /* Boolean: not a valid field any more */
|
||||
bool hasMacro; /* Identifier requires a macro */
|
||||
char macro[10]; /* Macro for this identifier */
|
||||
std::string name; /* Identifier's name */
|
||||
QString name; /* Identifier's name */
|
||||
union ID_UNION { /* Different types of identifiers */
|
||||
friend struct ID;
|
||||
protected:
|
||||
LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
|
||||
public:
|
||||
eReg regi; /* For TYPE_BYTE(uint16_t)_(UN)SIGN registers */
|
||||
struct { /* For TYPE_BYTE(uint16_t)_(UN)SIGN on the stack */
|
||||
eReg regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */
|
||||
struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */
|
||||
uint8_t regOff; /* register offset (if any) */
|
||||
int off; /* offset from BP */
|
||||
} bwId;
|
||||
@ -118,27 +120,30 @@ public:
|
||||
} longKte;
|
||||
ID_UNION() { /*new (&longStkId) LONG_STKID_TYPE();*/}
|
||||
} id;
|
||||
LONGID_TYPE & longId() {assert(isLong() && loc==REG_FRAME); return m_longId;}
|
||||
const LONGID_TYPE & longId() const {assert(isLong() && loc==REG_FRAME); return m_longId;}
|
||||
LONG_STKID_TYPE & longStkId() {assert(isLong() && loc==STK_FRAME); return id.longStkId;}
|
||||
const LONG_STKID_TYPE & longStkId() const {assert(isLong() && loc==STK_FRAME); return id.longStkId;}
|
||||
|
||||
LONGID_TYPE & longId() {assert(isLong() and loc==REG_FRAME); return m_longId;}
|
||||
const LONGID_TYPE & longId() const {assert(isLong() and loc==REG_FRAME); return m_longId;}
|
||||
LONG_STKID_TYPE & longStkId() {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
|
||||
const LONG_STKID_TYPE & longStkId() const {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
|
||||
ID();
|
||||
ID(hlType t, frameType f);
|
||||
ID(hlType t, const LONGID_TYPE &s);
|
||||
ID(hlType t, const LONG_STKID_TYPE &s);
|
||||
ID(hlType t, const LONGGLB_TYPE &s);
|
||||
bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);}
|
||||
bool isSigned() const { return (type==TYPE_BYTE_SIGN) or (type==TYPE_WORD_SIGN) or (type==TYPE_LONG_SIGN);}
|
||||
uint16_t typeBitsize() const
|
||||
{
|
||||
return TypeContainer::typeSize(type)*8;
|
||||
}
|
||||
bool isLong() const { return (type==TYPE_LONG_UNSIGN)||(type==TYPE_LONG_SIGN); }
|
||||
bool isLong() const { return (type==TYPE_LONG_UNSIGN) or (type==TYPE_LONG_SIGN); }
|
||||
void setLocalName(int i)
|
||||
{
|
||||
char buf[32];
|
||||
sprintf (buf, "loc%d", i);
|
||||
name=buf;
|
||||
}
|
||||
bool isLongRegisterPair() const { return (loc == REG_FRAME) and isLong();}
|
||||
eReg getPairedRegister(eReg first) const;
|
||||
};
|
||||
|
||||
struct LOCAL_ID
|
||||
@ -164,12 +169,13 @@ public:
|
||||
int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
|
||||
void newIdent(hlType t, frameType f);
|
||||
void flagByteWordId(int off);
|
||||
void propLongId(uint8_t regL, uint8_t regH, const char *name);
|
||||
void propLongId(uint8_t regL, uint8_t regH, const QString & name);
|
||||
size_t csym() const {return id_arr.size();}
|
||||
void newRegArg(iICODE picode, iICODE ticode) const;
|
||||
void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong) const;
|
||||
void forwardSubs(Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
|
||||
void newRegArg(ICODE & picode, ICODE & ticode) const;
|
||||
void processTargetIcode(ICODE & picode, int &numHlIcodes, ICODE & ticode, bool isLong) const;
|
||||
void forwardSubs(Expr *lhs, Expr *rhs, ICODE & picode, ICODE & ticode, int &numHlIcodes) const;
|
||||
AstIdent *createId(const ID *retVal, iICODE ix_);
|
||||
eReg getPairedRegisterAt(int idx,eReg first) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <bitset>
|
||||
|
||||
class QTextStream;
|
||||
|
||||
struct LivenessSet;
|
||||
/* Machine registers */
|
||||
enum eReg
|
||||
@ -60,13 +62,13 @@ class Machine_X86 : public SourceMachine
|
||||
public:
|
||||
Machine_X86();
|
||||
virtual ~Machine_X86() {}
|
||||
static const std::string ®Name(eReg r);
|
||||
static const std::string &opcodeName(unsigned r);
|
||||
static const std::string &floatOpName(unsigned r);
|
||||
static const QString & regName(eReg r);
|
||||
static const QString & opcodeName(unsigned r);
|
||||
static const QString & floatOpName(unsigned r);
|
||||
bool physicalReg(eReg r);
|
||||
/* Writes the registers that are set in the bitvector */
|
||||
//TODO: move this into Machine_X86 ?
|
||||
static void writeRegVector (std::ostream &ostr,const LivenessSet ®i);
|
||||
static void writeRegVector (QTextStream & ostr, const LivenessSet ®i);
|
||||
static eReg subRegH(eReg reg);
|
||||
static eReg subRegL(eReg reg);
|
||||
static bool isMemOff(eReg r);
|
||||
|
||||
3
include/msvc_fixes.h
Normal file
3
include/msvc_fixes.h
Normal file
@ -0,0 +1,3 @@
|
||||
#ifdef _MSC_VER
|
||||
#include <iso646.h>
|
||||
#endif
|
||||
@ -3,7 +3,6 @@
|
||||
#include <stdint.h>
|
||||
#include <cassert>
|
||||
#include <list>
|
||||
#include <llvm/ADT/ilist.h>
|
||||
#include <boost/icl/interval.hpp>
|
||||
#include <boost/icl/interval_map.hpp>
|
||||
#include <boost/icl/split_interval_map.hpp>
|
||||
@ -29,11 +28,11 @@ class Project : public IProject
|
||||
QString m_output_path;
|
||||
public:
|
||||
|
||||
typedef llvm::iplist<Function> FunctionListType;
|
||||
typedef std::list<Function> FunctionListType;
|
||||
typedef FunctionListType lFunction;
|
||||
typedef FunctionListType::iterator ilFunction;
|
||||
SYMTAB symtab; /* Global symbol table */
|
||||
|
||||
SYMTAB symtab; /* Global symbol table */
|
||||
FunctionListType pProcList;
|
||||
CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
|
||||
PROG prog; /* Loaded program image parameters */
|
||||
@ -52,14 +51,14 @@ public:
|
||||
QString output_name(const char *ext);
|
||||
ilFunction funcIter(Function *to_find);
|
||||
ilFunction findByEntry(uint32_t entry);
|
||||
ilFunction createFunction(FunctionType *f,const std::string &name);
|
||||
ilFunction createFunction(FunctionType *f, const QString & name);
|
||||
bool valid(ilFunction iter);
|
||||
|
||||
int getSymIdxByAdd(uint32_t adr);
|
||||
int getSymIdxByAddr(uint32_t adr);
|
||||
bool validSymIdx(size_t idx);
|
||||
size_t symbolSize(size_t idx);
|
||||
hlType symbolType(size_t idx);
|
||||
const std::string &symbolName(size_t idx);
|
||||
const QString & symbolName(size_t idx);
|
||||
const SYM & getSymByIdx(size_t idx) const;
|
||||
|
||||
static Project * get();
|
||||
@ -67,6 +66,7 @@ public:
|
||||
SourceMachine *machine();
|
||||
|
||||
const FunctionListType &functions() const { return pProcList; }
|
||||
FunctionListType &functions() { return pProcList; }
|
||||
protected:
|
||||
void initialize();
|
||||
void writeGlobSymTable();
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
struct STATE
|
||||
{
|
||||
uint32_t IP; /* Offset into Image */
|
||||
int16_t r[INDEX_BX_SI]; /* Value of segs and AX */
|
||||
uint16_t r[INDEX_BX_SI]; /* Value of segs and AX */
|
||||
bool f[INDEX_BX_SI]; /* True if r[.] has a value */
|
||||
struct
|
||||
{ /* For case stmt indexed reg */
|
||||
|
||||
@ -3,10 +3,13 @@
|
||||
* (C) Mike van Emmerik
|
||||
*/
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "Enums.h"
|
||||
#include "types.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
struct Expr;
|
||||
struct AstIdent;
|
||||
struct TypeContainer;
|
||||
@ -15,7 +18,7 @@ struct TypeContainer;
|
||||
/* * * * * * * * * * * * * * * * * */
|
||||
struct SymbolCommon
|
||||
{
|
||||
std::string name; /* New name for this variable/symbol/argument */
|
||||
QString name; /* New name for this variable/symbol/argument */
|
||||
int size; /* Size/maximum size */
|
||||
hlType type; /* probable type */
|
||||
eDuVal duVal; /* DEF, USE, VAL */
|
||||
@ -41,7 +44,7 @@ struct STKSYM : public SymbolCommon
|
||||
tLabel label=0; /* Immediate off from BP (+:args, -:params) */
|
||||
uint8_t regOff=0; /* Offset is a register (e.g. SI, DI) */
|
||||
bool hasMacro=false; /* This type needs a macro */
|
||||
std::string macro; /* Macro name */
|
||||
QString macro; /* Macro name */
|
||||
bool invalid=false; /* Boolean: invalid entry in formal arg list*/
|
||||
void setArgName(int i)
|
||||
{
|
||||
@ -91,7 +94,7 @@ struct SYMTABLE
|
||||
{
|
||||
// does not yse pSymName, to ease finding by symOff/symProc combo
|
||||
// in map<SYMTABLE,X>
|
||||
return (symOff==other.symOff) && symProc==(other.symProc);
|
||||
return (symOff==other.symOff) and symProc==(other.symProc);
|
||||
}
|
||||
};
|
||||
|
||||
@ -104,6 +107,6 @@ constexpr int NUM_TABLE_TYPES = int(Comment)+1; /* Number of entries: must be la
|
||||
|
||||
void createSymTables(void);
|
||||
void destroySymTables(void);
|
||||
bool readVal (std::ostringstream &symName, uint32_t symOff, Function *symProc);
|
||||
bool readVal (QTextStream & symName, uint32_t symOff, Function *symProc);
|
||||
void selectTable(tableType); /* Select a particular table */
|
||||
|
||||
|
||||
@ -5,9 +5,13 @@
|
||||
***************************************************************************
|
||||
*/
|
||||
#pragma once
|
||||
#include "Enums.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <stdint.h>
|
||||
#include "Enums.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/**** Common definitions and macros ****/
|
||||
#define MAX 0x7FFFFFFF
|
||||
|
||||
@ -64,12 +68,22 @@ struct eDuVal
|
||||
use = x&USE;
|
||||
val = x&VAL;
|
||||
}
|
||||
bool isUSE_VAL() {return use&&val;} //Use and Val
|
||||
bool isUSE_VAL() {return use and val;} //Use and Val
|
||||
};
|
||||
static constexpr const char * hlTypes[13] = {
|
||||
"", "char", "unsigned char", "int", "unsigned int",
|
||||
"long", "unsigned long", "record", "int *", "char *",
|
||||
"", "float", "double"
|
||||
"",
|
||||
"char",
|
||||
"unsigned char",
|
||||
"int",
|
||||
"unsigned int",
|
||||
"long",
|
||||
"unsigned long",
|
||||
"record",
|
||||
"int *",
|
||||
"char *",
|
||||
"",
|
||||
"float",
|
||||
"double"
|
||||
};
|
||||
|
||||
struct TypeContainer
|
||||
|
||||
@ -1,11 +1,16 @@
|
||||
#include "BasicBlock.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "Procedure.h"
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <QtCore/QTextStream>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <boost/range/rbegin.hpp>
|
||||
#include <boost/range/rend.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
#include "BasicBlock.h"
|
||||
#include "Procedure.h"
|
||||
#include "dcc.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
@ -139,7 +144,8 @@ ICODE* BB::writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&lat
|
||||
if(loopType == eNodeHeaderType::NO_TYPE)
|
||||
return nullptr;
|
||||
latch = pProc->m_dfsLast[this->latchNode];
|
||||
std::ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
QTextStream ostr(&ostr_contents);
|
||||
ICODE* picode;
|
||||
switch (loopType)
|
||||
{
|
||||
@ -165,7 +171,7 @@ ICODE* BB::writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&lat
|
||||
picode->hlU()->replaceExpr(picode->hl()->expr()->inverse());
|
||||
}
|
||||
{
|
||||
string e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
|
||||
QString e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
|
||||
ostr << "\n"<<indentStr(indLevel)<<"while ("<<e<<") {\n";
|
||||
}
|
||||
picode->invalidate();
|
||||
@ -182,27 +188,28 @@ ICODE* BB::writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&lat
|
||||
picode = &latch->back();
|
||||
break;
|
||||
}
|
||||
cCode.appendCode(ostr.str());
|
||||
ostr.flush();
|
||||
cCode.appendCode(ostr_contents);
|
||||
stats.numHLIcode += 1;
|
||||
indLevel++;
|
||||
return picode;
|
||||
}
|
||||
bool BB::isEndOfPath(int latch_node_idx) const
|
||||
{
|
||||
return nodeType == RETURN_NODE || nodeType == TERMINATE_NODE ||
|
||||
nodeType == NOWHERE_NODE || (dfsLastNum == latch_node_idx);
|
||||
return nodeType == RETURN_NODE or nodeType == TERMINATE_NODE or
|
||||
nodeType == NOWHERE_NODE or dfsLastNum == latch_node_idx;
|
||||
}
|
||||
void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode, int _ifFollow)
|
||||
{
|
||||
int follow; /* ifFollow */
|
||||
BB * succ, *latch; /* Successor and latching node */
|
||||
ICODE * picode; /* Pointer to HLI_JCOND instruction */
|
||||
std::string l; /* Pointer to HLI_JCOND expression */
|
||||
QString l; /* Pointer to HLI_JCOND expression */
|
||||
bool emptyThen, /* THEN clause is empty */
|
||||
repCond; /* Repeat condition for while() */
|
||||
|
||||
/* Check if this basic block should be analysed */
|
||||
if ((_ifFollow != UN_INIT) && (this == pProc->m_dfsLast[_ifFollow]))
|
||||
if ((_ifFollow != UN_INIT) and (this == pProc->m_dfsLast[_ifFollow]))
|
||||
return;
|
||||
|
||||
if (wasTraversedAtLevel(DFS_ALPHA))
|
||||
@ -217,9 +224,11 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
/* Write the code for this basic block */
|
||||
if (repCond == false)
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
QTextStream ostr(&ostr_contents);
|
||||
writeBB(ostr,indLevel, pProc, numLoc);
|
||||
cCode.appendCode(ostr.str());
|
||||
ostr.flush();
|
||||
cCode.appendCode(ostr_contents);
|
||||
}
|
||||
|
||||
/* Check for end of path */
|
||||
@ -250,7 +259,8 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
indLevel--;
|
||||
if (loopType == eNodeHeaderType::WHILE_TYPE)
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
QTextStream ostr(&ostr_contents);
|
||||
/* Check if there is need to repeat other statements involved
|
||||
* in while condition, then, emit the loop trailer */
|
||||
if (repCond)
|
||||
@ -258,13 +268,14 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
writeBB(ostr,indLevel+1, pProc, numLoc);
|
||||
}
|
||||
ostr <<indentStr(indLevel)<< "} /* end of while */\n";
|
||||
cCode.appendCode(ostr.str());
|
||||
ostr.flush();
|
||||
cCode.appendCode(ostr_contents);
|
||||
}
|
||||
else if (loopType == eNodeHeaderType::ENDLESS_TYPE)
|
||||
cCode.appendCode( "%s} /* end of loop */\n",indentStr(indLevel));
|
||||
else if (loopType == eNodeHeaderType::REPEAT_TYPE)
|
||||
{
|
||||
string e = "//*failed*//";
|
||||
QString e = "//*failed*//";
|
||||
if (picode->hl()->opcode != HLI_JCOND)
|
||||
{
|
||||
reportError (REPEAT_FAIL);
|
||||
@ -273,7 +284,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
{
|
||||
e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
|
||||
}
|
||||
cCode.appendCode( "%s} while (%s);\n", indentStr(indLevel),e.c_str());
|
||||
cCode.appendCode( "%s} while (%s);\n", indentStr(indLevel),qPrintable(e));
|
||||
}
|
||||
|
||||
/* Recurse on the loop follow */
|
||||
@ -305,13 +316,13 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
if (succ->dfsLastNum != follow) /* THEN part */
|
||||
{
|
||||
l = writeJcond ( *back().hl(), pProc, numLoc);
|
||||
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l.c_str());
|
||||
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), qPrintable(l));
|
||||
succ->writeCode (indLevel, pProc, numLoc, _latchNode,follow);
|
||||
}
|
||||
else /* empty THEN part => negate ELSE part */
|
||||
{
|
||||
l = writeJcondInv ( *back().hl(), pProc, numLoc);
|
||||
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l.c_str());
|
||||
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), qPrintable(l));
|
||||
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, follow);
|
||||
emptyThen = true;
|
||||
}
|
||||
@ -331,7 +342,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
}
|
||||
/* else (empty ELSE part) */
|
||||
}
|
||||
else if (! emptyThen) /* already visited => emit label */
|
||||
else if (not emptyThen) /* already visited => emit label */
|
||||
{
|
||||
cCode.appendCode( "%s}\n%selse {\n",
|
||||
indentStr(indLevel-1), indentStr(indLevel - 1));
|
||||
@ -347,7 +358,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
else /* no follow => if..then..else */
|
||||
{
|
||||
l = writeJcond ( *back().hl(), pProc, numLoc);
|
||||
cCode.appendCode( "%s%s", indentStr(indLevel-1), l.c_str());
|
||||
cCode.appendCode( "%s%s", indentStr(indLevel-1), qPrintable(l));
|
||||
edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
|
||||
cCode.appendCode( "%s}\n%selse {\n", indentStr(indLevel-1), indentStr(indLevel - 1));
|
||||
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
|
||||
@ -370,7 +381,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
|
||||
* Args: pBB: pointer to the current basic block.
|
||||
* Icode: pointer to the array of icodes for current procedure.
|
||||
* lev: indentation level - used for formatting. */
|
||||
void BB::writeBB(std::ostream &ostr,int lev, Function * pProc, int *numLoc)
|
||||
void BB::writeBB(QTextStream &ostr,int lev, Function * pProc, int *numLoc)
|
||||
{
|
||||
/* Save the index into the code table in case there is a later goto
|
||||
* into this instruction (first instruction of the BB) */
|
||||
@ -380,10 +391,10 @@ void BB::writeBB(std::ostream &ostr,int lev, Function * pProc, int *numLoc)
|
||||
|
||||
for(ICODE &pHli : instructions)
|
||||
{
|
||||
if ((pHli.type == HIGH_LEVEL) && ( pHli.valid() )) //TODO: use filtering range here.
|
||||
if ((pHli.type == HIGH_LEVEL_ICODE) and ( pHli.valid() )) //TODO: use filtering range here.
|
||||
{
|
||||
std::string line = pHli.hl()->write1HlIcode(pProc, numLoc);
|
||||
if (!line.empty())
|
||||
QString line = pHli.hl()->write1HlIcode(pProc, numLoc);
|
||||
if (not line.isEmpty())
|
||||
{
|
||||
ostr<<indentStr(lev)<<line;
|
||||
stats.numHLIcode++;
|
||||
|
||||
@ -1,36 +1,37 @@
|
||||
#include <ostream>
|
||||
#include <cassert>
|
||||
#include "CallConvention.h"
|
||||
#include <QtCore/QTextStream>
|
||||
|
||||
CConv *CConv::create(Type v)
|
||||
{
|
||||
static C_CallingConvention *c_call = nullptr;
|
||||
static Pascal_CallingConvention *p_call = nullptr;
|
||||
static Unknown_CallingConvention *u_call= nullptr;
|
||||
if(!c_call)
|
||||
if(nullptr==c_call)
|
||||
c_call = new C_CallingConvention;
|
||||
if(!p_call)
|
||||
if(nullptr==p_call)
|
||||
p_call = new Pascal_CallingConvention;
|
||||
if(!u_call)
|
||||
if(nullptr==u_call)
|
||||
u_call = new Unknown_CallingConvention;
|
||||
switch(v) {
|
||||
case UNKNOWN: return u_call;
|
||||
case C: return c_call;
|
||||
case PASCAL: return p_call;
|
||||
case eUnknown: return u_call;
|
||||
case eCdecl: return c_call;
|
||||
case ePascal: return p_call;
|
||||
}
|
||||
assert(false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void C_CallingConvention::writeComments(std::ostream &ostr)
|
||||
void C_CallingConvention::writeComments(QTextStream & ostr)
|
||||
{
|
||||
ostr << " * C calling convention.\n";
|
||||
}
|
||||
void Pascal_CallingConvention::writeComments(std::ostream &ostr)
|
||||
void Pascal_CallingConvention::writeComments(QTextStream & ostr)
|
||||
{
|
||||
ostr << " * Pascal calling convention.\n";
|
||||
}
|
||||
void Unknown_CallingConvention::writeComments(std::ostream &ostr)
|
||||
void Unknown_CallingConvention::writeComments(QTextStream & ostr)
|
||||
{
|
||||
ostr << " * Unknown calling convention.\n";
|
||||
}
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
#include "dcc.h"
|
||||
#include "DccFrontend.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "project.h"
|
||||
#include "disassem.h"
|
||||
#include "CallGraph.h"
|
||||
|
||||
#include <QtCore/QFileInfo>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
@ -64,7 +65,7 @@ void PROG::displayLoadInfo(void)
|
||||
int i;
|
||||
|
||||
printf("File type is %s\n", (fCOM)?"COM":"EXE");
|
||||
if (! fCOM) {
|
||||
if (not fCOM) {
|
||||
printf("Signature = %02X%02X\n", header.sigLo, header.sigHi);
|
||||
printf("File size %% 512 = %04X\n", LH(&header.lastPageSize));
|
||||
printf("File size / 512 = %04X pages\n", LH(&header.numPages));
|
||||
@ -73,11 +74,11 @@ void PROG::displayLoadInfo(void)
|
||||
printf("Minimum allocation = %04X paras\n", LH(&header.minAlloc));
|
||||
printf("Maximum allocation = %04X paras\n", LH(&header.maxAlloc));
|
||||
}
|
||||
printf("Load image size = %04" PRIiPTR "\n", cbImage - sizeof(PSP));
|
||||
printf("Load image size = %08lX\n", cbImage - sizeof(PSP));
|
||||
printf("Initial SS:SP = %04X:%04X\n", initSS, initSP);
|
||||
printf("Initial CS:IP = %04X:%04X\n", initCS, initIP);
|
||||
|
||||
if (option.VeryVerbose && cReloc)
|
||||
if (option.VeryVerbose and cReloc)
|
||||
{
|
||||
printf("\nRelocation Table\n");
|
||||
for (i = 0; i < cReloc; i++)
|
||||
@ -123,20 +124,20 @@ static void displayMemMap(void)
|
||||
fill(ip, b1);
|
||||
printf("%06X %s\n", ip, b1);
|
||||
ip += 16;
|
||||
for (i = 3, c = b1[1]; i < 32 && c == b1[i]; i += 2)
|
||||
for (i = 3, c = b1[1]; i < 32 and c == b1[i]; i += 2)
|
||||
; /* Check if all same */
|
||||
if (i > 32)
|
||||
{
|
||||
fill(ip, b2); /* Skip until next two are not same */
|
||||
fill(ip+16, b3);
|
||||
if (! (strcmp(b1, b2) || strcmp(b1, b3)))
|
||||
if (not (strcmp(b1, b2) || strcmp(b1, b3)))
|
||||
{
|
||||
printf(" :\n");
|
||||
do
|
||||
{
|
||||
ip += 16;
|
||||
fill(ip+16, b1);
|
||||
} while (! strcmp(b1, b2));
|
||||
} while (0==strcmp(b1, b2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -206,7 +207,7 @@ struct ComLoader : public DosLoader {
|
||||
fp.seek(0);
|
||||
char sig[2];
|
||||
if(2==fp.read(sig,2)) {
|
||||
return not (sig[0] == 0x4D && sig[1] == 0x5A);
|
||||
return not (sig[0] == 0x4D and sig[1] == 0x5A);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -229,7 +230,6 @@ struct ComLoader : public DosLoader {
|
||||
|
||||
prepareImage(prog, cb, fp);
|
||||
|
||||
|
||||
/* Set up memory map */
|
||||
cb = (prog.cbImage + 3) / 4;
|
||||
prog.map = (uint8_t *)malloc(cb);
|
||||
@ -237,6 +237,120 @@ struct ComLoader : public DosLoader {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
#if 0
|
||||
struct RomLoader {
|
||||
bool canLoad(QFile &fp) {
|
||||
fp.seek(0xFFF0);
|
||||
uint8_t sig[1];
|
||||
if(fp.read((char *)sig,1) == 1)
|
||||
{
|
||||
return (sig[0] == 0xEA);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool load(PROG &prog,QFile &fp) {
|
||||
printf("Loading ROM...\n");
|
||||
fp.seek(0);
|
||||
/* ROM file
|
||||
* In this case the load module size is just the file length
|
||||
*/
|
||||
auto cb = fp.size();
|
||||
|
||||
fp.seek(cb - 0x10);
|
||||
uint8_t buf[5];
|
||||
printf("Going to get CS/IP...\n");
|
||||
if(fp.read((char *)buf, 5) != 5)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
fp.seek(0);
|
||||
|
||||
/* ROM File, Hard to say where it is suppose to start, so try to trust the
|
||||
*/
|
||||
prog.initIP = (buf[2] << 8) | buf[1];
|
||||
//prog.initCS = 0;
|
||||
prog.initCS = (buf[4] << 8) | buf[3];
|
||||
|
||||
prog.initSS = 0;
|
||||
prog.initSP = 0xFFFE;
|
||||
|
||||
prog.cReloc = 0;
|
||||
|
||||
prepareImage(prog, cb, fp);
|
||||
|
||||
/* Set up memory map */
|
||||
cb = (prog.cbImage + 3) / 4;
|
||||
prog.map = (uint8_t *)malloc(cb);
|
||||
memset(prog.map, BM_UNKNOWN, (size_t)cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
void prepareImage(PROG &prog, size_t sz, QFile &fp)
|
||||
{
|
||||
int32_t start = 0x100000 - sz;
|
||||
/* Allocate a block of memory for the program. */
|
||||
prog.cbImage = 1 * 1024 * 1024; /* Allocate the whole 1MB memory */
|
||||
//prog.cbImage = 64 * 1024; /* Allocate the whole 1MB memory */
|
||||
prog.Imagez = new uint8_t [prog.cbImage];
|
||||
|
||||
if (fp.read((char *)prog.Imagez + start, sz) != sz)
|
||||
//if (fp.read((char *)prog.Imagez, sz) != sz)
|
||||
{
|
||||
fatalError(CANNOT_READ, fp.fileName().toLocal8Bit().data());
|
||||
}
|
||||
}
|
||||
};
|
||||
#else
|
||||
struct RomLoader {
|
||||
bool canLoad(QFile &fp) {
|
||||
fp.seek(0xFFF0);
|
||||
uint8_t sig[1];
|
||||
if(fp.read((char *)sig,1) == 1)
|
||||
{
|
||||
return (sig[0] == 0xEA);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool load(PROG &prog,QFile &fp) {
|
||||
fp.seek(0);
|
||||
/* COM file
|
||||
* In this case the load module size is just the file length
|
||||
*/
|
||||
auto cb = fp.size();
|
||||
|
||||
/* COM programs start off with an ORG 100H (to leave room for a PSP)
|
||||
* This is also the implied start address so if we load the image
|
||||
* at offset 100H addresses should all line up properly again.
|
||||
*/
|
||||
prog.initCS = 0;
|
||||
prog.initIP = 0x000;
|
||||
prog.initSS = 0;
|
||||
prog.initSP = 0xFFFE;
|
||||
prog.cReloc = 0;
|
||||
|
||||
prepareImage(prog, cb, fp);
|
||||
|
||||
/* Set up memory map */
|
||||
cb = (prog.cbImage + 3) / 4;
|
||||
prog.map = (uint8_t *)malloc(cb);
|
||||
memset(prog.map, BM_UNKNOWN, (size_t)cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
void prepareImage(PROG &prog, size_t sz, QFile &fp)
|
||||
{
|
||||
/* Allocate a block of memory for the program. */
|
||||
prog.cbImage = sz;
|
||||
prog.Imagez = new uint8_t[prog.cbImage];
|
||||
|
||||
if (sz != fp.read((char *)prog.Imagez, sz))
|
||||
fatalError(CANNOT_READ, fp.fileName().toLocal8Bit().data());
|
||||
}
|
||||
};
|
||||
#endif
|
||||
struct ExeLoader : public DosLoader {
|
||||
bool canLoad(QFile &fp) {
|
||||
if(fp.size()<sizeof(header))
|
||||
@ -244,7 +358,7 @@ struct ExeLoader : public DosLoader {
|
||||
MZHeader tmp_header;
|
||||
fp.seek(0);
|
||||
fp.read((char *)&tmp_header, sizeof(header));
|
||||
if(not (tmp_header.sigLo == 0x4D && tmp_header.sigHi == 0x5A))
|
||||
if(not (tmp_header.sigLo == 0x4D and tmp_header.sigHi == 0x5A))
|
||||
return false;
|
||||
|
||||
/* This is a typical DOS kludge! */
|
||||
@ -331,7 +445,7 @@ bool Project::load()
|
||||
const char *fname = binary_path().toLocal8Bit().data();
|
||||
QFile finfo(binary_path());
|
||||
/* Open the input file */
|
||||
if(!finfo.open(QFile::ReadOnly)) {
|
||||
if(not finfo.open(QFile::ReadOnly)) {
|
||||
fatalError(CANNOT_OPEN, fname);
|
||||
}
|
||||
/* Read in first 2 bytes to check EXE signature */
|
||||
@ -339,8 +453,16 @@ bool Project::load()
|
||||
{
|
||||
fatalError(CANNOT_READ, fname);
|
||||
}
|
||||
RomLoader rom_loader;
|
||||
ComLoader com_loader;
|
||||
ExeLoader exe_loader;
|
||||
if(rom_loader.canLoad(finfo)) {
|
||||
/* We have no relacation and code should be on 64K only,
|
||||
* So let's consider it as a COM file
|
||||
*/
|
||||
prog.fCOM = true;
|
||||
return rom_loader.load(prog,finfo);
|
||||
}
|
||||
if(exe_loader.canLoad(finfo)) {
|
||||
prog.fCOM = false;
|
||||
return exe_loader.load(prog,finfo);
|
||||
@ -371,7 +493,7 @@ void DccFrontend::parse(Project &proj)
|
||||
/* Check for special settings of initial state, based on idioms of the
|
||||
startup code */
|
||||
state.checkStartup();
|
||||
Function *start_proc;
|
||||
ilFunction start_proc;
|
||||
/* Make a struct for the initial procedure */
|
||||
if (prog.offMain != -1)
|
||||
{
|
||||
@ -402,7 +524,7 @@ void DccFrontend::parse(Project &proj)
|
||||
|
||||
/* This proc needs to be called to set things up for LibCheck(), which
|
||||
checks a proc to see if it is a know C (etc) library */
|
||||
SetupLibCheck();
|
||||
prog.bSigs = SetupLibCheck();
|
||||
//BUG: proj and g_proj are 'live' at this point !
|
||||
|
||||
/* Recursively build entire procedure list */
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
#include "Procedure.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "project.h"
|
||||
#include "scanner.h"
|
||||
|
||||
//FunctionType *Function::getFunctionType() const
|
||||
//{
|
||||
// return &m_type;
|
||||
@ -15,7 +18,7 @@ void JumpTable::pruneEntries(uint16_t cs)
|
||||
for (uint32_t i = start; i < finish; i += 2)
|
||||
{
|
||||
uint32_t target = cs + LH(&prg->image()[i]);
|
||||
if (target < finish && target >= start)
|
||||
if (target < finish and target >= start)
|
||||
finish = target;
|
||||
else if (target >= (uint32_t)prg->cbImage)
|
||||
finish = i;
|
||||
@ -25,7 +28,7 @@ void JumpTable::pruneEntries(uint16_t cs)
|
||||
{
|
||||
uint32_t target = cs + LH(&prg->image()[i]);
|
||||
/* Be wary of 00 00 as code - it's probably data */
|
||||
if (! (prg->image()[target] || prg->image()[target+1]) || scan(target, _Icode))
|
||||
if (not (prg->image()[target] or prg->image()[target+1]) or scan(target, _Icode))
|
||||
finish = i;
|
||||
}
|
||||
|
||||
|
||||
@ -1,19 +1,20 @@
|
||||
#include "types.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "ast.h"
|
||||
#include "bundle.h"
|
||||
#include "machine_x86.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <boost/range/adaptor/filtered.hpp>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
//#include <boost/range/algorithm.hpp>
|
||||
//#include <boost/assign.hpp>
|
||||
|
||||
#include "types.h"
|
||||
#include "ast.h"
|
||||
#include "bundle.h"
|
||||
|
||||
#include "machine_x86.h"
|
||||
#include "project.h"
|
||||
using namespace std;
|
||||
using namespace boost::adaptors;
|
||||
RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
|
||||
@ -41,7 +42,7 @@ RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
|
||||
// ident.type(REGISTER);
|
||||
// hlType type_sel;
|
||||
// regType reg_type;
|
||||
// if ((icodeFlg & B) || (icodeFlg & SRC_B))
|
||||
// if ((icodeFlg & B) or (icodeFlg & SRC_B))
|
||||
// {
|
||||
// type_sel = TYPE_BYTE_SIGN;
|
||||
// reg_type = BYTE_REG;
|
||||
@ -55,42 +56,42 @@ RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
|
||||
// regiType = reg_type;
|
||||
//}
|
||||
|
||||
string RegisterNode::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
QString RegisterNode::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
{
|
||||
std::ostringstream codeOut;
|
||||
QString codeOut;
|
||||
|
||||
std::ostringstream o;
|
||||
QString o;
|
||||
assert(&pProc->localId==m_syms);
|
||||
ID *id = &pProc->localId.id_arr[regiIdx];
|
||||
if (id->name[0] == '\0') /* no name */
|
||||
{
|
||||
id->setLocalName(++(*numLoc));
|
||||
codeOut <<TypeContainer::typeName(id->type)<< " "<<id->name<<"; ";
|
||||
codeOut <<"/* "<<Machine_X86::regName(id->id.regi)<<" */\n";
|
||||
codeOut += QString("%1 %2; ").arg(TypeContainer::typeName(id->type)).arg(id->name);
|
||||
codeOut += QString("/* %1 */\n").arg(Machine_X86::regName(id->id.regi));
|
||||
}
|
||||
if (id->hasMacro)
|
||||
o << id->macro << "("<<id->name<<")";
|
||||
o += QString("%1(%2)").arg(id->macro).arg(id->name);
|
||||
else
|
||||
o << id->name;
|
||||
o += id->name;
|
||||
|
||||
cCode.appendDecl(codeOut.str());
|
||||
return o.str();
|
||||
cCode.appendDecl(codeOut);
|
||||
return o;
|
||||
}
|
||||
|
||||
int RegisterNode::hlTypeSize(Function *) const
|
||||
{
|
||||
if (regiType == BYTE_REG)
|
||||
return (1);
|
||||
return 1;
|
||||
else
|
||||
return (2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
hlType RegisterNode::expType(Function *pproc) const
|
||||
{
|
||||
if (regiType == BYTE_REG)
|
||||
return (TYPE_BYTE_SIGN);
|
||||
return TYPE_BYTE_SIGN;
|
||||
else
|
||||
return (TYPE_WORD_SIGN);
|
||||
return TYPE_WORD_SIGN;
|
||||
}
|
||||
|
||||
Expr *RegisterNode::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
|
||||
|
||||
262
src/ast.cpp
262
src/ast.cpp
@ -4,34 +4,41 @@
|
||||
* Date: September 1993
|
||||
* (C) Cristina Cifuentes
|
||||
*/
|
||||
#include "ast.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "types.h"
|
||||
#include "bundle.h"
|
||||
#include "machine_x86.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <QtCore/QTextStream>
|
||||
#include <QtCore/QDebug>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptor/filtered.hpp>
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <boost/assign.hpp>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <boost/assign.hpp>
|
||||
|
||||
#include "types.h"
|
||||
#include "ast.h"
|
||||
#include "bundle.h"
|
||||
#include "machine_x86.h"
|
||||
#include "project.h"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
using namespace boost::adaptors;
|
||||
|
||||
extern int strSize (const uint8_t *, char);
|
||||
extern char *cChar(uint8_t c);
|
||||
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
// Conditional operator symbols in C. Index by condOp enumeration type
|
||||
static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
|
||||
constexpr const char * condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
|
||||
" & ", " | ", " ^ ", " ~ ",
|
||||
" + ", " - ", " * ", " / ",
|
||||
" >> ", " << ", " % ", " && ", " || " };
|
||||
/* Size of hl types */
|
||||
constexpr const int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
|
||||
|
||||
/* Local expression stack */
|
||||
//typedef struct _EXP_STK {
|
||||
@ -40,13 +47,13 @@ static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ",
|
||||
//} EXP_STK; - for local expression stack
|
||||
|
||||
/* Returns the integer i in C hexadecimal format */
|
||||
static const char *hexStr (uint16_t i)
|
||||
const char *hexStr (uint16_t i)
|
||||
{
|
||||
static char buf[10];
|
||||
sprintf (buf, "%s%x", (i > 9) ? "0x" : "", i);
|
||||
return (buf);
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Sets the du record for registers according to the du flag */
|
||||
void ICODE::setRegDU (eReg regi, operDu du_in)
|
||||
@ -121,7 +128,7 @@ GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
|
||||
valid = true;
|
||||
ident.idType = GLOB_VAR;
|
||||
adr = opAdr(segValue, off);
|
||||
auto i=Project::get()->getSymIdxByAdd(adr);
|
||||
auto i=Project::get()->getSymIdxByAddr(adr);
|
||||
if ( not Project::get()->validSymIdx(i) )
|
||||
{
|
||||
printf ("Error, glob var not found in symtab\n");
|
||||
@ -130,10 +137,10 @@ GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
|
||||
globIdx = i;
|
||||
}
|
||||
|
||||
string GlobalVariable::walkCondExpr(Function *, int *) const
|
||||
QString GlobalVariable::walkCondExpr(Function *, int *) const
|
||||
{
|
||||
if(valid)
|
||||
return Project::get()->symtab[globIdx].name;
|
||||
return Project::get()->symbolName(globIdx);
|
||||
return "INVALID GlobalVariable";
|
||||
}
|
||||
|
||||
@ -146,7 +153,7 @@ AstIdent *AstIdent::Loc(int off, LOCAL_ID *localId)
|
||||
for (i = 0; i < localId->csym(); i++)
|
||||
{
|
||||
const ID &lID(localId->id_arr[i]);
|
||||
if ((lID.id.bwId.off == off) && (lID.id.bwId.regOff == 0))
|
||||
if ((lID.id.bwId.off == off) and (lID.id.bwId.regOff == 0))
|
||||
break;
|
||||
}
|
||||
if (i == localId->csym())
|
||||
@ -181,19 +188,17 @@ GlobalVariableIdx::GlobalVariableIdx (int16_t segValue, int16_t off, uint8_t reg
|
||||
for (i = 0; i < locSym->csym(); i++)
|
||||
{
|
||||
const BWGLB_TYPE &lID(locSym->id_arr[i].id.bwGlb);
|
||||
if ((lID.seg == segValue) && (lID.off == off) && (lID.regi == regi))
|
||||
if ((lID.seg == segValue) and (lID.off == off) and (lID.regi == regi))
|
||||
break;
|
||||
}
|
||||
if (i == locSym->csym())
|
||||
printf ("Error, indexed-glob var not found in local id table\n");
|
||||
idxGlbIdx = i;
|
||||
}
|
||||
string GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
|
||||
QString GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
|
||||
{
|
||||
ostringstream o;
|
||||
auto bwGlb = &pProc->localId.id_arr[idxGlbIdx].id.bwGlb;
|
||||
o << (bwGlb->seg << 4) + bwGlb->off << "["<<Machine_X86::regName(bwGlb->regi)<<"]";
|
||||
return o.str();
|
||||
return QString("%1[%2]").arg((bwGlb->seg << 4) + bwGlb->off).arg(Machine_X86::regName(bwGlb->regi));
|
||||
}
|
||||
|
||||
|
||||
@ -221,7 +226,7 @@ AstIdent *AstIdent::Long(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f,
|
||||
{
|
||||
AstIdent *newExp;
|
||||
/* Check for long constant and save it as a constant expression */
|
||||
if ((sd == SRC) && pIcode->ll()->testFlags(I)) /* constant */
|
||||
if ((sd == SRC) and pIcode->ll()->testFlags(I)) /* constant */
|
||||
{
|
||||
int value;
|
||||
if (f == HIGH_FIRST)
|
||||
@ -258,18 +263,18 @@ AstIdent *AstIdent::Other(eReg seg, eReg regi, int16_t off)
|
||||
* TYPE_WORD_SIGN */
|
||||
AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
|
||||
{
|
||||
int idx;
|
||||
AstIdent *newExp=nullptr;
|
||||
switch(retVal->type)
|
||||
{
|
||||
case TYPE_LONG_SIGN:
|
||||
{
|
||||
newExp = new AstIdent();
|
||||
idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), ix_);
|
||||
int idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), ix_);
|
||||
newExp->ident.idType = LONG_VAR;
|
||||
newExp->ident.idNode.longIdx = idx;
|
||||
break;
|
||||
}
|
||||
case TYPE_WORD_UNSIGN:
|
||||
case TYPE_WORD_SIGN:
|
||||
newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),WORD_REG,locsym);
|
||||
break;
|
||||
@ -296,8 +301,8 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
|
||||
|
||||
const LLOperand &pm(*ll_insn.get(sd));
|
||||
|
||||
if ( ((sd == DST) && ll_insn.testFlags(IM_DST)) or
|
||||
((sd == SRC) && ll_insn.testFlags(IM_SRC)) or
|
||||
if ( ((sd == DST) and ll_insn.testFlags(IM_DST)) or
|
||||
((sd == SRC) and ll_insn.testFlags(IM_SRC)) or
|
||||
(sd == LHS_OP)) /* for MUL lhs */
|
||||
{ /* implicit dx:ax */
|
||||
idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(rDX, rAX), ix_);
|
||||
@ -306,13 +311,13 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
|
||||
duIcode.setRegDU (rAX, du);
|
||||
}
|
||||
|
||||
else if ((sd == DST) && ll_insn.testFlags(IM_TMP_DST))
|
||||
else if ((sd == DST) and ll_insn.testFlags(IM_TMP_DST))
|
||||
{ /* implicit tmp */
|
||||
newExp = new RegisterNode(LLOperand(rTMP,2), &pProc->localId);
|
||||
duIcode.setRegDU(rTMP, (operDu)eUSE);
|
||||
}
|
||||
|
||||
else if ((sd == SRC) && ll_insn.testFlags(I)) /* constant */
|
||||
else if ((sd == SRC) and ll_insn.testFlags(I)) /* constant */
|
||||
newExp = new Constant(ll_insn.src().getImm2(), 2);
|
||||
else if (pm.regi == rUNDEF) /* global variable */
|
||||
newExp = new GlobalVariable(pm.segValue, pm.off);
|
||||
@ -325,14 +330,14 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
|
||||
|
||||
else if (pm.off) /* offset */
|
||||
{ // TODO: this is ABI specific, should be actually based on Function calling conv
|
||||
if ((pm.seg == rSS) && (pm.regi == INDEX_BP)) /* idx on bp */
|
||||
if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) /* idx on bp */
|
||||
{
|
||||
if (pm.off >= 0) /* argument */
|
||||
newExp = AstIdent::Param (pm.off, &pProc->args);
|
||||
else /* local variable */
|
||||
newExp = AstIdent::Loc (pm.off, &pProc->localId);
|
||||
}
|
||||
else if ((pm.seg == rDS) && (pm.regi == INDEX_BX)) /* bx */
|
||||
else if ((pm.seg == rDS) and (pm.regi == INDEX_BX)) /* bx */
|
||||
{
|
||||
if (pm.off > 0) /* global variable */
|
||||
newExp = new GlobalVariableIdx(pm.segValue, pm.off, rBX,&pProc->localId);
|
||||
@ -344,10 +349,9 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
|
||||
newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
|
||||
/**** check long ops, indexed global var *****/
|
||||
}
|
||||
|
||||
else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
|
||||
else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
|
||||
{
|
||||
if ((pm.seg == rDS) && (pm.regi > INDEX_BP_DI)) /* dereference */
|
||||
if ((pm.seg == rDS) and (pm.regi > INDEX_BP_DI)) /* dereference */
|
||||
{
|
||||
eReg selected;
|
||||
switch (pm.regi) {
|
||||
@ -367,7 +371,7 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
|
||||
else
|
||||
newExp = AstIdent::Other (pm.seg, pm.regi, 0);
|
||||
}
|
||||
return (newExp);
|
||||
return newExp;
|
||||
}
|
||||
|
||||
|
||||
@ -376,32 +380,29 @@ condId LLInst::idType(opLoc sd) const
|
||||
{
|
||||
const LLOperand &pm((sd == SRC) ? src() : m_dst);
|
||||
|
||||
if ((sd == SRC) && testFlags(I))
|
||||
return (CONSTANT);
|
||||
if ((sd == SRC) and testFlags(I))
|
||||
return CONSTANT;
|
||||
else if (pm.regi == 0)
|
||||
return (GLOB_VAR);
|
||||
return GLOB_VAR;
|
||||
else if ( pm.isReg() )
|
||||
return (REGISTER);
|
||||
else if ((pm.seg == rSS) && (pm.regi == INDEX_BP))
|
||||
return REGISTER;
|
||||
else if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) // TODO: this assumes BP-based function frames !
|
||||
{
|
||||
//TODO: which pm.seg/pm.regi pairs should produce PARAM/LOCAL_VAR ?
|
||||
if (pm.off >= 0)
|
||||
return (PARAM);
|
||||
else
|
||||
return (LOCAL_VAR);
|
||||
return PARAM;
|
||||
return LOCAL_VAR;
|
||||
}
|
||||
else
|
||||
return (OTHER);
|
||||
return OTHER;
|
||||
}
|
||||
|
||||
|
||||
/* Size of hl types */
|
||||
int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
|
||||
|
||||
int Expr::hlTypeSize(Function * pproc) const
|
||||
{
|
||||
if (this == nullptr)
|
||||
return (2); /* for TYPE_UNKNOWN */
|
||||
return 2; /* for TYPE_UNKNOWN */
|
||||
fprintf(stderr,"hlTypeSize queried for Unkown type %d \n",m_type);
|
||||
return 2; // CC: is this correct?
|
||||
}
|
||||
@ -436,11 +437,11 @@ int AstIdent::hlTypeSize(Function *pproc) const
|
||||
case PARAM:
|
||||
return (hlSize[pproc->args[ident.idNode.paramIdx].type]);
|
||||
case STRING:
|
||||
return (2);
|
||||
return 2;
|
||||
case LONG_VAR:
|
||||
return (4);
|
||||
return 4;
|
||||
case OTHER:
|
||||
return (2);
|
||||
return 2;
|
||||
default:
|
||||
assert(false);
|
||||
return -1;
|
||||
@ -523,42 +524,40 @@ Expr * HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree
|
||||
}
|
||||
|
||||
/* Returns the string located in image, formatted in C format. */
|
||||
static std::string getString (int offset)
|
||||
static QString getString (int offset)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
ostringstream o;
|
||||
QString o;
|
||||
int strLen, i;
|
||||
|
||||
strLen = strSize (&prog.image()[offset], '\0');
|
||||
o << '"';
|
||||
o += '"';
|
||||
for (i = 0; i < strLen; i++)
|
||||
o<<cChar(prog.image()[offset+i]);
|
||||
o << "\"\0";
|
||||
return (o.str());
|
||||
o += cChar(prog.image()[offset+i]);
|
||||
o += "\"\0";
|
||||
return o;
|
||||
}
|
||||
string BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
|
||||
QString BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
|
||||
{
|
||||
std::ostringstream outStr;
|
||||
outStr << "(";
|
||||
if(m_op!=NOT)
|
||||
{
|
||||
outStr << lhs()->walkCondExpr(pProc, numLoc);
|
||||
}
|
||||
assert(rhs());
|
||||
outStr << condOpSym[m_op];
|
||||
outStr << rhs()->walkCondExpr(pProc, numLoc);
|
||||
outStr << ")";
|
||||
return outStr.str();
|
||||
|
||||
return QString("(%1%2%3)")
|
||||
.arg((m_op!=NOT) ? lhs()->walkCondExpr(pProc, numLoc) : "")
|
||||
.arg(condOpSym[m_op])
|
||||
.arg(rhs()->walkCondExpr(pProc, numLoc));
|
||||
}
|
||||
string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
QString AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
{
|
||||
int16_t off; /* temporal - for OTHER */
|
||||
ID* id; /* Pointer to local identifier table */
|
||||
BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
|
||||
STKSYM * psym; /* Pointer to argument in the stack */
|
||||
std::ostringstream outStr,codeOut;
|
||||
QString codeContents;
|
||||
QString collectedContents;
|
||||
|
||||
QTextStream codeOut(&codeContents);
|
||||
QTextStream o(&collectedContents);
|
||||
|
||||
std::ostringstream o;
|
||||
switch (ident.idType)
|
||||
{
|
||||
case LOCAL_VAR:
|
||||
@ -587,7 +586,7 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
codeOut <<"/* "<<Machine_X86::regName(id->longId().h()) << ":" <<
|
||||
Machine_X86::regName(id->longId().l()) << " */\n";
|
||||
o << id->name;
|
||||
pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name.c_str());
|
||||
pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name);
|
||||
}
|
||||
else /* GLB_FRAME */
|
||||
{
|
||||
@ -595,6 +594,10 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
|
||||
else if (id->id.longGlb.regi == rBX)
|
||||
o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
|
||||
else {
|
||||
qCritical() << "AstIdent::walkCondExpr unhandled LONG_VAR in GLB_FRAME";
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case OTHER:
|
||||
@ -613,79 +616,60 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
|
||||
|
||||
} /* eos */
|
||||
outStr << o.str();
|
||||
cCode.appendDecl(codeOut.str());
|
||||
return outStr.str();
|
||||
cCode.appendDecl(codeContents);
|
||||
return collectedContents;
|
||||
|
||||
}
|
||||
string UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
QString UnaryOperator::wrapUnary(Function *pProc, int *numLoc,QChar op) const
|
||||
{
|
||||
std::ostringstream outStr;
|
||||
bool needBracket=true;
|
||||
QString outStr = op;
|
||||
QString inner = unaryExp->walkCondExpr (pProc, numLoc);
|
||||
if (unaryExp->m_type == IDENTIFIER)
|
||||
outStr += inner;
|
||||
else
|
||||
outStr += "("+inner+')';
|
||||
return outStr;
|
||||
}
|
||||
|
||||
QString UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
{
|
||||
QString outStr;
|
||||
switch(m_type)
|
||||
{
|
||||
case NEGATION:
|
||||
if (unaryExp->m_type == IDENTIFIER)
|
||||
{
|
||||
needBracket = false;
|
||||
outStr << "!";
|
||||
}
|
||||
else
|
||||
outStr << "! (";
|
||||
outStr << unaryExp->walkCondExpr (pProc, numLoc);
|
||||
if (needBracket == true)
|
||||
outStr << ")";
|
||||
outStr+=wrapUnary(pProc,numLoc,'!');
|
||||
break;
|
||||
|
||||
case ADDRESSOF:
|
||||
if (unaryExp->m_type == IDENTIFIER)
|
||||
{
|
||||
needBracket = false;
|
||||
outStr << "&";
|
||||
}
|
||||
else
|
||||
outStr << "&(";
|
||||
outStr << unaryExp->walkCondExpr (pProc, numLoc);
|
||||
if (needBracket == true)
|
||||
outStr << ")";
|
||||
outStr+=wrapUnary(pProc,numLoc,'&');
|
||||
break;
|
||||
|
||||
case DEREFERENCE:
|
||||
outStr << "*";
|
||||
if (unaryExp->m_type == IDENTIFIER)
|
||||
needBracket = false;
|
||||
else
|
||||
outStr << "(";
|
||||
outStr << unaryExp->walkCondExpr (pProc, numLoc);
|
||||
if (needBracket == true)
|
||||
outStr << ")";
|
||||
outStr+=wrapUnary(pProc,numLoc,'*');
|
||||
break;
|
||||
|
||||
case POST_INC:
|
||||
outStr << unaryExp->walkCondExpr (pProc, numLoc) << "++";
|
||||
outStr += unaryExp->walkCondExpr (pProc, numLoc) + "++";
|
||||
break;
|
||||
|
||||
case POST_DEC:
|
||||
outStr << unaryExp->walkCondExpr (pProc, numLoc) << "--";
|
||||
outStr += unaryExp->walkCondExpr (pProc, numLoc) + "--";
|
||||
break;
|
||||
|
||||
case PRE_INC:
|
||||
outStr << "++"<< unaryExp->walkCondExpr (pProc, numLoc);
|
||||
outStr += "++" + unaryExp->walkCondExpr (pProc, numLoc);
|
||||
break;
|
||||
|
||||
case PRE_DEC:
|
||||
outStr << "--"<< unaryExp->walkCondExpr (pProc, numLoc);
|
||||
outStr += "--" + unaryExp->walkCondExpr (pProc, numLoc);
|
||||
break;
|
||||
}
|
||||
return outStr.str();
|
||||
return outStr;
|
||||
}
|
||||
|
||||
/* Walks the conditional expression tree and returns the result on a string */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Changes the boolean conditional operator at the root of this expression */
|
||||
void BinaryOperator::changeBoolOp (condOp newOp)
|
||||
{
|
||||
@ -816,9 +800,8 @@ Expr *BinaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
|
||||
Expr *AstIdent::insertSubTreeLongReg(Expr *_expr, int longIdx)
|
||||
{
|
||||
if (ident.idNode.longIdx == longIdx)
|
||||
{
|
||||
return _expr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -834,7 +817,7 @@ Expr *BinaryOperator::clone() const
|
||||
|
||||
Expr *BinaryOperator::inverse() const
|
||||
{
|
||||
static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
|
||||
constexpr static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
|
||||
LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
|
||||
DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
|
||||
DUMMY, DBL_OR, DBL_AND};
|
||||
@ -866,37 +849,19 @@ Expr *AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
|
||||
{
|
||||
eReg otherRegi; /* high or low part of long register */
|
||||
|
||||
if (ident.idType == LONG_VAR)
|
||||
{
|
||||
otherRegi = otherLongRegi (regi, ident.idNode.longIdx, locId);
|
||||
delete this;
|
||||
return new RegisterNode(locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi),WORD_REG,locId);
|
||||
}
|
||||
if (ident.idType != LONG_VAR)
|
||||
return this;
|
||||
}
|
||||
eReg AstIdent::otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
|
||||
{
|
||||
ID *id = &locTbl->id_arr[idx];
|
||||
if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
|
||||
(id->type == TYPE_LONG_UNSIGN)))
|
||||
{
|
||||
if (id->longId().h() == regi)
|
||||
return (id->longId().l());
|
||||
else if (id->longId().l() == regi)
|
||||
return (id->longId().h());
|
||||
}
|
||||
return rUNDEF; // Cristina: please check this!
|
||||
otherRegi = locId->getPairedRegisterAt(ident.idNode.longIdx,regi);
|
||||
bool long_was_signed = locId->id_arr[ident.idNode.longIdx].isSigned();
|
||||
delete this;
|
||||
return new RegisterNode(locId->newByteWordReg(long_was_signed ? TYPE_WORD_SIGN : TYPE_WORD_UNSIGN,otherRegi),WORD_REG,locId);
|
||||
}
|
||||
|
||||
|
||||
string Constant::walkCondExpr(Function *, int *) const
|
||||
QString Constant::walkCondExpr(Function *, int *) const
|
||||
{
|
||||
ostringstream o;
|
||||
if (kte.kte < 1000)
|
||||
o << kte.kte;
|
||||
else
|
||||
o << "0x"<<std::hex << kte.kte;
|
||||
return o.str();
|
||||
return QString::number(kte.kte);
|
||||
return "0x" + QString::number(kte.kte,16);
|
||||
}
|
||||
|
||||
int Constant::hlTypeSize(Function *) const
|
||||
@ -904,12 +869,7 @@ int Constant::hlTypeSize(Function *) const
|
||||
return kte.size;
|
||||
}
|
||||
|
||||
hlType Constant::expType(Function *pproc) const
|
||||
{
|
||||
return TYPE_CONST;
|
||||
}
|
||||
|
||||
string FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
QString FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
|
||||
{
|
||||
return pProc->writeCall(call.proc,*call.args, numLoc);
|
||||
}
|
||||
|
||||
134
src/backend.cpp
134
src/backend.cpp
@ -4,12 +4,20 @@
|
||||
* Purpose: Back-end module. Generates C code for each procedure.
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "disassem.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
|
||||
#include <QtCore/QDir>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QDebug>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
#include <boost/range/adaptor/filtered.hpp>
|
||||
#include <boost/range/algorithm.hpp>
|
||||
|
||||
#include <fstream>
|
||||
@ -17,15 +25,13 @@
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "dcc.h"
|
||||
#include "disassem.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
|
||||
using namespace boost;
|
||||
using namespace boost::adaptors;
|
||||
bundle cCode; /* Procedure declaration and code */
|
||||
using namespace std;
|
||||
|
||||
bundle cCode; /* Procedure declaration and code */
|
||||
|
||||
/* Returns a unique index to the next label */
|
||||
int getNextLabel()
|
||||
{
|
||||
@ -37,14 +43,14 @@ int getNextLabel()
|
||||
/* displays statistics on the subroutine */
|
||||
void Function::displayStats ()
|
||||
{
|
||||
printf("\nStatistics - Subroutine %s\n", name.c_str());
|
||||
printf ("Number of Icode instructions:\n");
|
||||
printf (" Low-level : %4d\n", stats.numLLIcode);
|
||||
if (! (flg & PROC_ASM))
|
||||
qDebug() << "\nStatistics - Subroutine" << name;
|
||||
qDebug() << "Number of Icode instructions:";
|
||||
qDebug() << " Low-level :" << stats.numLLIcode;
|
||||
if (not (flg & PROC_ASM))
|
||||
{
|
||||
printf (" High-level: %4d\n", stats.numHLIcode);
|
||||
printf (" Percentage reduction: %2.2f%%\n", 100.0 - (stats.numHLIcode *
|
||||
100.0) / stats.numLLIcode);
|
||||
qDebug() << " High-level:"<<stats.numHLIcode;
|
||||
qDebug() << QString(" Percentage reduction: %1%%").arg(100.0 - (stats.numHLIcode *
|
||||
100.0) / stats.numLLIcode,4,'f',2,QChar('0'));
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,7 +108,7 @@ char *cChar (uint8_t c)
|
||||
* Note: to get to the value of the variable:
|
||||
* com file: prog.Image[operand]
|
||||
* exe file: prog.Image[operand+0x100] */
|
||||
static void printGlobVar (std::ostream &ostr,SYM * psym)
|
||||
static void printGlobVar (QTextStream &ostr,SYM * psym)
|
||||
{
|
||||
int j;
|
||||
PROG &prog(Project::get()->prog);
|
||||
@ -125,10 +131,10 @@ static void printGlobVar (std::ostream &ostr,SYM * psym)
|
||||
break;
|
||||
default:
|
||||
{
|
||||
ostringstream strContents;
|
||||
QString strContents;
|
||||
for (j=0; j < psym->size; j++)
|
||||
strContents << cChar(prog.image()[relocOp + j]);
|
||||
ostr << "char\t*"<<psym->name<<" = \""<<strContents.str()<<"\";\n";
|
||||
strContents += cChar(prog.image()[relocOp + j]);
|
||||
ostr << "char\t*"<<psym->name<<" = \""<<strContents<<"\";\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -139,7 +145,8 @@ static void printGlobVar (std::ostream &ostr,SYM * psym)
|
||||
* initialization. */
|
||||
void Project::writeGlobSymTable()
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
QString contents;
|
||||
QTextStream ostr(&contents);
|
||||
|
||||
if (symtab.empty())
|
||||
return;
|
||||
@ -151,9 +158,9 @@ void Project::writeGlobSymTable()
|
||||
else { /* first defined */
|
||||
switch (sym.size) {
|
||||
case 1: ostr<<"uint8_t\t"; break;
|
||||
case 2: ostr<<"int\t"; break;
|
||||
case 2: ostr<<"int16_t\t"; break;
|
||||
case 4: if (sym.type == TYPE_PTR)
|
||||
ostr<<"int\t*";
|
||||
ostr<<"int32_t\t*";
|
||||
else
|
||||
ostr<<"char\t*";
|
||||
break;
|
||||
@ -163,13 +170,14 @@ void Project::writeGlobSymTable()
|
||||
}
|
||||
}
|
||||
ostr<< "\n";
|
||||
cCode.appendDecl( ostr.str() );
|
||||
ostr.flush();
|
||||
cCode.appendDecl( contents );
|
||||
}
|
||||
|
||||
|
||||
/* Writes the header information and global variables to the output C file
|
||||
* fp. */
|
||||
static void writeHeader (std::ostream &_ios, const std::string &fileName)
|
||||
static void writeHeader (QIODevice &_ios, const std::string &fileName)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
/* Write header information */
|
||||
@ -206,10 +214,11 @@ static void emitFwdGotoLabel (ICODE * pt, int indLevel)
|
||||
|
||||
/* Writes the procedure's declaration (including arguments), local variables,
|
||||
* and invokes the procedure that writes the code of the given record *hli */
|
||||
void Function::codeGen (std::ostream &fs)
|
||||
void Function::codeGen (QIODevice &fs)
|
||||
{
|
||||
int numLoc;
|
||||
ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
QTextStream ostr(&ostr_contents);
|
||||
//STKFRAME * args; /* Procedure arguments */
|
||||
//char buf[200], /* Procedure's definition */
|
||||
// arg[30]; /* One argument */
|
||||
@ -218,30 +227,27 @@ void Function::codeGen (std::ostream &fs)
|
||||
/* Write procedure/function header */
|
||||
cCode.init();
|
||||
if (flg & PROC_IS_FUNC) /* Function */
|
||||
ostr<< "\n"<<TypeContainer::typeName(retVal.type)<<" "<<name<<" (";
|
||||
ostr << QString("\n%1 %2 (").arg(TypeContainer::typeName(retVal.type)).arg(name);
|
||||
else /* Procedure */
|
||||
ostr<< "\nvoid "<<name<<" (";
|
||||
ostr << "\nvoid "+name+" (";
|
||||
|
||||
/* Write arguments */
|
||||
struct validArg
|
||||
{
|
||||
bool operator()(STKSYM &s) { return s.invalid==false;}
|
||||
};
|
||||
auto valid_args = args | filtered(validArg());
|
||||
int count_valid = std::distance(valid_args.begin(),valid_args.end());
|
||||
for (STKSYM &arg : valid_args)
|
||||
QStringList parts;
|
||||
for (STKSYM &arg : (args | filtered(validArg())))
|
||||
{
|
||||
ostr<<hlTypes[arg.type]<<" "<<arg.name;
|
||||
if(--count_valid!=0)
|
||||
ostr<<", ";
|
||||
parts << QString("%1 %2").arg(hlTypes[arg.type]).arg(arg.name);
|
||||
}
|
||||
ostr<<")\n";
|
||||
ostr << parts.join(", ")+")\n";
|
||||
|
||||
/* Write comments */
|
||||
writeProcComments( ostr );
|
||||
|
||||
/* Write local variables */
|
||||
if (! (flg & PROC_ASM))
|
||||
if (not (flg & PROC_ASM))
|
||||
{
|
||||
numLoc = 0;
|
||||
for (ID &refId : localId )
|
||||
@ -252,8 +258,8 @@ void Function::codeGen (std::ostream &fs)
|
||||
if (refId.loc == REG_FRAME)
|
||||
{
|
||||
/* Register variables are assigned to a local variable */
|
||||
if (((flg & SI_REGVAR) && (refId.id.regi == rSI)) ||
|
||||
((flg & DI_REGVAR) && (refId.id.regi == rDI)))
|
||||
if (((flg & SI_REGVAR) and (refId.id.regi == rSI)) or
|
||||
((flg & DI_REGVAR) and (refId.id.regi == rDI)))
|
||||
{
|
||||
refId.setLocalName(++numLoc);
|
||||
ostr << "int "<<refId.name<<";\n";
|
||||
@ -269,7 +275,9 @@ void Function::codeGen (std::ostream &fs)
|
||||
}
|
||||
}
|
||||
}
|
||||
fs<<ostr.str();
|
||||
ostr.flush();
|
||||
fs.write(ostr_contents.toLatin1());
|
||||
|
||||
/* Write procedure's code */
|
||||
if (flg & PROC_ASM) /* generate assembler */
|
||||
{
|
||||
@ -286,36 +294,41 @@ void Function::codeGen (std::ostream &fs)
|
||||
freeBundle (&cCode);
|
||||
|
||||
/* Write Live register analysis information */
|
||||
if (option.verbose)
|
||||
if (option.verbose) {
|
||||
QString debug_contents;
|
||||
QTextStream debug_stream(&debug_contents);
|
||||
for (size_t i = 0; i < numBBs; i++)
|
||||
{
|
||||
pBB = m_dfsLast[i];
|
||||
if (pBB->flg & INVALID_BB) continue; /* skip invalid BBs */
|
||||
cout << "BB "<<i<<"\n";
|
||||
cout << " Start = "<<pBB->begin()->loc_ip;
|
||||
cout << ", end = "<<pBB->begin()->loc_ip+pBB->size()<<"\n";
|
||||
cout << " LiveUse = ";
|
||||
Machine_X86::writeRegVector(cout,pBB->liveUse);
|
||||
cout << "\n Def = ";
|
||||
Machine_X86::writeRegVector(cout,pBB->def);
|
||||
cout << "\n LiveOut = ";
|
||||
Machine_X86::writeRegVector(cout,pBB->liveOut);
|
||||
cout << "\n LiveIn = ";
|
||||
Machine_X86::writeRegVector(cout,pBB->liveIn);
|
||||
cout <<"\n\n";
|
||||
debug_stream << "BB "<<i<<"\n";
|
||||
debug_stream << " Start = "<<pBB->begin()->loc_ip;
|
||||
debug_stream << ", end = "<<pBB->begin()->loc_ip+pBB->size()<<"\n";
|
||||
debug_stream << " LiveUse = ";
|
||||
Machine_X86::writeRegVector(debug_stream,pBB->liveUse);
|
||||
debug_stream << "\n Def = ";
|
||||
Machine_X86::writeRegVector(debug_stream,pBB->def);
|
||||
debug_stream << "\n LiveOut = ";
|
||||
Machine_X86::writeRegVector(debug_stream,pBB->liveOut);
|
||||
debug_stream << "\n LiveIn = ";
|
||||
Machine_X86::writeRegVector(debug_stream,pBB->liveIn);
|
||||
debug_stream <<"\n\n";
|
||||
}
|
||||
debug_stream.flush();
|
||||
qDebug() << debug_contents.toLatin1();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Recursive procedure. Displays the procedure's code in depth-first order
|
||||
* of the call graph. */
|
||||
static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
|
||||
static void backBackEnd (CALL_GRAPH * pcallGraph, QIODevice &_ios)
|
||||
{
|
||||
|
||||
// IFace.Yield(); /* This is a good place to yield to other apps */
|
||||
|
||||
/* Check if this procedure has been processed already */
|
||||
if ((pcallGraph->proc->flg & PROC_OUTPUT) ||
|
||||
if ((pcallGraph->proc->flg & PROC_OUTPUT) or
|
||||
(pcallGraph->proc->flg & PROC_ISLIB))
|
||||
return;
|
||||
pcallGraph->proc->flg |= PROC_OUTPUT;
|
||||
@ -334,7 +347,7 @@ static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
|
||||
/* Generate statistics */
|
||||
if (option.Stats)
|
||||
pcallGraph->proc->displayStats ();
|
||||
if (! (pcallGraph->proc->flg & PROC_ASM))
|
||||
if (not (pcallGraph->proc->flg & PROC_ASM))
|
||||
{
|
||||
stats.totalLL += stats.numLLIcode;
|
||||
stats.totalHL += stats.numHLIcode;
|
||||
@ -345,16 +358,15 @@ static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
|
||||
/* Invokes the necessary routines to produce code one procedure at a time. */
|
||||
void BackEnd(CALL_GRAPH * pcallGraph)
|
||||
{
|
||||
std::ofstream fs; /* Output C file */
|
||||
|
||||
/* Get output file name */
|
||||
QString outNam(Project::get()->output_name("b")); /* b for beta */
|
||||
QFile fs(outNam); /* Output C file */
|
||||
|
||||
/* Open output file */
|
||||
fs.open(outNam.toStdString());
|
||||
if(!fs.is_open())
|
||||
if(not fs.open(QFile::WriteOnly|QFile::Text))
|
||||
fatalError (CANNOT_OPEN, outNam.toStdString().c_str());
|
||||
std::cout<<"dcc: Writing C beta file "<<outNam.toStdString()<<"\n";
|
||||
|
||||
qDebug()<<"dcc: Writing C beta file"<<outNam;
|
||||
|
||||
/* Header information */
|
||||
writeHeader (fs, option.filename.toStdString());
|
||||
@ -368,7 +380,7 @@ void BackEnd(CALL_GRAPH * pcallGraph)
|
||||
|
||||
/* Close output file */
|
||||
fs.close();
|
||||
std::cout << "dcc: Finished writing C beta file\n";
|
||||
qDebug() << "dcc: Finished writing C beta file";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
#include <memory.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <QtCore/QIODevice>
|
||||
#define deltaProcLines 20
|
||||
|
||||
using namespace std;
|
||||
@ -21,26 +21,26 @@ using namespace std;
|
||||
* tab is removed and replaced by this label */
|
||||
void strTable::addLabelBundle (int idx, int label)
|
||||
{
|
||||
char s[16];
|
||||
sprintf (s, "l%d: ", label);
|
||||
if(at(idx).size()<4)
|
||||
at(idx)=s;
|
||||
QString &processedLine(at(idx));
|
||||
QString s = QString("l%1: ").arg(label);
|
||||
if(processedLine.size()<4)
|
||||
processedLine = s;
|
||||
else
|
||||
at(idx) = string(s)+at(idx).substr(4);
|
||||
processedLine = s+processedLine.mid(4);
|
||||
}
|
||||
|
||||
|
||||
/* Writes the contents of the string table on the file fp. */
|
||||
static void writeStrTab (std::ostream &ios, strTable &strTab)
|
||||
static void writeStrTab (QIODevice &ios, strTable &strTab)
|
||||
{
|
||||
for (size_t i = 0; i < strTab.size(); i++)
|
||||
ios << strTab[i];
|
||||
ios.write(strTab[i].toLatin1());
|
||||
}
|
||||
|
||||
|
||||
/* Writes the contents of the bundle (procedure code and declaration) to
|
||||
* a file. */
|
||||
void writeBundle (std::ostream &ios, bundle procCode)
|
||||
void writeBundle (QIODevice &ios, bundle procCode)
|
||||
{
|
||||
writeStrTab (ios, procCode.decl);
|
||||
writeStrTab (ios, procCode.code);
|
||||
@ -70,7 +70,7 @@ void bundle::appendCode(const char *format,...)
|
||||
code.push_back(buf);
|
||||
va_end (args);
|
||||
}
|
||||
void bundle::appendCode(const std::string &s)
|
||||
void bundle::appendCode(const QString & s)
|
||||
{
|
||||
code.push_back(s);
|
||||
}
|
||||
@ -85,7 +85,7 @@ void bundle::appendDecl(const char *format,...)
|
||||
va_end (args);
|
||||
}
|
||||
|
||||
void bundle::appendDecl(const std::string &v)
|
||||
void bundle::appendDecl(const QString &v)
|
||||
{
|
||||
decl.push_back(v);
|
||||
}
|
||||
|
||||
@ -6,15 +6,19 @@
|
||||
*/
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "project.h"
|
||||
#include "perfhlib.h"
|
||||
#include "dcc_interface.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QtCore/QDir>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QDebug>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
|
||||
PerfectHash g_pattern_hasher;
|
||||
#define NIL -1 /* Used like NULL, but 0 is valid */
|
||||
|
||||
@ -49,7 +53,7 @@ int numVert; /* Number of vertices in the graph (also size of g[]
|
||||
unsigned PatLen; /* Size of the keys (pattern length) */
|
||||
unsigned SymLen; /* Max size of the symbols, including null */
|
||||
static FILE *g_file; /* File being read */
|
||||
static char sSigName[100]; /* Full path name of .sig file */
|
||||
static QString sSigName; /* Full path name of .sig file */
|
||||
|
||||
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static uint16_t *g; /* g[] */
|
||||
@ -294,9 +298,8 @@ static uint8_t pattMsChkstk[] =
|
||||
|
||||
|
||||
/* This procedure is called to initialise the library check code */
|
||||
void SetupLibCheck(void)
|
||||
bool SetupLibCheck(void)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
uint16_t w, len;
|
||||
int i;
|
||||
IDcc *dcc = IDcc::get();
|
||||
@ -304,13 +307,12 @@ void SetupLibCheck(void)
|
||||
if ((g_file = fopen(qPrintable(fpath), "rb")) == nullptr)
|
||||
{
|
||||
printf("Warning: cannot open signature file %s\n", qPrintable(fpath));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
readProtoFile();
|
||||
|
||||
|
||||
prog.bSigs = false; /* False unless everything goes right */
|
||||
/* Read the parameters */
|
||||
grab(4, g_file);
|
||||
if (memcmp("dccs", buf, 4) != 0)
|
||||
@ -322,11 +324,10 @@ void SetupLibCheck(void)
|
||||
numVert = readFileShort(g_file);
|
||||
PatLen = readFileShort(g_file);
|
||||
SymLen = readFileShort(g_file);
|
||||
if ((PatLen != PATLEN) || (SymLen != SYMLEN))
|
||||
if ((PatLen != PATLEN) or (SymLen != SYMLEN))
|
||||
{
|
||||
printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n",
|
||||
SYMLEN, PATLEN);
|
||||
exit(1);
|
||||
printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n", SYMLEN, PATLEN);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
|
||||
@ -353,7 +354,7 @@ void SetupLibCheck(void)
|
||||
if (w != len)
|
||||
{
|
||||
printf("Problem with size of T1: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
return false;
|
||||
}
|
||||
readFileSection(T1base, len, g_file);
|
||||
|
||||
@ -361,13 +362,13 @@ void SetupLibCheck(void)
|
||||
if (memcmp("T2", buf, 2) != 0)
|
||||
{
|
||||
printf("Expected 'T2'\n");
|
||||
exit(3);
|
||||
return false;
|
||||
}
|
||||
w = readFileShort(g_file);
|
||||
if (w != len)
|
||||
{
|
||||
printf("Problem with size of T2: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
return false;
|
||||
}
|
||||
readFileSection(T2base, len, g_file);
|
||||
|
||||
@ -376,14 +377,14 @@ void SetupLibCheck(void)
|
||||
if (memcmp("gg", buf, 2) != 0)
|
||||
{
|
||||
printf("Expected 'gg'\n");
|
||||
exit(3);
|
||||
return false;
|
||||
}
|
||||
len = (uint16_t)(numVert * sizeof(uint16_t));
|
||||
w = readFileShort(g_file);
|
||||
if (w != len)
|
||||
{
|
||||
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
return false;
|
||||
}
|
||||
readFileSection(g, len, g_file);
|
||||
|
||||
@ -394,19 +395,19 @@ void SetupLibCheck(void)
|
||||
if ( nullptr == ht)
|
||||
{
|
||||
printf("Could not allocate hash table\n");
|
||||
exit(1);
|
||||
return false;
|
||||
}
|
||||
grab(2, g_file);
|
||||
if (memcmp("ht", buf, 2) != 0)
|
||||
{
|
||||
printf("Expected 'ht'\n");
|
||||
exit(3);
|
||||
return false;
|
||||
}
|
||||
w = readFileShort(g_file);
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
|
||||
{
|
||||
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
|
||||
exit(6);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -415,11 +416,11 @@ void SetupLibCheck(void)
|
||||
if (fread(&ht[i], 1, SymLen + PatLen, g_file) != SymLen + PatLen)
|
||||
{
|
||||
printf("Could not read signature\n");
|
||||
exit(11);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fclose(g_file);
|
||||
prog.bSigs = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -466,17 +467,17 @@ bool LibCheck(Function & pProc)
|
||||
if (memcmp(ht[h].htPat, pat, PATLEN) == 0)
|
||||
{
|
||||
/* We have a match. Save the name, if not already set */
|
||||
if (pProc.name.empty() ) /* Don't overwrite existing name */
|
||||
if (pProc.name.isEmpty() ) /* Don't overwrite existing name */
|
||||
{
|
||||
/* Give proc the new name */
|
||||
pProc.name = ht[h].htSym;
|
||||
}
|
||||
/* But is it a real library function? */
|
||||
i = NIL;
|
||||
if ((numFunc == 0) || (i=searchPList(ht[h].htSym)) != NIL)
|
||||
if ((numFunc == 0) or (i=searchPList(ht[h].htSym)) != NIL)
|
||||
{
|
||||
pProc.flg |= PROC_ISLIB; /* It's a lib function */
|
||||
pProc.callingConv(CConv::C);
|
||||
pProc.callingConv(CConv::eCdecl);
|
||||
if (i != NIL)
|
||||
{
|
||||
/* Allocate space for the arg struct, and copy the hlType to
|
||||
@ -502,12 +503,14 @@ bool LibCheck(Function & pProc)
|
||||
case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
|
||||
pProc.liveOut.setReg(rAL);
|
||||
break;
|
||||
case TYPE_STR:
|
||||
case TYPE_PTR:
|
||||
fprintf(stderr,"Warning assuming Large memory model\n");
|
||||
pProc.liveOut.setReg(rAX).addReg(rDS);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,"Unknown retval type %d for %s in LibCheck\n",pProc.retVal.type,pProc.name.c_str());
|
||||
qCritical() << QString("Unknown retval type %1 for %2 in LibCheck")
|
||||
.arg(pProc.retVal.type).arg(pProc.name);
|
||||
/*** other types are not considered yet ***/
|
||||
}
|
||||
}
|
||||
@ -531,7 +534,7 @@ bool LibCheck(Function & pProc)
|
||||
pProc.args.numArgs = 0; /* With no args */
|
||||
}
|
||||
|
||||
return (bool)((pProc.flg & PROC_ISLIB) != 0);
|
||||
return pProc.isLibrary();
|
||||
}
|
||||
|
||||
|
||||
@ -545,8 +548,7 @@ void grab(int n, FILE *_file)
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t
|
||||
readFileShort(FILE *f)
|
||||
uint16_t readFileShort(FILE *f)
|
||||
{
|
||||
uint8_t b1, b2;
|
||||
|
||||
@ -603,7 +605,7 @@ static bool locatePattern(const uint8_t *source, int iMin, int iMax, uint8_t *pa
|
||||
for (j=0; j < iPatLen; j++)
|
||||
{
|
||||
/* j is the index of the uint8_t being considered in the pattern. */
|
||||
if ((*pSrc != pattern[j]) && (pattern[j] != WILD))
|
||||
if ((*pSrc != pattern[j]) and (pattern[j] != WILD))
|
||||
{
|
||||
/* A definite mismatch */
|
||||
break; /* Break to outer loop */
|
||||
@ -828,16 +830,12 @@ void STATE::checkStartup()
|
||||
|
||||
gotVendor:
|
||||
|
||||
strcat(sSigName, "dcc");
|
||||
temp[1] = '\0';
|
||||
temp[0] = chVendor;
|
||||
strcat(sSigName, temp); /* Add vendor */
|
||||
temp[0] = chVersion;
|
||||
strcat(sSigName, temp); /* Add version */
|
||||
temp[0] = chModel;
|
||||
strcat(sSigName, temp); /* Add model */
|
||||
strcat(sSigName, ".sig"); /* Add extension */
|
||||
printf("Signature file: %s\n", sSigName);
|
||||
sSigName = QString("dcc%1%2%3.sig")
|
||||
.arg(QChar(chVendor)) /* Add vendor */
|
||||
.arg(QChar(chVersion)) /* Add version */
|
||||
.arg(QChar(chModel)) /* Add model */
|
||||
;
|
||||
printf("Signature file: %s\n", qPrintable(sSigName));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -7,9 +7,12 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "machine_x86.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
#include <QTextStream>
|
||||
using namespace std;
|
||||
#define intSize 40
|
||||
|
||||
@ -148,7 +151,7 @@ static const char *intOthers[] = {
|
||||
|
||||
/* Writes the description of the current interrupt. Appends it to the
|
||||
* string s. */
|
||||
void LLInst::writeIntComment (std::ostringstream &s)
|
||||
void LLInst::writeIntComment (QTextStream &s)
|
||||
{
|
||||
uint32_t src_immed=src().getImm2();
|
||||
s<<"\t/* ";
|
||||
@ -156,7 +159,7 @@ void LLInst::writeIntComment (std::ostringstream &s)
|
||||
{
|
||||
s <<int21h[m_dst.off];
|
||||
}
|
||||
else if (src_immed > 0x1F && src_immed < 0x2F)
|
||||
else if (src_immed > 0x1F and src_immed < 0x2F)
|
||||
{
|
||||
s <<intOthers[src_immed - 0x20];
|
||||
}
|
||||
@ -186,12 +189,15 @@ void LLInst::writeIntComment (std::ostringstream &s)
|
||||
//, &cCode.decl
|
||||
void Function::writeProcComments()
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
QString dest_str;
|
||||
{
|
||||
QTextStream ostr(&dest_str);
|
||||
writeProcComments(ostr);
|
||||
cCode.appendDecl(ostr.str());
|
||||
}
|
||||
cCode.appendDecl(dest_str);
|
||||
}
|
||||
|
||||
void Function::writeProcComments(std::ostream &ostr)
|
||||
void Function::writeProcComments(QTextStream &ostr)
|
||||
{
|
||||
int i;
|
||||
ID *id; /* Pointer to register argument identifier */
|
||||
|
||||
300
src/control.cpp
300
src/control.cpp
@ -2,33 +2,34 @@
|
||||
* Description : Performs control flow analysis on the CFG
|
||||
* (C) Cristina Cifuentes
|
||||
********************************************************************/
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <cassert>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
//typedef struct list {
|
||||
// int nodeIdx;
|
||||
// struct list *next;
|
||||
//} nodeList;
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
|
||||
namespace {
|
||||
typedef std::list<int> nodeList; /* dfsLast index to the node */
|
||||
|
||||
#define ancestor(a,b) ((a->dfsLastNum < b->dfsLastNum) && (a->dfsFirstNum < b->dfsFirstNum))
|
||||
/* there is a path on the DFST from a to b if the a was first visited in a
|
||||
* dfs, and a was later visited than b when doing the last visit of each
|
||||
* node. */
|
||||
bool inline ancestor(BB *a,BB *b)
|
||||
{
|
||||
return (a->dfsLastNum < b->dfsLastNum) and (a->dfsFirstNum < b->dfsFirstNum);
|
||||
}
|
||||
|
||||
|
||||
/* Checks if the edge (p,s) is a back edge. If node s was visited first
|
||||
/** Checks if the edge (p,s) is a back edge. If node s was visited first
|
||||
* during the dfs traversal (ie. s has a smaller dfsFirst number) or s == p,
|
||||
* then it is a backedge.
|
||||
* Also incrementes the number of backedges entries to the header node. */
|
||||
static bool isBackEdge (BB * p,BB * s)
|
||||
bool isBackEdge (BB * p,BB * s)
|
||||
{
|
||||
if (p->dfsFirstNum >= s->dfsFirstNum)
|
||||
{
|
||||
@ -39,16 +40,16 @@ static bool isBackEdge (BB * p,BB * s)
|
||||
}
|
||||
|
||||
|
||||
/* Finds the common dominator of the current immediate dominator
|
||||
/** Finds the common dominator of the current immediate dominator
|
||||
* currImmDom and its predecessor's immediate dominator predImmDom */
|
||||
static int commonDom (int currImmDom, int predImmDom, Function * pProc)
|
||||
int commonDom (int currImmDom, int predImmDom, Function * pProc)
|
||||
{
|
||||
if (currImmDom == NO_DOM)
|
||||
return (predImmDom);
|
||||
if (predImmDom == NO_DOM) /* predecessor is the root */
|
||||
return (currImmDom);
|
||||
|
||||
while ((currImmDom != NO_DOM) && (predImmDom != NO_DOM) &&
|
||||
while ((currImmDom != NO_DOM) and (predImmDom != NO_DOM) and
|
||||
(currImmDom != predImmDom))
|
||||
{
|
||||
if (currImmDom < predImmDom)
|
||||
@ -58,70 +59,47 @@ static int commonDom (int currImmDom, int predImmDom, Function * pProc)
|
||||
}
|
||||
return (currImmDom);
|
||||
}
|
||||
|
||||
|
||||
/* Finds the immediate dominator of each node in the graph pProc->cfg.
|
||||
* Adapted version of the dominators algorithm by Hecht and Ullman; finds
|
||||
* immediate dominators only.
|
||||
* Note: graph should be reducible */
|
||||
void Function::findImmedDom ()
|
||||
{
|
||||
BB * currNode;
|
||||
for (size_t currIdx = 0; currIdx < numBBs; currIdx++)
|
||||
{
|
||||
currNode = m_dfsLast[currIdx];
|
||||
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
|
||||
continue;
|
||||
for (BB * inedge : currNode->inEdges)
|
||||
{
|
||||
size_t predIdx = inedge->dfsLastNum;
|
||||
if (predIdx < currIdx)
|
||||
currNode->immedDom = commonDom (currNode->immedDom, predIdx, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Inserts the node n to the list l. */
|
||||
static void insertList (nodeList &l, int n)
|
||||
{
|
||||
l.push_back(n);
|
||||
}
|
||||
|
||||
|
||||
/* Returns whether or not the node n (dfsLast numbering of a basic block)
|
||||
* is on the list l. */
|
||||
static bool inList (const nodeList &l, int n)
|
||||
bool inList (const nodeList &l, int n)
|
||||
{
|
||||
return std::find(l.begin(),l.end(),n)!=l.end();
|
||||
}
|
||||
|
||||
|
||||
/* Frees space allocated by the list l. */
|
||||
static void freeList (nodeList &l)
|
||||
{
|
||||
l.clear();
|
||||
}
|
||||
|
||||
|
||||
/* Returns whether the node n belongs to the queue list q. */
|
||||
static bool inInt(BB * n, queue &q)
|
||||
bool inInt(BB * n, queue &q)
|
||||
{
|
||||
return std::find(q.begin(),q.end(),n)!=q.end();
|
||||
}
|
||||
|
||||
|
||||
/** Recursive procedure to find nodes that belong to the interval (ie. nodes
|
||||
* from G1). */
|
||||
void findNodesInInt (queue &intNodes, int level, interval *Ii)
|
||||
{
|
||||
if (level == 1)
|
||||
{
|
||||
for(BB *en : Ii->nodes)
|
||||
{
|
||||
appendQueue(intNodes,en);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(BB *en : Ii->nodes)
|
||||
{
|
||||
findNodesInInt(intNodes,level-1,en->correspInt);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Finds the follow of the endless loop headed at node head (if any).
|
||||
* The follow node is the closest node to the loop. */
|
||||
static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
|
||||
void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
|
||||
{
|
||||
head->loopFollow = MAX;
|
||||
for( int loop_node : loopNodes)
|
||||
{
|
||||
for (TYPEADR_TYPE &typeaddr: pProc->m_dfsLast[loop_node]->edges)
|
||||
for (const TYPEADR_TYPE &typeaddr: pProc->m_dfsLast[loop_node]->edges)
|
||||
{
|
||||
int succ = typeaddr.BBptr->dfsLastNum;
|
||||
if ((! inList(loopNodes, succ)) && (succ < head->loopFollow))
|
||||
if ((not inList(loopNodes, succ)) and (succ < head->loopFollow))
|
||||
head->loopFollow = succ;
|
||||
}
|
||||
}
|
||||
@ -131,7 +109,7 @@ static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
|
||||
//static void findNodesInLoop(BB * latchNode,BB * head,PPROC pProc,queue *intNodes)
|
||||
/* Flags nodes that belong to the loop determined by (latchNode, head) and
|
||||
* determines the type of loop. */
|
||||
static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &intNodes)
|
||||
void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &intNodes)
|
||||
{
|
||||
int i, headDfsNum, intNodeType;
|
||||
nodeList loopNodes;
|
||||
@ -142,30 +120,30 @@ static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &int
|
||||
/* Flag nodes in loop headed by head (except header node) */
|
||||
headDfsNum = head->dfsLastNum;
|
||||
head->loopHead = headDfsNum;
|
||||
insertList (loopNodes, headDfsNum);
|
||||
loopNodes.push_back(headDfsNum);
|
||||
for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++)
|
||||
{
|
||||
if (pProc->m_dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */
|
||||
continue;
|
||||
|
||||
immedDom = pProc->m_dfsLast[i]->immedDom;
|
||||
if (inList (loopNodes, immedDom) && inInt(pProc->m_dfsLast[i], intNodes))
|
||||
if (inList (loopNodes, immedDom) and inInt(pProc->m_dfsLast[i], intNodes))
|
||||
{
|
||||
insertList (loopNodes, i);
|
||||
loopNodes.push_back(i);
|
||||
if (pProc->m_dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/
|
||||
pProc->m_dfsLast[i]->loopHead = headDfsNum;
|
||||
}
|
||||
}
|
||||
latchNode->loopHead = headDfsNum;
|
||||
if (latchNode != head)
|
||||
insertList (loopNodes, latchNode->dfsLastNum);
|
||||
loopNodes.push_back(latchNode->dfsLastNum);
|
||||
|
||||
/* Determine type of loop and follow node */
|
||||
intNodeType = head->nodeType;
|
||||
if (latchNode->nodeType == TWO_BRANCH)
|
||||
if ((intNodeType == TWO_BRANCH) || (latchNode == head))
|
||||
if ((latchNode == head) ||
|
||||
(inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum) &&
|
||||
if ((intNodeType == TWO_BRANCH) or (latchNode == head))
|
||||
if ((latchNode == head) or
|
||||
(inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum) and
|
||||
inList (loopNodes, head->edges[ELSE].BBptr->dfsLastNum)))
|
||||
{
|
||||
head->loopType = eNodeHeaderType::REPEAT_TYPE;
|
||||
@ -238,32 +216,79 @@ static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &int
|
||||
findEndlessFollow (pProc, loopNodes, head);
|
||||
}
|
||||
|
||||
freeList(loopNodes);
|
||||
loopNodes.clear();
|
||||
}
|
||||
/** \returns whether the BB indexed by s is a successor of the BB indexed by \arg h
|
||||
* \note that h is a case node.
|
||||
*/
|
||||
bool successor (int s, int h, Function * pProc)
|
||||
{
|
||||
BB * header = pProc->m_dfsLast[h];
|
||||
auto iter = std::find_if(header->edges.begin(),
|
||||
header->edges.end(),
|
||||
[s](const TYPEADR_TYPE &te)->bool{ return te.BBptr->dfsLastNum == s;});
|
||||
return iter!=header->edges.end();
|
||||
}
|
||||
|
||||
//static void findNodesInInt (queue **intNodes, int level, interval *Ii)
|
||||
/* Recursive procedure to find nodes that belong to the interval (ie. nodes
|
||||
* from G1). */
|
||||
static void findNodesInInt (queue &intNodes, int level, interval *Ii)
|
||||
|
||||
/** Recursive procedure to tag nodes that belong to the case described by
|
||||
* the list l, head and tail (dfsLast index to first and exit node of the
|
||||
* case). */
|
||||
void tagNodesInCase (BB * pBB, nodeList &l, int head, int tail)
|
||||
{
|
||||
if (level == 1)
|
||||
int current; /* index to current node */
|
||||
|
||||
pBB->traversed = DFS_CASE;
|
||||
current = pBB->dfsLastNum;
|
||||
if ((current != tail) and (pBB->nodeType != MULTI_BRANCH) and (inList (l, pBB->immedDom)))
|
||||
{
|
||||
for(BB *en : Ii->nodes)
|
||||
l.push_back(current);
|
||||
pBB->caseHead = head;
|
||||
for(TYPEADR_TYPE &edge : pBB->edges)
|
||||
{
|
||||
appendQueue(intNodes,en);
|
||||
if (edge.BBptr->traversed != DFS_CASE)
|
||||
tagNodesInCase (edge.BBptr, l, head, tail);
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
/** Flags all nodes in the list l as having follow node f, and deletes all
|
||||
* nodes from the list. */
|
||||
void flagNodes (nodeList &l, int f, Function * pProc)
|
||||
{
|
||||
for(BB *en : Ii->nodes)
|
||||
for(int idx : l)
|
||||
{
|
||||
findNodesInInt(intNodes,level-1,en->correspInt);
|
||||
pProc->m_dfsLast[idx]->ifFollow = f;
|
||||
}
|
||||
l.clear();
|
||||
}
|
||||
|
||||
|
||||
} // end of anonymouse namespace
|
||||
|
||||
/** Finds the immediate dominator of each node in the graph pProc->cfg.
|
||||
* Adapted version of the dominators algorithm by Hecht and Ullman; finds
|
||||
* immediate dominators only.
|
||||
* Note: graph should be reducible */
|
||||
void Function::findImmedDom ()
|
||||
{
|
||||
BB * currNode;
|
||||
for (size_t currIdx = 0; currIdx < numBBs; currIdx++)
|
||||
{
|
||||
currNode = m_dfsLast[currIdx];
|
||||
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
|
||||
continue;
|
||||
for (BB * inedge : currNode->inEdges)
|
||||
{
|
||||
size_t predIdx = inedge->dfsLastNum;
|
||||
if (predIdx < currIdx)
|
||||
currNode->immedDom = commonDom (currNode->immedDom, predIdx, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Algorithm for structuring loops */
|
||||
/** Algorithm for structuring loops */
|
||||
void Function::structLoops(derSeq *derivedG)
|
||||
{
|
||||
interval *Ii;
|
||||
@ -279,8 +304,7 @@ void Function::structLoops(derSeq *derivedG)
|
||||
for(auto & elem : *derivedG)
|
||||
{
|
||||
level++;
|
||||
Ii = elem.Ii;
|
||||
while (Ii) /* for all intervals Ii of Gi */
|
||||
for (Ii = elem.Ii; Ii!=nullptr; Ii = Ii->next) /* for all intervals Ii of Gi */
|
||||
{
|
||||
latchNode = nullptr;
|
||||
intNodes.clear();
|
||||
@ -299,9 +323,9 @@ void Function::structLoops(derSeq *derivedG)
|
||||
for (size_t i = 0; i < intHead->inEdges.size(); i++)
|
||||
{
|
||||
pred = intHead->inEdges[i];
|
||||
if (inInt(pred, intNodes) && isBackEdge(pred, intHead))
|
||||
if (inInt(pred, intNodes) and isBackEdge(pred, intHead))
|
||||
{
|
||||
if (! latchNode)
|
||||
if (nullptr == latchNode)
|
||||
latchNode = pred;
|
||||
else if (pred->dfsLastNum > latchNode->dfsLastNum)
|
||||
latchNode = pred;
|
||||
@ -314,7 +338,7 @@ void Function::structLoops(derSeq *derivedG)
|
||||
/* Check latching node is at the same nesting level of case
|
||||
* statements (if any) and that the node doesn't belong to
|
||||
* another loop. */
|
||||
if ((latchNode->caseHead == intHead->caseHead) &&
|
||||
if ((latchNode->caseHead == intHead->caseHead) and
|
||||
(latchNode->loopHead == NO_NODE))
|
||||
{
|
||||
intHead->latchNode = latchNode->dfsLastNum;
|
||||
@ -322,45 +346,6 @@ void Function::structLoops(derSeq *derivedG)
|
||||
latchNode->flg |= IS_LATCH_NODE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Next interval */
|
||||
Ii = Ii->next;
|
||||
}
|
||||
|
||||
/* Next derived sequence */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Returns whether the BB indexed by s is a successor of the BB indexed by
|
||||
* h. Note that h is a case node. */
|
||||
static bool successor (int s, int h, Function * pProc)
|
||||
{
|
||||
BB * header = pProc->m_dfsLast[h];
|
||||
auto iter = std::find_if(header->edges.begin(),
|
||||
header->edges.end(),
|
||||
[s](const TYPEADR_TYPE &te)->bool{ return te.BBptr->dfsLastNum == s;});
|
||||
return iter!=header->edges.end();
|
||||
}
|
||||
|
||||
|
||||
/* Recursive procedure to tag nodes that belong to the case described by
|
||||
* the list l, head and tail (dfsLast index to first and exit node of the
|
||||
* case). */
|
||||
static void tagNodesInCase (BB * pBB, nodeList &l, int head, int tail)
|
||||
{
|
||||
int current; /* index to current node */
|
||||
|
||||
pBB->traversed = DFS_CASE;
|
||||
current = pBB->dfsLastNum;
|
||||
if ((current != tail) && (pBB->nodeType != MULTI_BRANCH) && (inList (l, pBB->immedDom)))
|
||||
{
|
||||
insertList (l, current);
|
||||
pBB->caseHead = head;
|
||||
for(TYPEADR_TYPE &edge : pBB->edges)
|
||||
{
|
||||
if (edge.BBptr->traversed != DFS_CASE)
|
||||
tagNodesInCase (edge.BBptr, l, head, tail);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -385,12 +370,10 @@ void Function::structCases()
|
||||
* the current header node, and is not a successor. */
|
||||
for (size_t j = i + 2; j < numBBs; j++)
|
||||
{
|
||||
if ((!successor(j, i, this)) && (m_dfsLast[j]->immedDom == i))
|
||||
if ((not successor(j, i, this)) and (m_dfsLast[j]->immedDom == i))
|
||||
{
|
||||
if (exitNode == NO_NODE)
|
||||
{
|
||||
exitNode = j;
|
||||
}
|
||||
else if (m_dfsLast[exitNode]->inEdges.size() < m_dfsLast[j]->inEdges.size())
|
||||
exitNode = j;
|
||||
}
|
||||
@ -399,7 +382,7 @@ void Function::structCases()
|
||||
|
||||
/* Tag nodes that belong to the case by recording the
|
||||
* header field with caseHeader. */
|
||||
insertList (caseNodes, i);
|
||||
caseNodes.push_back(i);
|
||||
m_dfsLast[i]->caseHead = i;
|
||||
for(TYPEADR_TYPE &pb : caseHeader->edges)
|
||||
{
|
||||
@ -412,20 +395,6 @@ void Function::structCases()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Flags all nodes in the list l as having follow node f, and deletes all
|
||||
* nodes from the list. */
|
||||
static void flagNodes (nodeList &l, int f, Function * pProc)
|
||||
{
|
||||
nodeList::iterator p;
|
||||
for(int idx : l)
|
||||
{
|
||||
pProc->m_dfsLast[idx]->ifFollow = f;
|
||||
}
|
||||
l.clear();
|
||||
}
|
||||
|
||||
|
||||
/* Structures if statements */
|
||||
void Function::structIfs ()
|
||||
{
|
||||
@ -446,7 +415,7 @@ void Function::structIfs ()
|
||||
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
|
||||
continue;
|
||||
|
||||
if ((currNode->nodeType == TWO_BRANCH) && (!currNode->back().ll()->testFlags(JX_LOOP)))
|
||||
if ((currNode->nodeType == TWO_BRANCH) and (not currNode->back().ll()->testFlags(JX_LOOP)))
|
||||
{
|
||||
followInEdges = 0;
|
||||
follow = 0;
|
||||
@ -456,7 +425,7 @@ void Function::structIfs ()
|
||||
{
|
||||
if (m_dfsLast[desc]->immedDom == curr)
|
||||
{
|
||||
insertList (domDesc, desc);
|
||||
domDesc.push_back(desc);
|
||||
pbb = m_dfsLast[desc];
|
||||
if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges)
|
||||
{
|
||||
@ -468,16 +437,16 @@ void Function::structIfs ()
|
||||
|
||||
/* Determine follow according to number of descendants
|
||||
* immediately dominated by this node */
|
||||
if ((follow != 0) && (followInEdges > 1))
|
||||
if ((follow != 0) and (followInEdges > 1))
|
||||
{
|
||||
currNode->ifFollow = follow;
|
||||
if (!unresolved.empty())
|
||||
if (not unresolved.empty())
|
||||
flagNodes (unresolved, follow, this);
|
||||
}
|
||||
else
|
||||
insertList (unresolved, curr);
|
||||
unresolved.push_back(curr);
|
||||
}
|
||||
freeList (domDesc);
|
||||
domDesc.clear();
|
||||
}
|
||||
}
|
||||
bool Function::removeInEdge_Flag_and_ProcessLatch(BB *pbb,BB *a,BB *b)
|
||||
@ -617,33 +586,33 @@ void Function::compoundCond()
|
||||
|
||||
change = true; //assume change
|
||||
|
||||
/* Check (X || Y) case */
|
||||
if ((thenBB->nodeType == TWO_BRANCH) && (thenBB->numHlIcodes == 1) &&
|
||||
(thenBB->inEdges.size() == 1) && (thenBB->edges[ELSE].BBptr == elseBB))
|
||||
/* Check (X or Y) case */
|
||||
if ((thenBB->nodeType == TWO_BRANCH) and (thenBB->numHlIcodes == 1) and
|
||||
(thenBB->inEdges.size() == 1) and (thenBB->edges[ELSE].BBptr == elseBB))
|
||||
{
|
||||
if(Case_X_or_Y(pbb, thenBB, elseBB))
|
||||
--i;
|
||||
}
|
||||
|
||||
/* Check (!X && Y) case */
|
||||
else if ((thenBB->nodeType == TWO_BRANCH) && (thenBB->numHlIcodes == 1) &&
|
||||
(thenBB->inEdges.size() == 1) && (thenBB->edges[THEN].BBptr == elseBB))
|
||||
/* Check (not X and Y) case */
|
||||
else if ((thenBB->nodeType == TWO_BRANCH) and (thenBB->numHlIcodes == 1) and
|
||||
(thenBB->inEdges.size() == 1) and (thenBB->edges[THEN].BBptr == elseBB))
|
||||
{
|
||||
if(Case_notX_and_Y(pbb, thenBB, elseBB))
|
||||
--i;
|
||||
}
|
||||
|
||||
/* Check (X && Y) case */
|
||||
else if ((elseBB->nodeType == TWO_BRANCH) && (elseBB->numHlIcodes == 1) &&
|
||||
(elseBB->inEdges.size()==1) && (elseBB->edges[THEN].BBptr == thenBB))
|
||||
/* Check (X and Y) case */
|
||||
else if ((elseBB->nodeType == TWO_BRANCH) and (elseBB->numHlIcodes == 1) and
|
||||
(elseBB->inEdges.size()==1) and (elseBB->edges[THEN].BBptr == thenBB))
|
||||
{
|
||||
if(Case_X_and_Y(pbb, thenBB, elseBB ))
|
||||
--i;
|
||||
}
|
||||
|
||||
/* Check (!X || Y) case */
|
||||
else if ((elseBB->nodeType == TWO_BRANCH) && (elseBB->numHlIcodes == 1) &&
|
||||
(elseBB->inEdges.size() == 1) && (elseBB->edges[ELSE].BBptr == thenBB))
|
||||
/* Check (not X or Y) case */
|
||||
else if ((elseBB->nodeType == TWO_BRANCH) and (elseBB->numHlIcodes == 1) and
|
||||
(elseBB->inEdges.size() == 1) and (elseBB->edges[ELSE].BBptr == thenBB))
|
||||
{
|
||||
if(Case_notX_or_Y(pbb, thenBB, elseBB ))
|
||||
--i;
|
||||
@ -654,8 +623,7 @@ void Function::compoundCond()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Structuring algorithm to find the structures of the graph pProc->cfg */
|
||||
/** Structuring algorithm to find the structures of the graph pProc->cfg */
|
||||
void Function::structure(derSeq *derivedG)
|
||||
{
|
||||
/* Find immediate dominators of the graph */
|
||||
|
||||
350
src/dataflow.cpp
350
src/dataflow.cpp
@ -4,20 +4,27 @@
|
||||
* Purpose: Data flow analysis module.
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptor/filtered.hpp>
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <boost/assign.hpp>
|
||||
#include <stdint.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdio>
|
||||
#include <boost/range.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
#include <boost/range/algorithm.hpp>
|
||||
#include <boost/assign.hpp>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
using namespace boost;
|
||||
using namespace boost::adaptors;
|
||||
using namespace std;
|
||||
namespace
|
||||
{
|
||||
struct ExpStack
|
||||
{
|
||||
Function *func;
|
||||
@ -28,7 +35,7 @@ struct ExpStack
|
||||
void push(Expr *);
|
||||
Expr * pop();
|
||||
Expr * top() const {
|
||||
if(!expStk.empty())
|
||||
if(not expStk.empty())
|
||||
return expStk.back();
|
||||
return nullptr;
|
||||
}
|
||||
@ -45,6 +52,57 @@ struct ExpStack
|
||||
}
|
||||
|
||||
};
|
||||
ExpStack g_exp_stk;
|
||||
/** Returns a string with the source operand of Icode */
|
||||
Expr *srcIdent (const LLInst &ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
|
||||
{
|
||||
const LLOperand * src_op = ll_insn.get(SRC);
|
||||
if (src_op->isImmediate()) /* immediate operand ll_insn.testFlags(I)*/
|
||||
{
|
||||
//if (ll_insn.testFlags(B))
|
||||
return new Constant(src_op->getImm2(), src_op->byteWidth());
|
||||
}
|
||||
// otherwise
|
||||
return AstIdent::id (ll_insn, SRC, pProc, i, duIcode, du);
|
||||
}
|
||||
|
||||
|
||||
/** Returns the destination operand */
|
||||
Expr *dstIdent (const LLInst & ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
|
||||
{
|
||||
Expr *n;
|
||||
n = AstIdent::id (ll_insn, DST, pProc, i, duIcode, du);
|
||||
/** Is it needed? (pIcode->ll()->flg) & NO_SRC_B **/
|
||||
return (n);
|
||||
}
|
||||
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the expression exp given */
|
||||
void forwardSubsLong (int longIdx, Expr *_exp, ICODE &picode, ICODE &ticode, int *numHlIcodes)
|
||||
{
|
||||
bool res;
|
||||
|
||||
if (_exp == nullptr) /* In case expression popped is NULL */
|
||||
return;
|
||||
|
||||
/* Insert on rhs of ticode, if possible */
|
||||
res = Expr::insertSubTreeLongReg (_exp, ticode.hlU()->asgn.m_rhs, longIdx);
|
||||
if (res)
|
||||
{
|
||||
picode.invalidate();
|
||||
(*numHlIcodes)--;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Try to insert it on lhs of ticode*/
|
||||
res = Expr::insertSubTreeLongReg (_exp, ticode.hlU()->asgn.m_lhs, longIdx);
|
||||
if (res)
|
||||
{
|
||||
picode.invalidate();
|
||||
(*numHlIcodes)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // end of anonymous namespace
|
||||
/***************************************************************************
|
||||
* Expression stack functions
|
||||
**************************************************************************/
|
||||
@ -88,8 +146,6 @@ bool ExpStack::empty()
|
||||
return expStk.empty();
|
||||
}
|
||||
|
||||
using namespace std;
|
||||
ExpStack g_exp_stk;
|
||||
|
||||
/* Returns the index of the local variable or parameter at offset off, if it
|
||||
* is in the stack frame provided. */
|
||||
@ -100,28 +156,6 @@ size_t STKFRAME::getLocVar(int off)
|
||||
}
|
||||
|
||||
|
||||
/* Returns a string with the source operand of Icode */
|
||||
static Expr *srcIdent (const LLInst &ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
|
||||
{
|
||||
const LLOperand * src_op = ll_insn.get(SRC);
|
||||
if (src_op->isImmediate()) /* immediate operand ll_insn.testFlags(I)*/
|
||||
{
|
||||
//if (ll_insn.testFlags(B))
|
||||
return new Constant(src_op->getImm2(), src_op->byteWidth());
|
||||
}
|
||||
// otherwise
|
||||
return AstIdent::id (ll_insn, SRC, pProc, i, duIcode, du);
|
||||
}
|
||||
|
||||
|
||||
/* Returns the destination operand */
|
||||
static Expr *dstIdent (const LLInst & ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
|
||||
{
|
||||
Expr *n;
|
||||
n = AstIdent::id (ll_insn, DST, pProc, i, duIcode, du);
|
||||
/** Is it needed? (pIcode->ll()->flg) & NO_SRC_B **/
|
||||
return (n);
|
||||
}
|
||||
/* Eliminates all condition codes and generates new hlIcode instructions */
|
||||
void Function::elimCondCodes ()
|
||||
{
|
||||
@ -145,7 +179,7 @@ void Function::elimCondCodes ()
|
||||
{
|
||||
llIcode useAtOp = llIcode(useAt->ll()->getOpcode());
|
||||
use = useAt->ll()->flagDU.u;
|
||||
if ((useAt->type != LOW_LEVEL) || ( ! useAt->valid() ) || ( 0 == use ))
|
||||
if ((useAt->type != LOW_LEVEL_ICODE) or ( not useAt->valid() ) or ( 0 == use ))
|
||||
continue;
|
||||
/* Find definition within the same basic block */
|
||||
defAt=useAt;
|
||||
@ -158,7 +192,7 @@ void Function::elimCondCodes ()
|
||||
continue;
|
||||
notSup = false;
|
||||
LLOperand *dest_ll = defIcode.ll()->get(DST);
|
||||
if ((useAtOp >= iJB) && (useAtOp <= iJNS))
|
||||
if ((useAtOp >= iJB) and (useAtOp <= iJNS))
|
||||
{
|
||||
iICODE befDefAt = (++riICODE(defAt)).base();
|
||||
switch (defIcode.ll()->getOpcode())
|
||||
@ -194,7 +228,7 @@ void Function::elimCondCodes ()
|
||||
reportError (JX_NOT_DEF, defIcode.ll()->getOpcode());
|
||||
flg |= PROC_ASM; /* generate asm */
|
||||
}
|
||||
if (! notSup)
|
||||
if (not notSup)
|
||||
{
|
||||
assert(lhs);
|
||||
assert(rhs);
|
||||
@ -219,14 +253,14 @@ void Function::elimCondCodes ()
|
||||
{
|
||||
ICODE &a(*defAt);
|
||||
ICODE &b(*useAt);
|
||||
reportError (NOT_DEF_USE,a.ll()->getOpcode(),b.ll()->getOpcode());
|
||||
reportError (NOT_DEF_USE,a.ll()->label,a.ll()->getOpcode(),b.ll()->getOpcode());
|
||||
flg |= PROC_ASM; /* generate asm */
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check for extended basic block */
|
||||
if ((pBB->size() == 1) &&(useAtOp >= iJB) && (useAtOp <= iJNS))
|
||||
if ((pBB->size() == 1) and(useAtOp >= iJB) and (useAtOp <= iJNS))
|
||||
{
|
||||
ICODE & _prev(pBB->back()); /* For extended basic blocks - previous icode inst */
|
||||
if (_prev.hl()->opcode == HLI_JCOND)
|
||||
@ -268,7 +302,7 @@ void Function::genLiveKtes ()
|
||||
continue; // skip invalid BBs
|
||||
for(ICODE &insn : *pbb)
|
||||
{
|
||||
if ((insn.type == HIGH_LEVEL) && ( insn.valid() ))
|
||||
if ((insn.type == HIGH_LEVEL_ICODE) and ( insn.valid() ))
|
||||
{
|
||||
liveUse |= (insn.du.use - def);
|
||||
def |= insn.du.def;
|
||||
@ -342,7 +376,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
|
||||
pcallee = ticode.hl()->call.proc;
|
||||
|
||||
/* user/runtime routine */
|
||||
if (! (pcallee->flg & PROC_ISLIB))
|
||||
if (not (pcallee->flg & PROC_ISLIB))
|
||||
{
|
||||
if (pcallee->liveAnal == false) /* hasn't been processed */
|
||||
pcallee->dataFlow (pbb->liveOut);
|
||||
@ -350,7 +384,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
|
||||
}
|
||||
else /* library routine */
|
||||
{
|
||||
if ( (pcallee->flg & PROC_IS_FUNC) && /* returns a value */
|
||||
if ( (pcallee->flg & PROC_IS_FUNC) and /* returns a value */
|
||||
(pcallee->liveOut & pbb->edges[0].BBptr->liveIn).any()
|
||||
)
|
||||
pbb->liveOut = pcallee->liveOut;
|
||||
@ -358,10 +392,11 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
|
||||
pbb->liveOut.reset();
|
||||
}
|
||||
|
||||
if ((! (pcallee->flg & PROC_ISLIB)) || ( pbb->liveOut.any() ))
|
||||
if ((not (pcallee->flg & PROC_ISLIB)) or ( pbb->liveOut.any() ))
|
||||
{
|
||||
switch (pcallee->retVal.type) {
|
||||
case TYPE_LONG_SIGN: case TYPE_LONG_UNSIGN:
|
||||
case TYPE_LONG_SIGN:
|
||||
case TYPE_LONG_UNSIGN:
|
||||
ticode.du1.setDef(rAX).addDef(rDX);
|
||||
//TODO: use Calling convention to properly set regs here
|
||||
break;
|
||||
@ -385,7 +420,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
|
||||
pbb->liveIn = LivenessSet(pbb->liveUse + (pbb->liveOut - pbb->def));
|
||||
|
||||
/* Check if live sets have been modified */
|
||||
if ((prevLiveIn != pbb->liveIn) || (prevLiveOut != pbb->liveOut))
|
||||
if ((prevLiveIn != pbb->liveIn) or (prevLiveOut != pbb->liveOut))
|
||||
change = true;
|
||||
}
|
||||
}
|
||||
@ -412,9 +447,9 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
|
||||
* register */
|
||||
bool BB::FindUseBeforeDef(eReg regi, int defRegIdx, iICODE start_at)
|
||||
{
|
||||
if ((regi == rDI) && (flg & DI_REGVAR))
|
||||
if ((regi == rDI) and (flg & DI_REGVAR))
|
||||
return true;
|
||||
if ((regi == rSI) && (flg & SI_REGVAR))
|
||||
if ((regi == rSI) and (flg & SI_REGVAR))
|
||||
return true;
|
||||
if (distance(start_at,end())>1) /* several instructions */
|
||||
{
|
||||
@ -457,7 +492,7 @@ bool BB::FindUseBeforeDef(eReg regi, int defRegIdx, iICODE start_at)
|
||||
* on optimized code. */
|
||||
void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
|
||||
{
|
||||
if (!((picode.hl()->opcode == HLI_CALL) && (picode.hl()->call.proc->flg & PROC_IS_FUNC)))
|
||||
if (not ((picode.hl()->opcode == HLI_CALL) and (picode.hl()->call.proc->flg & PROC_IS_FUNC)))
|
||||
return;
|
||||
|
||||
BB *tbb = this->edges[0].BBptr;
|
||||
@ -475,7 +510,7 @@ void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
|
||||
/* if not used in this basic block, check if the
|
||||
* register is live out, if so, make it the last
|
||||
* definition of this register */
|
||||
if ( picode.du1.used(defRegIdx) && tbb->liveOut.testRegAndSubregs(regi))
|
||||
if ( picode.du1.used(defRegIdx) and tbb->liveOut.testRegAndSubregs(regi))
|
||||
picode.du.lastDefRegi.addReg(regi);
|
||||
}
|
||||
|
||||
@ -488,11 +523,11 @@ void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
|
||||
void BB::RemoveUnusedDefs(eReg regi, int defRegIdx, iICODE picode)
|
||||
{
|
||||
if (picode->valid() and not picode->du1.used(defRegIdx) and
|
||||
(not picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
|
||||
(not ((picode->hl()->opcode == HLI_CALL) &&
|
||||
(not picode->du.lastDefRegi.testRegAndSubregs(regi)) and
|
||||
(not ((picode->hl()->opcode == HLI_CALL) and
|
||||
(picode->hl()->call.proc->flg & PROC_ISLIB))))
|
||||
{
|
||||
if (! (this->liveOut.testRegAndSubregs(regi))) /* not liveOut */
|
||||
if (not (this->liveOut.testRegAndSubregs(regi))) /* not liveOut */
|
||||
{
|
||||
bool res = picode->removeDefRegi (regi, defRegIdx+1,&Parent->localId);
|
||||
if (res == true)
|
||||
@ -540,7 +575,7 @@ void BB::genDU1()
|
||||
defRegIdx++;
|
||||
|
||||
/* Check if all defined registers have been processed */
|
||||
if ((defRegIdx >= picode->du1.getNumRegsDef()) || (defRegIdx == MAX_REGS_DEF))
|
||||
if ((defRegIdx >= picode->du1.getNumRegsDef()) or (defRegIdx == MAX_REGS_DEF))
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -558,7 +593,7 @@ void Function::genDU1 ()
|
||||
}
|
||||
|
||||
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs of picode. */
|
||||
void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const
|
||||
void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, ICODE &picode, ICODE &ticode, int &numHlIcodes) const
|
||||
{
|
||||
bool res;
|
||||
UnaryOperator *lhs_unary;
|
||||
@ -574,64 +609,37 @@ void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode,
|
||||
return;
|
||||
|
||||
/* Insert on rhs of ticode, if possible */
|
||||
res = Expr::insertSubTreeReg (ticode->hlU()->asgn.rhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
|
||||
res = Expr::insertSubTreeReg (ticode.hlU()->asgn.m_rhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
picode.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Try to insert it on lhs of ticode*/
|
||||
RegisterNode *op = dynamic_cast<RegisterNode *>(ticode->hlU()->asgn.m_lhs);
|
||||
RegisterNode *op = dynamic_cast<RegisterNode *>(ticode.hlU()->asgn.m_lhs);
|
||||
if(op)
|
||||
{
|
||||
eReg inserted = id_arr[lhs_reg->regiIdx].id.regi;
|
||||
eReg lhsReg = id_arr[op->regiIdx].id.regi;
|
||||
if((lhsReg==inserted)||Machine_X86::isSubRegisterOf(lhsReg,inserted))
|
||||
if((lhsReg==inserted) or Machine_X86::isSubRegisterOf(lhsReg,inserted))
|
||||
{
|
||||
// Do not replace ax = XYZ; given ax = H << P; with H << P =
|
||||
return;
|
||||
}
|
||||
}
|
||||
res = Expr::insertSubTreeReg (ticode->hlU()->asgn.m_lhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
|
||||
res = Expr::insertSubTreeReg (ticode.hlU()->asgn.m_lhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
picode.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the expression exp given */
|
||||
static void forwardSubsLong (int longIdx, Expr *_exp, iICODE picode, iICODE ticode, int *numHlIcodes)
|
||||
{
|
||||
bool res;
|
||||
|
||||
if (_exp == nullptr) /* In case expression popped is NULL */
|
||||
return;
|
||||
|
||||
/* Insert on rhs of ticode, if possible */
|
||||
res = Expr::insertSubTreeLongReg (_exp, ticode->hlU()->asgn.rhs, longIdx);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
(*numHlIcodes)--;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Try to insert it on lhs of ticode*/
|
||||
res = Expr::insertSubTreeLongReg (_exp, ticode->hlU()->asgn.m_lhs, longIdx);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
(*numHlIcodes)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns whether the elements of the expression rhs are all x-clear from
|
||||
/** Returns whether the elements of the expression rhs are all x-clear from
|
||||
* instruction f up to instruction t. */
|
||||
bool UnaryOperator::xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs)
|
||||
{
|
||||
@ -644,7 +652,7 @@ bool BinaryOperator::xClear(rICODE range_to_check, iICODE lastBBinst, const LOCA
|
||||
{
|
||||
if(nullptr==m_rhs)
|
||||
return false;
|
||||
if ( ! m_rhs->xClear (range_to_check, lastBBinst, locs) )
|
||||
if ( not m_rhs->xClear (range_to_check, lastBBinst, locs) )
|
||||
return false;
|
||||
if(nullptr==m_lhs)
|
||||
return false;
|
||||
@ -674,7 +682,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
|
||||
return; */
|
||||
assert(pProc==g_exp_stk.func);
|
||||
_exp = g_exp_stk.pop();
|
||||
if (callee->flg & PROC_ISLIB) /* library function */
|
||||
if (callee->isLibrary() ) /* library function */
|
||||
{
|
||||
if (callee->args.numArgs > 0)
|
||||
{
|
||||
@ -690,7 +698,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
|
||||
else {
|
||||
if(numArgs<callee->args.size()) {
|
||||
if(prog.addressingMode=='l') {
|
||||
if((callee->args[numArgs].type==TYPE_STR)||(callee->args[numArgs].type==TYPE_PTR)) {
|
||||
if((callee->args[numArgs].type==TYPE_STR) or (callee->args[numArgs].type==TYPE_PTR)) {
|
||||
RegisterNode *rn = dynamic_cast<RegisterNode *>(g_exp_stk.top());
|
||||
AstIdent *idn = dynamic_cast<AstIdent *>(g_exp_stk.top());
|
||||
if(rn) {
|
||||
@ -727,7 +735,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
|
||||
callee->args.adjustForArgType (numArgs, _exp->expType (pProc));
|
||||
}
|
||||
}
|
||||
res = picode->newStkArg (_exp, (llIcode)picode->ll()->getOpcode(), pProc);
|
||||
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), pProc);
|
||||
/* Do not update the size of k if the expression was a segment register
|
||||
* in a near call */
|
||||
if (res == false)
|
||||
@ -742,11 +750,11 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
|
||||
/** Eliminates extraneous intermediate icode instructions when finding
|
||||
* expressions. Generates new hlIcodes in the form of expression trees.
|
||||
* For HLI_CALL hlIcodes, places the arguments in the argument list. */
|
||||
void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode,bool isLong) const
|
||||
void LOCAL_ID::processTargetIcode(ICODE &picode, int &numHlIcodes, ICODE &ticode,bool isLong) const
|
||||
{
|
||||
bool res;
|
||||
HLTYPE &p_hl(*picode->hlU());
|
||||
HLTYPE &t_hl(*ticode->hlU());
|
||||
HLTYPE &p_hl(*picode.hlU());
|
||||
HLTYPE &t_hl(*ticode.hlU());
|
||||
|
||||
AstIdent *lhs_ident = dynamic_cast<AstIdent *>(p_hl.asgn.lhs());
|
||||
switch (t_hl.opcode)
|
||||
@ -756,11 +764,11 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
|
||||
if(isLong)
|
||||
{
|
||||
forwardSubsLong (lhs_ident->ident.idNode.longIdx,
|
||||
p_hl.asgn.rhs, picode,ticode,
|
||||
p_hl.asgn.m_rhs, picode,ticode,
|
||||
&numHlIcodes);
|
||||
}
|
||||
else
|
||||
this->forwardSubs (lhs_ident, p_hl.asgn.rhs, picode, ticode, numHlIcodes);
|
||||
this->forwardSubs (lhs_ident, p_hl.asgn.m_rhs, picode, ticode, numHlIcodes);
|
||||
break;
|
||||
|
||||
case HLI_JCOND: case HLI_PUSH: case HLI_RET:
|
||||
@ -768,7 +776,7 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
|
||||
{
|
||||
assert(lhs_ident);
|
||||
res = Expr::insertSubTreeLongReg (
|
||||
p_hl.asgn.rhs,
|
||||
p_hl.asgn.m_rhs,
|
||||
t_hl.exp.v,
|
||||
lhs_ident->ident.idNode.longIdx);
|
||||
}
|
||||
@ -778,20 +786,20 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
|
||||
assert(lhs_reg);
|
||||
res = Expr::insertSubTreeReg (
|
||||
t_hl.exp.v,
|
||||
p_hl.asgn.rhs,
|
||||
p_hl.asgn.m_rhs,
|
||||
id_arr[lhs_reg->regiIdx].id.regi,
|
||||
this);
|
||||
}
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
picode.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
break;
|
||||
|
||||
case HLI_CALL: /* register arguments */
|
||||
newRegArg ( picode, ticode);
|
||||
picode->invalidate();
|
||||
picode.invalidate();
|
||||
numHlIcodes--;
|
||||
break;
|
||||
default:
|
||||
@ -815,9 +823,9 @@ void C_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE picode) {
|
||||
numArgs++;
|
||||
}
|
||||
}
|
||||
else if ((cb == 0) && picode->ll()->testFlags(REST_STK))
|
||||
else if ((cb == 0) and picode->ll()->testFlags(REST_STK))
|
||||
{
|
||||
while (! g_exp_stk.empty())
|
||||
while (not g_exp_stk.empty())
|
||||
{
|
||||
k+=processCArg (pp, func, &(*picode), numArgs);
|
||||
numArgs++;
|
||||
@ -838,11 +846,11 @@ void Pascal_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE pico
|
||||
while(k<cb)
|
||||
{
|
||||
_exp = g_exp_stk.pop();
|
||||
if (pp->flg & PROC_ISLIB) /* library function */
|
||||
if (pp->isLibrary() ) /* library function */
|
||||
{
|
||||
if (pp->args.numArgs > 0)
|
||||
_exp = func->adjustActArgType(_exp, pp->args[numArgs].type);
|
||||
res = picode->newStkArg (_exp, (llIcode)picode->ll()->getOpcode(), func);
|
||||
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), func);
|
||||
}
|
||||
else /* user function */
|
||||
{
|
||||
@ -852,9 +860,10 @@ void Pascal_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE pico
|
||||
{
|
||||
fprintf(stderr,"Would try to adjustForArgType with null _exp\n");
|
||||
}
|
||||
else
|
||||
pp->args.adjustForArgType (numArgs,_exp->expType (func));
|
||||
}
|
||||
res = picode->newStkArg (_exp,(llIcode)picode->ll()->getOpcode(), func);
|
||||
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), func);
|
||||
}
|
||||
if (res == false)
|
||||
k += _exp->hlTypeSize (func);
|
||||
@ -875,28 +884,28 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
|
||||
ID *_retVal; // function return value
|
||||
Expr *_exp; // expression pointer - for HLI_POP and HLI_CALL */
|
||||
//Expr *lhs; // exp ptr for return value of a HLI_CALL */
|
||||
iICODE ticode; // Target icode */
|
||||
HLTYPE *ti_hl=nullptr;
|
||||
uint8_t regi;
|
||||
numHlIcodes = 0;
|
||||
assert(&fnc->localId==&locals);
|
||||
// register(s) to be forward substituted */
|
||||
auto valid_and_highlevel = instructions | filtered(ICODE::TypeAndValidFilter<HIGH_LEVEL>());
|
||||
auto valid_and_highlevel = instructions | filtered(ICODE::TypeAndValidFilter<HIGH_LEVEL_ICODE>());
|
||||
for (auto picode = valid_and_highlevel.begin(); picode != valid_and_highlevel.end(); picode++)
|
||||
{
|
||||
HLTYPE &_icHl(*picode->hlU());
|
||||
ICODE &_ic(*picode);
|
||||
HLTYPE &_icHl(*_ic.hlU());
|
||||
numHlIcodes++;
|
||||
if (picode->du1.getNumRegsDef() == 1) /* uint8_t/uint16_t regs */
|
||||
if (_ic.du1.getNumRegsDef() == 1) /* uint8_t/uint16_t regs */
|
||||
{
|
||||
/* Check for only one use of this register. If this is
|
||||
* the last definition of the register in this BB, check
|
||||
* that it is not liveOut from this basic block */
|
||||
if (picode->du1.numUses(0)==1)
|
||||
if (_ic.du1.numUses(0)==1)
|
||||
{
|
||||
/* Check that this register is not liveOut, if it
|
||||
* is the last definition of the register */
|
||||
regi = picode->du1.regi[0];
|
||||
regi = _ic.du1.regi[0];
|
||||
|
||||
/* Check if we can forward substitute this register */
|
||||
switch (_icHl.opcode)
|
||||
@ -905,16 +914,16 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
/* Replace rhs of current icode into target
|
||||
* icode expression */
|
||||
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
|
||||
((ticode->hl()->opcode != HLI_CALL) &&
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
|
||||
((ticode->hl()->opcode != HLI_CALL) and
|
||||
(ticode->hl()->opcode != HLI_RET)))
|
||||
continue;
|
||||
|
||||
if (_icHl.asgn.rhs->xClear (make_iterator_range(picode.base(),picode->du1.idx[0].uses[0]),
|
||||
if (_icHl.asgn.m_rhs->xClear (make_iterator_range(picode.base(),_ic.du1.idx[0].uses[0]),
|
||||
end(), locals))
|
||||
{
|
||||
locals.processTargetIcode(picode.base(), numHlIcodes, ticode,false);
|
||||
locals.processTargetIcode(_ic, numHlIcodes, *ticode,false);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -923,17 +932,17 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
// pop X
|
||||
// lab1:
|
||||
// call F() <- somehow this is marked as user of POP ?
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
ti_hl = ticode->hlU();
|
||||
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
|
||||
((ti_hl->opcode != HLI_CALL) &&
|
||||
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
|
||||
((ti_hl->opcode != HLI_CALL) and
|
||||
(ti_hl->opcode != HLI_RET)))
|
||||
continue;
|
||||
|
||||
_exp = g_exp_stk.pop(); /* pop last exp pushed */
|
||||
switch (ticode->hl()->opcode) {
|
||||
case HLI_ASSIGN:
|
||||
locals.forwardSubs(_icHl.expr(), _exp, picode.base(), ticode, numHlIcodes);
|
||||
locals.forwardSubs(_icHl.expr(), _exp, _ic, *ticode, numHlIcodes);
|
||||
break;
|
||||
|
||||
case HLI_JCOND: case HLI_PUSH: case HLI_RET:
|
||||
@ -946,7 +955,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
&locals);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
}
|
||||
@ -964,25 +973,25 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
break;
|
||||
|
||||
case HLI_CALL:
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
ti_hl = ticode->hlU();
|
||||
_retVal = &_icHl.call.proc->retVal;
|
||||
switch (ti_hl->opcode)
|
||||
{
|
||||
case HLI_ASSIGN:
|
||||
assert(ti_hl->asgn.rhs);
|
||||
assert(ti_hl->asgn.m_rhs);
|
||||
_exp = _icHl.call.toAst();
|
||||
res = Expr::insertSubTreeReg (ti_hl->asgn.rhs,_exp, _retVal->id.regi, &locals);
|
||||
if (! res)
|
||||
res = Expr::insertSubTreeReg (ti_hl->asgn.m_rhs,_exp, _retVal->id.regi, &locals);
|
||||
if (not res)
|
||||
Expr::insertSubTreeReg (ti_hl->asgn.m_lhs, _exp,_retVal->id.regi, &locals);
|
||||
//TODO: HERE missing: 2 regs
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
break;
|
||||
|
||||
case HLI_PUSH: case HLI_RET:
|
||||
ti_hl->expr( _icHl.call.toAst() );
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
break;
|
||||
|
||||
@ -991,13 +1000,13 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
res = Expr::insertSubTreeReg (ti_hl->exp.v, _exp, _retVal->id.regi, &locals);
|
||||
if (res) /* was substituted */
|
||||
{
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
else /* cannot substitute function */
|
||||
{
|
||||
auto lhs = AstIdent::idID(_retVal,&locals,picode.base());
|
||||
picode->setAsgn(lhs, _exp);
|
||||
_ic.setAsgn(lhs, _exp);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -1010,35 +1019,35 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
}
|
||||
}
|
||||
|
||||
else if (picode->du1.getNumRegsDef() == 2) /* long regs */
|
||||
else if (_ic.du1.getNumRegsDef() == 2) /* long regs */
|
||||
{
|
||||
/* Check for only one use of these registers */
|
||||
if ((picode->du1.numUses(0) == 1) and (picode->du1.numUses(1) == 1))
|
||||
if ((_ic.du1.numUses(0) == 1) and (_ic.du1.numUses(1) == 1))
|
||||
{
|
||||
regi = picode->du1.regi[0]; //TODO: verify that regi actually should be assigned this
|
||||
regi = _ic.du1.regi[0]; //TODO: verify that regi actually should be assigned this
|
||||
|
||||
switch (_icHl.opcode)
|
||||
{
|
||||
case HLI_ASSIGN:
|
||||
/* Replace rhs of current icode into target
|
||||
* icode expression */
|
||||
if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
|
||||
if (_ic.du1.idx[0].uses[0] == _ic.du1.idx[1].uses[0])
|
||||
{
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
|
||||
((ticode->hl()->opcode != HLI_CALL) &&
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
|
||||
((ticode->hl()->opcode != HLI_CALL) and
|
||||
(ticode->hl()->opcode != HLI_RET)))
|
||||
continue;
|
||||
locals.processTargetIcode(picode.base(), numHlIcodes, ticode,true);
|
||||
locals.processTargetIcode(_ic, numHlIcodes, *ticode,true);
|
||||
}
|
||||
break;
|
||||
|
||||
case HLI_POP:
|
||||
if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
|
||||
if (_ic.du1.idx[0].uses[0] == _ic.du1.idx[1].uses[0])
|
||||
{
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
|
||||
((ticode->hl()->opcode != HLI_CALL) &&
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
|
||||
((ticode->hl()->opcode != HLI_CALL) and
|
||||
(ticode->hl()->opcode != HLI_RET)))
|
||||
continue;
|
||||
|
||||
@ -1046,7 +1055,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
switch (ticode->hl()->opcode) {
|
||||
case HLI_ASSIGN:
|
||||
forwardSubsLong (dynamic_cast<AstIdent *>(_icHl.expr())->ident.idNode.longIdx,
|
||||
_exp, picode.base(), ticode, &numHlIcodes);
|
||||
_exp, _ic, *ticode, &numHlIcodes);
|
||||
break;
|
||||
case HLI_JCOND: case HLI_PUSH:
|
||||
res = Expr::insertSubTreeLongReg (_exp,
|
||||
@ -1054,7 +1063,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
dynamic_cast<AstIdent *>(_icHl.asgn.lhs())->ident.idNode.longIdx);
|
||||
if (res)
|
||||
{
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
break;
|
||||
@ -1067,7 +1076,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
break;
|
||||
|
||||
case HLI_CALL: /* check for function return */
|
||||
ticode = picode->du1.idx[0].uses.front();
|
||||
ticode = _ic.du1.idx[0].uses.front();
|
||||
switch (ticode->hl()->opcode)
|
||||
{
|
||||
case HLI_ASSIGN:
|
||||
@ -1076,33 +1085,33 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
AstIdent::Long(&locals, DST,
|
||||
ticode,HIGH_FIRST, picode.base(),
|
||||
eDEF, *(++iICODE(ticode))->ll()));
|
||||
ticode->hlU()->asgn.rhs = _exp;
|
||||
picode->invalidate();
|
||||
ticode->hlU()->asgn.m_rhs = _exp;
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
break;
|
||||
|
||||
case HLI_PUSH:
|
||||
case HLI_RET:
|
||||
ticode->hlU()->expr( _icHl.call.toAst() );
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
break;
|
||||
|
||||
case HLI_JCOND:
|
||||
_exp = _icHl.call.toAst();
|
||||
_retVal = &picode->hl()->call.proc->retVal;
|
||||
_retVal = &_ic.hl()->call.proc->retVal;
|
||||
res = Expr::insertSubTreeLongReg (_exp,
|
||||
ticode->hlU()->exp.v,
|
||||
locals.newLongReg ( _retVal->type, _retVal->longId(), picode.base()));
|
||||
if (res) /* was substituted */
|
||||
{
|
||||
picode->invalidate();
|
||||
_ic.invalidate();
|
||||
numHlIcodes--;
|
||||
}
|
||||
else /* cannot substitute function */
|
||||
{
|
||||
auto lhs = locals.createId(_retVal,picode.base());
|
||||
picode->setAsgn(lhs, _exp);
|
||||
_ic.setAsgn(lhs, _exp);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -1115,6 +1124,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
} /* eos */
|
||||
}
|
||||
}
|
||||
|
||||
/* HLI_PUSH doesn't define any registers, only uses registers.
|
||||
* Push the associated expression to the register on the local
|
||||
* expression stack */
|
||||
@ -1122,8 +1132,8 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
{
|
||||
g_exp_stk.processExpPush(numHlIcodes, *picode);
|
||||
}
|
||||
else if(picode->du1.getNumRegsDef()!=0)
|
||||
printf("Num def %d\n",picode->du1.getNumRegsDef());
|
||||
else if(_ic.du1.getNumRegsDef()!=0)
|
||||
printf("Num def %d\n",_ic.du1.getNumRegsDef());
|
||||
|
||||
/* For HLI_CALL instructions that use arguments from the stack,
|
||||
* pop them from the expression stack and place them on the
|
||||
@ -1137,11 +1147,11 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
|
||||
|
||||
/* If we could not substitute the result of a function,
|
||||
* assign it to the corresponding registers */
|
||||
if ( not _icHl.call.proc->isLibrary() and (not picode->du1.used(0)) and (picode->du1.getNumRegsDef() > 0))
|
||||
if ( not _icHl.call.proc->isLibrary() and (not _ic.du1.used(0)) and (_ic.du1.getNumRegsDef() > 0))
|
||||
{
|
||||
_exp = new FuncNode(_icHl.call.proc, _icHl.call.args);
|
||||
auto lhs = AstIdent::idID (&_icHl.call.proc->retVal, &locals, picode.base());
|
||||
picode->setAsgn(lhs, _exp);
|
||||
_ic.setAsgn(lhs, _exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1182,44 +1192,44 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
|
||||
isBx = _liveOut.testReg(rBX);
|
||||
isCx = _liveOut.testReg(rCX);
|
||||
isDx = _liveOut.testReg(rDX);
|
||||
bool isAL = !isAx && _liveOut.testReg(rAL);
|
||||
bool isAH = !isAx && _liveOut.testReg(rAH);
|
||||
bool isBL = !isBx && _liveOut.testReg(rBL);
|
||||
bool isBH = !isBx && _liveOut.testReg(rBH);
|
||||
bool isCL = !isCx && _liveOut.testReg(rCL);
|
||||
bool isCH = !isCx && _liveOut.testReg(rCH);
|
||||
bool isDL = !isDx && _liveOut.testReg(rDL);
|
||||
bool isDH = !isDx && _liveOut.testReg(rDH);
|
||||
if(isAL && isAH)
|
||||
bool isAL = not isAx and _liveOut.testReg(rAL);
|
||||
bool isAH = not isAx and _liveOut.testReg(rAH);
|
||||
bool isBL = not isBx and _liveOut.testReg(rBL);
|
||||
bool isBH = not isBx and _liveOut.testReg(rBH);
|
||||
bool isCL = not isCx and _liveOut.testReg(rCL);
|
||||
bool isCH = not isCx and _liveOut.testReg(rCH);
|
||||
bool isDL = not isDx and _liveOut.testReg(rDL);
|
||||
bool isDH = not isDx and _liveOut.testReg(rDH);
|
||||
if(isAL and isAH)
|
||||
{
|
||||
isAx = true;
|
||||
isAH=isAL=false;
|
||||
}
|
||||
if(isDL && isDH)
|
||||
if(isDL and isDH)
|
||||
{
|
||||
isDx = true;
|
||||
isDH=isDL=false;
|
||||
}
|
||||
if(isBL && isBH)
|
||||
if(isBL and isBH)
|
||||
{
|
||||
isBx = true;
|
||||
isBH=isBL=false;
|
||||
}
|
||||
if(isCL && isCH)
|
||||
if(isCL and isCH)
|
||||
{
|
||||
isCx = true;
|
||||
isCH=isCL=false;
|
||||
}
|
||||
|
||||
if (isAx && isDx) /* long or pointer */
|
||||
if (isAx and isDx) /* long or pointer */
|
||||
{
|
||||
retVal.type = TYPE_LONG_SIGN;
|
||||
retVal.loc = REG_FRAME;
|
||||
retVal.longId() = LONGID_TYPE(rDX,rAX);
|
||||
/*idx = */localId.newLongReg(TYPE_LONG_SIGN, LONGID_TYPE(rDX,rAX), Icode.begin());
|
||||
localId.propLongId (rAX, rDX, "\0");
|
||||
localId.propLongId (rAX, rDX, "");
|
||||
}
|
||||
else if (isAx || isBx || isCx || isDx) /* uint16_t */
|
||||
else if (isAx or isBx or isCx or isDx) /* uint16_t */
|
||||
{
|
||||
retVal.type = TYPE_WORD_SIGN;
|
||||
retVal.loc = REG_FRAME;
|
||||
@ -1233,7 +1243,7 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
|
||||
retVal.id.regi = rDX;
|
||||
/*idx = */localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi);
|
||||
}
|
||||
else if(isAL||isBL||isCL||isDL)
|
||||
else if(isAL or isBL or isCL or isDL)
|
||||
{
|
||||
retVal.type = TYPE_BYTE_SIGN;
|
||||
retVal.loc = REG_FRAME;
|
||||
@ -1247,7 +1257,7 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
|
||||
retVal.id.regi = rDL;
|
||||
/*idx = */localId.newByteWordReg(TYPE_BYTE_SIGN,retVal.id.regi);
|
||||
}
|
||||
else if(isAH||isBH||isCH||isDH)
|
||||
else if(isAH or isBH or isCH or isDH)
|
||||
{
|
||||
retVal.type = TYPE_BYTE_SIGN;
|
||||
retVal.loc = REG_FRAME;
|
||||
@ -1283,7 +1293,7 @@ void Function::dataFlow(LivenessSet &_liveOut)
|
||||
elimCondCodes();
|
||||
genLiveKtes();
|
||||
liveRegAnalysis (_liveOut); /* calls dataFlow() recursively */
|
||||
if (! (flg & PROC_ASM)) /* can generate C for pProc */
|
||||
if (not (flg & PROC_ASM)) /* can generate C for pProc */
|
||||
{
|
||||
genDU1 (); /* generate def/use level 1 chain */
|
||||
findExps (); /* forward substitution algorithm */
|
||||
|
||||
120
src/dcc.cpp
120
src/dcc.cpp
@ -4,32 +4,20 @@
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
|
||||
#include "dcc.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
#include "DccFrontend.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QCommandLineParser>
|
||||
#ifdef LLVM_EXPERIMENTAL
|
||||
#include <llvm/Support/raw_os_ostream.h>
|
||||
#include <llvm/Support/CommandLine.h>
|
||||
#include <llvm/Support/TargetSelect.h>
|
||||
#include <llvm/Support/TargetRegistry.h>
|
||||
#include <llvm/Support/PrettyStackTrace.h>
|
||||
#include <llvm/Support/Signals.h>
|
||||
#include <llvm/Support/Host.h>
|
||||
#include <llvm/Target/TargetMachine.h>
|
||||
#include <llvm/Target/TargetInstrInfo.h>
|
||||
#include <llvm/MC/MCAsmInfo.h>
|
||||
#include <llvm/CodeGen/MachineInstrBuilder.h>
|
||||
#include <llvm/TableGen/Main.h>
|
||||
#include <llvm/TableGen/TableGenBackend.h>
|
||||
#include <llvm/TableGen/Record.h>
|
||||
#endif
|
||||
|
||||
#include <QtCore/QFile>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
#include "DccFrontend.h"
|
||||
|
||||
/* Global variables - extern to other modules */
|
||||
extern QString asm1_name, asm2_name; /* Assembler output filenames */
|
||||
@ -37,85 +25,11 @@ extern SYMTAB symtab; /* Global symbol table */
|
||||
extern STATS stats; /* cfg statistics */
|
||||
extern OPTION option; /* Command line options */
|
||||
|
||||
static char *initargs(int argc, char *argv[]);
|
||||
static void displayTotalStats(void);
|
||||
static void displayTotalStats();
|
||||
/****************************************************************************
|
||||
* main
|
||||
***************************************************************************/
|
||||
#ifdef LLVM_EXPERIMENTAL
|
||||
using namespace llvm;
|
||||
bool TVisitor(raw_ostream &OS, RecordKeeper &Records)
|
||||
{
|
||||
Record *rec = Records.getDef("ADD8i8");
|
||||
if(rec)
|
||||
{
|
||||
if(not rec->getTemplateArgs().empty())
|
||||
std::cout << "Has template args\n";
|
||||
auto classes(rec->getSuperClasses());
|
||||
for(auto val : rec->getSuperClasses())
|
||||
std::cout << "Super "<<val->getName()<<"\n";
|
||||
|
||||
// DagInit * in = rec->getValueAsDag(val.getName());
|
||||
// in->dump();
|
||||
for(const RecordVal &val : rec->getValues())
|
||||
{
|
||||
// val.dump();
|
||||
}
|
||||
rec->dump();
|
||||
|
||||
}
|
||||
// rec = Records.getDef("CCR");
|
||||
// if(rec)
|
||||
// rec->dump();
|
||||
// for(auto val : Records.getDefs())
|
||||
// {
|
||||
// //std::cout<< "Def "<<val.first<<"\n";
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
int testTblGen(int argc, char **argv)
|
||||
{
|
||||
// using namespace llvm;
|
||||
// sys::PrintStackTraceOnErrorSignal();
|
||||
// PrettyStackTraceProgram(argc,argv);
|
||||
// cl::ParseCommandLineOptions(argc,argv);
|
||||
// return llvm::TableGenMain(argv[0],TVisitor);
|
||||
// InitializeNativeTarget();
|
||||
// Triple TheTriple;
|
||||
// std::string def = sys::getDefaultTargetTriple();
|
||||
// std::string MCPU="i386";
|
||||
// std::string MARCH="x86";
|
||||
// InitializeAllTargetInfos();
|
||||
// InitializeAllTargetMCs();
|
||||
// InitializeAllAsmPrinters();
|
||||
// InitializeAllAsmParsers();
|
||||
// InitializeAllDisassemblers();
|
||||
// std::string TargetTriple("i386-pc-linux-gnu");
|
||||
// TheTriple = Triple(Triple::normalize(TargetTriple));
|
||||
// MCOperand op=llvm::MCOperand::CreateImm(11);
|
||||
// MCAsmInfo info;
|
||||
// raw_os_ostream wrap(std::cerr);
|
||||
// op.print(wrap,&info);
|
||||
// wrap.flush();
|
||||
// std::cerr<<"\n";
|
||||
// std::string lookuperr;
|
||||
// TargetRegistry::printRegisteredTargetsForVersion();
|
||||
// const Target *t = TargetRegistry::lookupTarget(MARCH,TheTriple,lookuperr);
|
||||
// TargetOptions opts;
|
||||
// std::string Features;
|
||||
// opts.PrintMachineCode=1;
|
||||
// TargetMachine *tm = t->createTargetMachine(TheTriple.getTriple(),MCPU,Features,opts);
|
||||
// std::cerr<<tm->getInstrInfo()->getName(97)<<"\n";
|
||||
// const MCInstrDesc &ds(tm->getInstrInfo()->get(97));
|
||||
// const MCOperandInfo *op1=ds.OpInfo;
|
||||
// uint16_t impl_def = ds.getImplicitDefs()[0];
|
||||
// std::cerr<<lookuperr<<"\n";
|
||||
|
||||
// exit(0);
|
||||
|
||||
}
|
||||
#endif
|
||||
void setupOptions(QCoreApplication &app) {
|
||||
void setupOptions(const QCoreApplication &app) {
|
||||
//[-a1a2cmsi]
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("dcc");
|
||||
@ -133,14 +47,17 @@ void setupOptions(QCoreApplication &app) {
|
||||
parser.addOption(o);
|
||||
}
|
||||
QCommandLineOption assembly("a", QCoreApplication::translate("main", "Produce assembly"),"assembly_level");
|
||||
// A boolean option with multiple names (-f, --force)
|
||||
//QCommandLineOption forceOption(QStringList() << "f" << "force", "Overwrite existing files.");
|
||||
// An option with a value
|
||||
QCommandLineOption targetFileOption(QStringList() << "o" << "output",
|
||||
QCoreApplication::translate("main", "Place output into <file>."),
|
||||
QCoreApplication::translate("main", "file"));
|
||||
QCommandLineOption entryPointOption(QStringList() << "E",
|
||||
QCoreApplication::translate("main", "Custom entry point as hex"),
|
||||
QCoreApplication::translate("main", "offset"),
|
||||
"0"
|
||||
);
|
||||
parser.addOption(targetFileOption);
|
||||
parser.addOption(assembly);
|
||||
parser.addOption(entryPointOption);
|
||||
//parser.addOption(forceOption);
|
||||
// Process the actual command line arguments given by the user
|
||||
parser.addPositionalArgument("source", QCoreApplication::translate("main", "Dos Executable file to decompile."));
|
||||
@ -162,9 +79,10 @@ void setupOptions(QCoreApplication &app) {
|
||||
option.Interact = false;
|
||||
option.Calls = parser.isSet(boolOpts[2]);
|
||||
option.filename = args.first();
|
||||
option.CustomEntryPoint = parser.value(entryPointOption).toUInt(nullptr,16);
|
||||
if(parser.isSet(targetFileOption))
|
||||
asm1_name = asm2_name = parser.value(targetFileOption);
|
||||
else if(option.asm1 || option.asm2) {
|
||||
else if(option.asm1 or option.asm2) {
|
||||
asm1_name = option.filename+".a1";
|
||||
asm2_name = option.filename+".a2";
|
||||
}
|
||||
@ -184,7 +102,7 @@ int main(int argc, char **argv)
|
||||
Project::get()->create(option.filename);
|
||||
|
||||
DccFrontend fe(&app);
|
||||
if(!Project::get()->load()) {
|
||||
if(not Project::get()->load()) {
|
||||
return -1;
|
||||
}
|
||||
if (option.verbose)
|
||||
|
||||
@ -2,21 +2,23 @@
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
struct DccImpl : public IDcc {
|
||||
|
||||
|
||||
ilFunction m_current_func;
|
||||
// IDcc interface
|
||||
public:
|
||||
void BaseInit()
|
||||
{
|
||||
m_current_func = Project::get()->functions().end();
|
||||
}
|
||||
void Init(QObject *tgt)
|
||||
{
|
||||
}
|
||||
ilFunction GetFirstFuncHandle()
|
||||
{
|
||||
return Project::get()->functions().begin();
|
||||
}
|
||||
ilFunction GetCurFuncHandle()
|
||||
{
|
||||
return m_current_func;
|
||||
}
|
||||
void analysis_Once()
|
||||
{
|
||||
@ -34,13 +36,21 @@ public:
|
||||
}
|
||||
size_t getFuncCount()
|
||||
{
|
||||
return Project::get()->functions().size();
|
||||
}
|
||||
const lFunction &validFunctions() const
|
||||
{
|
||||
return Project::get()->functions();
|
||||
}
|
||||
void SetCurFunc_by_Name(QString)
|
||||
void SetCurFunc_by_Name(QString v)
|
||||
{
|
||||
lFunction & funcs(Project::get()->functions());
|
||||
for(auto iter=funcs.begin(),fin=funcs.end(); iter!=fin; ++iter) {
|
||||
if(iter->name==v) {
|
||||
m_current_func = iter;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
QDir installDir() {
|
||||
return QDir(".");
|
||||
@ -54,8 +64,7 @@ public:
|
||||
|
||||
IDcc* IDcc::get() {
|
||||
static IDcc *v=0;
|
||||
if(!v)
|
||||
if(nullptr == v)
|
||||
v = new DccImpl;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
167
src/disassem.cpp
167
src/disassem.cpp
@ -2,6 +2,15 @@
|
||||
* dcc project disassembler
|
||||
* (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
|
||||
****************************************************************************/
|
||||
#include "disassem.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "symtab.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QDebug>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@ -10,10 +19,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "symtab.h"
|
||||
#include "disassem.h"
|
||||
#include "project.h"
|
||||
// Note: for the time being, there is no interactive disassembler
|
||||
// for unix
|
||||
|
||||
@ -70,8 +75,8 @@ static const char *szFlops3C[] =
|
||||
|
||||
static const char *szPtr[2] = { "word ptr ", "byte ptr " };
|
||||
|
||||
static void formatRM(ostringstream &p, const LLOperand &pm);
|
||||
static ostringstream &strDst(ostringstream &os, uint32_t flg, const LLOperand &pm);
|
||||
static void formatRM(QTextStream & p, const LLOperand &pm);
|
||||
static QTextStream & strDst(QTextStream & os, uint32_t flg, const LLOperand &pm);
|
||||
|
||||
static char *strHex(uint32_t d);
|
||||
//static int checkScanned(uint32_t pcCur);
|
||||
@ -107,7 +112,7 @@ static vector<POSSTACK_ENTRY> posStack; /* position stack */
|
||||
|
||||
void LLInst::findJumpTargets(CIcodeRec &_pc)
|
||||
{
|
||||
if (testFlags(I) && ! testFlags(JMP_ICODE) && isJmpInst())
|
||||
if (testFlags(I) and not testFlags(JMP_ICODE) and isJmpInst())
|
||||
{
|
||||
/* Replace the immediate operand with an icode index */
|
||||
iICODE labTgt=_pc.labelSrch(src().getImm2());
|
||||
@ -150,11 +155,11 @@ void Disassembler::disassem(Function * ppProc)
|
||||
if (pass != 3)
|
||||
{
|
||||
auto p = (pass == 1)? asm1_name: asm2_name;
|
||||
m_fp.open(p.toStdString(),ios_base::app);
|
||||
if (!m_fp.is_open())
|
||||
{
|
||||
m_disassembly_target = new QFile(p);
|
||||
if(!m_disassembly_target->open(QFile::WriteOnly|QFile::Text|QFile::Append)) {
|
||||
fatalError(CANNOT_OPEN, p.toStdString().c_str());
|
||||
}
|
||||
m_fp.setDevice(m_disassembly_target);
|
||||
}
|
||||
/* Create temporary code array */
|
||||
// Mike: needs objectising!
|
||||
@ -177,7 +182,7 @@ void Disassembler::disassem(Function * ppProc)
|
||||
/* Write procedure header */
|
||||
if (pass != 3)
|
||||
{
|
||||
std::string near_far=(pProc->flg & PROC_FAR)? "FAR": "NEAR";
|
||||
const char * near_far=(pProc->flg & PROC_FAR)? "FAR": "NEAR";
|
||||
m_fp << "\t\t"<<pProc->name<<" PROC "<< near_far<<"\n";
|
||||
}
|
||||
|
||||
@ -192,7 +197,10 @@ void Disassembler::disassem(Function * ppProc)
|
||||
if (pass != 3)
|
||||
{
|
||||
m_fp << "\n\t\t"<<pProc->name<<" ENDP\n\n";
|
||||
m_fp.close();
|
||||
m_fp.setDevice(nullptr);
|
||||
m_disassembly_target->close();
|
||||
delete m_disassembly_target;
|
||||
|
||||
}
|
||||
|
||||
pc.clear();
|
||||
@ -206,19 +214,23 @@ void Disassembler::disassem(Function * ppProc)
|
||||
void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
ostringstream oper_stream;
|
||||
ostringstream hex_bytes;
|
||||
ostringstream result_stream;
|
||||
ostringstream opcode_with_mods;
|
||||
ostringstream operands_s;
|
||||
oper_stream << uppercase;
|
||||
hex_bytes << uppercase;
|
||||
QString oper_contents;
|
||||
QTextStream oper_stream(&oper_contents);
|
||||
QString hex_bytes;
|
||||
QString result_contents;
|
||||
QTextStream result_stream(&result_contents);
|
||||
QString opcode_with_mods;
|
||||
|
||||
QString operands_contents;
|
||||
QTextStream operands_s(&operands_contents);
|
||||
oper_stream.setNumberFlags(QTextStream::UppercaseBase|QTextStream::UppercaseDigits);
|
||||
|
||||
/* Disassembly stage 1 --
|
||||
* Do not try to display NO_CODE entries or synthetic instructions,
|
||||
* other than JMPs, that have been introduced for def/use analysis. */
|
||||
if ((option.asm1) &&
|
||||
( inst.testFlags(NO_CODE) ||
|
||||
(inst.testFlags(SYNTHETIC) && (inst.getOpcode() != iJMP))))
|
||||
if ((option.asm1) and
|
||||
( inst.testFlags(NO_CODE) or
|
||||
(inst.testFlags(SYNTHETIC) and (inst.getOpcode() != iJMP))))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -242,25 +254,28 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
cb = (uint32_t) inst.numBytes;
|
||||
nextInst = inst.label + cb;
|
||||
|
||||
/* Output hexa code in program image */
|
||||
/* Output hex code in program image */
|
||||
if (pass != 3)
|
||||
{
|
||||
for (j = 0; j < cb; j++)
|
||||
{
|
||||
hex_bytes << hex << setw(2) << setfill('0') << uint16_t(prog.image()[inst.label + j]);
|
||||
hex_bytes += QString("%1").arg(uint16_t(prog.image()[inst.label + j]),2,16,QChar('0')).toUpper();
|
||||
}
|
||||
hex_bytes << ' ';
|
||||
hex_bytes += ' ';
|
||||
}
|
||||
}
|
||||
oper_stream << setw(POS_LAB) << left<< hex_bytes.str();
|
||||
oper_stream.setFieldWidth(POS_LAB);
|
||||
oper_stream.setFieldAlignment(QTextStream::AlignLeft);
|
||||
oper_stream << hex_bytes;
|
||||
/* Check if there is a symbol here */
|
||||
selectTable(Label);
|
||||
oper_stream << setw(5)<<left; // align for the labels
|
||||
oper_stream.setFieldWidth(5); // align for the labels
|
||||
{
|
||||
ostringstream lab_contents;
|
||||
if (readVal(lab_contents, inst.label, nullptr))
|
||||
QString lab_contents;
|
||||
QTextStream lab_stream(&lab_contents);
|
||||
if (readVal(lab_stream, inst.label, nullptr))
|
||||
{
|
||||
lab_contents << ':'; /* Also removes the null */
|
||||
lab_stream << ':'; /* Also removes the null */
|
||||
}
|
||||
else if (inst.testFlags(TARGET)) /* Symbols override Lnn labels */
|
||||
{
|
||||
@ -269,15 +284,17 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
{
|
||||
pl[loc_ip] = ++g_lab;
|
||||
}
|
||||
lab_contents<< "L"<<pl[loc_ip]<<':';
|
||||
lab_stream<< "L"<<pl[loc_ip]<<':';
|
||||
}
|
||||
oper_stream<< lab_contents.str();
|
||||
lab_stream.flush();
|
||||
oper_stream << lab_contents;
|
||||
oper_stream.setFieldWidth(0);
|
||||
}
|
||||
if ((inst.getOpcode()==iSIGNEX )&& inst.testFlags(B))
|
||||
if ((inst.getOpcode()==iSIGNEX )and inst.testFlags(B))
|
||||
{
|
||||
inst.setOpcode(iCBW);
|
||||
}
|
||||
opcode_with_mods<<Machine_X86::opcodeName(inst.getOpcode());
|
||||
opcode_with_mods += Machine_X86::opcodeName(inst.getOpcode());
|
||||
|
||||
switch ( inst.getOpcode() )
|
||||
{
|
||||
@ -342,7 +359,7 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
{
|
||||
ICODE *lab=pc.GetIcode(inst.src().getImm2());
|
||||
selectTable(Label);
|
||||
if ((inst.src().getImm2() < (uint32_t)numIcode) && /* Ensure in range */
|
||||
if ((inst.src().getImm2() < (uint32_t)numIcode) and /* Ensure in range */
|
||||
readVal(operands_s, lab->ll()->label, nullptr))
|
||||
{
|
||||
break; /* Symbolic label. Done */
|
||||
@ -381,11 +398,10 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
case iCALL: case iCALLF:
|
||||
if (inst.testFlags(I))
|
||||
{
|
||||
if((inst.getOpcode() == iCALL))
|
||||
operands_s<< "near";
|
||||
else
|
||||
operands_s<< " far";
|
||||
operands_s<<" ptr "<<(inst.src().proc.proc)->name;
|
||||
QString oper = QString("%1 ptr %2")
|
||||
.arg((inst.getOpcode() == iCALL) ? "near" : "far")
|
||||
.arg((inst.src().proc.proc)->name);
|
||||
operands_s<< qPrintable(oper);
|
||||
}
|
||||
else if (inst.getOpcode() == iCALLF)
|
||||
{
|
||||
@ -416,14 +432,14 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
case iOUTS: case iREP_OUTS:
|
||||
if (inst.src().segOver)
|
||||
{
|
||||
bool is_dx_src=(inst.getOpcode() == iOUTS || inst.getOpcode() == iREP_OUTS);
|
||||
bool is_dx_src=(inst.getOpcode() == iOUTS or inst.getOpcode() == iREP_OUTS);
|
||||
if(is_dx_src)
|
||||
operands_s<<"dx, "<<szPtr[inst.getFlag() & B];
|
||||
else
|
||||
operands_s<<szPtr[inst.getFlag() & B];
|
||||
if (inst.getOpcode() == iLODS ||
|
||||
inst.getOpcode() == iREP_LODS ||
|
||||
inst.getOpcode() == iOUTS ||
|
||||
if (inst.getOpcode() == iLODS or
|
||||
inst.getOpcode() == iREP_LODS or
|
||||
inst.getOpcode() == iOUTS or
|
||||
inst.getOpcode() == iREP_OUTS)
|
||||
{
|
||||
operands_s<<Machine_X86::regName(inst.src().segOver); // szWreg[src.segOver-rAX]
|
||||
@ -436,7 +452,10 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
}
|
||||
else
|
||||
{
|
||||
(inst.getFlag() & B)? opcode_with_mods<< "B": opcode_with_mods<< "W";
|
||||
if(inst.getFlag() & B)
|
||||
opcode_with_mods+='B';
|
||||
else
|
||||
opcode_with_mods+='W';
|
||||
}
|
||||
break;
|
||||
|
||||
@ -455,8 +474,8 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
|
||||
case iOUT:
|
||||
{
|
||||
std::string d1=((inst.testFlags(I))? strHex(inst.src().getImm2()): "dx");
|
||||
std::string d2=((inst.getFlag() & B) ? ", al": ", ax");
|
||||
QString d1=((inst.testFlags(I))? strHex(inst.src().getImm2()): "dx");
|
||||
QString d2=((inst.getFlag() & B) ? ", al": ", ax");
|
||||
operands_s<<d1 << d2;
|
||||
}
|
||||
break;
|
||||
@ -464,8 +483,9 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
oper_stream << setw(15) << left <<opcode_with_mods.str();
|
||||
oper_stream << operands_s.str();
|
||||
oper_stream.setFieldWidth(15);
|
||||
operands_s.flush();
|
||||
oper_stream << qSetFieldWidth(15) << opcode_with_mods << qSetFieldWidth(0) << operands_contents;
|
||||
/* Comments */
|
||||
if (inst.testFlags(SYNTHETIC))
|
||||
{
|
||||
@ -473,21 +493,26 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
}
|
||||
else
|
||||
{
|
||||
for (j = inst.label, fImpure = 0; j > 0 && j < (int)nextInst; j++)
|
||||
for (j = inst.label, fImpure = 0; j > 0 and j < (int)nextInst; j++)
|
||||
{
|
||||
fImpure |= BITMAP(j, BM_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
result_stream << setw(54) << left << oper_stream.str();
|
||||
result_stream.setFieldWidth(54);
|
||||
result_stream.setFieldAlignment(QTextStream::AlignLeft);
|
||||
oper_stream.flush();
|
||||
result_stream << oper_contents;
|
||||
result_stream.setFieldWidth(0);
|
||||
/* Check for user supplied comment */
|
||||
selectTable(Comment);
|
||||
ostringstream cbuf;
|
||||
QString cbuf_contents;
|
||||
QTextStream cbuf(&cbuf_contents);
|
||||
if (readVal(cbuf, inst.label, nullptr))
|
||||
{
|
||||
result_stream <<"; "<<cbuf.str();
|
||||
cbuf.flush();
|
||||
result_stream <<"; "<<*cbuf.string();
|
||||
}
|
||||
else if (fImpure || (inst.testFlags(SWITCH | CASE | SEG_IMMED | IMPURE | SYNTHETIC | TERMINATES)))
|
||||
else if (fImpure or (inst.testFlags(SWITCH | CASE | SEG_IMMED | IMPURE | SYNTHETIC | TERMINATES)))
|
||||
{
|
||||
if (inst.testFlags(CASE))
|
||||
{
|
||||
@ -525,8 +550,9 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
/* output to .b code buffer */
|
||||
if (inst.testFlags(SYNTHETIC))
|
||||
result_stream<<";Synthetic inst";
|
||||
if (pass == 3) /* output to .b code buffer */
|
||||
cCode.appendCode("%s\n", result_stream.str().c_str());
|
||||
if (pass == 3) { /* output to .b code buffer */
|
||||
cCode.appendCode("%s\n", qPrintable(result_contents));
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
@ -542,7 +568,8 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
sprintf(buf,"%03d ",loc_ip);
|
||||
result_stream<<";Synthetic inst";
|
||||
}
|
||||
m_fp<<buf<< " " << result_stream.str() << "\n";
|
||||
result_stream.flush();
|
||||
m_fp<<buf<< " " << result_contents << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
@ -551,7 +578,7 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
|
||||
/****************************************************************************
|
||||
* formatRM
|
||||
***************************************************************************/
|
||||
static void formatRM(std::ostringstream &p, const LLOperand &pm)
|
||||
static void formatRM(QTextStream &p, const LLOperand &pm)
|
||||
{
|
||||
//char seg[4];
|
||||
|
||||
@ -588,7 +615,7 @@ static void formatRM(std::ostringstream &p, const LLOperand &pm)
|
||||
/*****************************************************************************
|
||||
* strDst
|
||||
****************************************************************************/
|
||||
static ostringstream & strDst(ostringstream &os,uint32_t flg, const LLOperand &pm)
|
||||
static QTextStream & strDst(QTextStream &os,uint32_t flg, const LLOperand &pm)
|
||||
{
|
||||
/* Immediates to memory require size descriptor */
|
||||
//os << setw(WID_PTR);
|
||||
@ -602,7 +629,7 @@ static ostringstream & strDst(ostringstream &os,uint32_t flg, const LLOperand &p
|
||||
/****************************************************************************
|
||||
* strSrc *
|
||||
****************************************************************************/
|
||||
ostringstream &LLInst::strSrc(ostringstream &os,bool skip_comma)
|
||||
QTextStream &LLInst::strSrc(QTextStream &os,bool skip_comma)
|
||||
{
|
||||
if(false==skip_comma)
|
||||
os<<", ";
|
||||
@ -633,14 +660,18 @@ static char *strHex(uint32_t d)
|
||||
/****************************************************************************
|
||||
* interactDis - interactive disassembler *
|
||||
****************************************************************************/
|
||||
void interactDis(Function * /*initProc*/, int /*initIC*/)
|
||||
void interactDis(Function * initProc, int initIC)
|
||||
{
|
||||
printf("Sorry - interactive disasassembler option not available for Unix\n");
|
||||
QString procname = "UNKNOWN";
|
||||
if(initProc)
|
||||
procname = initProc->name;
|
||||
|
||||
qDebug() << "Wanted to start interactive disasassembler for "<<procname<<":"<<initIC;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Handle the floating point opcodes (icode iESC) */
|
||||
void LLInst::flops(std::ostringstream &out)
|
||||
void LLInst::flops(QTextStream &out)
|
||||
{
|
||||
//char bf[30];
|
||||
uint8_t op = (uint8_t)src().getImm2();
|
||||
@ -652,8 +683,8 @@ void LLInst::flops(std::ostringstream &out)
|
||||
{
|
||||
/* The mod/rm mod bits are not set to 11 (i.e. register). This is the normal floating point opcode */
|
||||
out<<Machine_X86::floatOpName(op)<<' ';
|
||||
out <<setw(10);
|
||||
if ((op == 0x29) || (op == 0x1F))
|
||||
out.setFieldWidth(10);
|
||||
if ((op == 0x29) or (op == 0x1F))
|
||||
{
|
||||
out << "tbyte ptr ";
|
||||
}
|
||||
@ -683,7 +714,7 @@ void LLInst::flops(std::ostringstream &out)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
out.setFieldWidth(0);
|
||||
formatRM(out, m_dst);
|
||||
}
|
||||
else
|
||||
@ -722,7 +753,7 @@ void LLInst::flops(std::ostringstream &out)
|
||||
break;
|
||||
default:
|
||||
out << Machine_X86::floatOpName(0x40+op);
|
||||
if ((op >= 0x20) && (op <= 0x27))
|
||||
if ((op >= 0x20) and (op <= 0x27))
|
||||
{
|
||||
/* This is the ST(i), ST form. */
|
||||
out << "ST("<<destRegIdx - rAX<<"),ST";
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
#include "dcc.h"
|
||||
|
||||
static std::map<eErrorId,std::string> errorMessage =
|
||||
static const std::map<eErrorId,std::string> errorMessage =
|
||||
{
|
||||
{INVALID_ARG ,"Invalid option -%c\n"},
|
||||
{INVALID_OPCODE ,"Invalid instruction %02X at location %06lX\n"},
|
||||
@ -28,7 +28,7 @@
|
||||
{IP_OUT_OF_RANGE ,"Instruction at location %06lX goes beyond loaded image\n"},
|
||||
{DEF_NOT_FOUND ,"Definition not found for condition code usage at opcode %d\n"},
|
||||
{JX_NOT_DEF ,"JX use, definition not supported at opcode #%d\n"},
|
||||
{NOT_DEF_USE ,"Def - use not supported. Def op = %d, use op = %d.\n"},
|
||||
{NOT_DEF_USE ,"%x: Def - use not supported. Def op = %d, use op = %d.\n"},
|
||||
{REPEAT_FAIL ,"Failed to construct repeat..until() condition.\n"},
|
||||
{WHILE_FAIL ,"Failed to construct while() condition.\n"},
|
||||
};
|
||||
@ -37,7 +37,8 @@
|
||||
fatalError: displays error message and exits the program.
|
||||
****************************************************************************/
|
||||
void fatalError(eErrorId errId, ...)
|
||||
{ va_list args;
|
||||
{
|
||||
va_list args;
|
||||
//#ifdef __UNIX__ /* ultrix */
|
||||
#if 0
|
||||
int errId;
|
||||
@ -51,8 +52,10 @@ void fatalError(eErrorId errId, ...)
|
||||
if (errId == USAGE)
|
||||
fprintf(stderr,"Usage: dcc [-a1a2cmpsvVi][-o asmfile] DOS_executable\n");
|
||||
else {
|
||||
auto msg_iter = errorMessage.find(errId);
|
||||
assert(msg_iter!=errorMessage.end());
|
||||
fprintf(stderr, "dcc: ");
|
||||
vfprintf(stderr, errorMessage[errId].c_str(), args);
|
||||
vfprintf(stderr, msg_iter->second.c_str(), args);
|
||||
}
|
||||
va_end(args);
|
||||
exit((int)errId);
|
||||
@ -63,7 +66,8 @@ void fatalError(eErrorId errId, ...)
|
||||
reportError: reports the warning/error and continues with the program.
|
||||
****************************************************************************/
|
||||
void reportError(eErrorId errId, ...)
|
||||
{ va_list args;
|
||||
{
|
||||
va_list args;
|
||||
//#ifdef __UNIX__ /* ultrix */
|
||||
#if 0
|
||||
int errId;
|
||||
@ -74,6 +78,8 @@ void reportError(eErrorId errId, ...)
|
||||
va_start(args, errId);
|
||||
#endif
|
||||
fprintf(stderr, "dcc: ");
|
||||
vfprintf(stderr, errorMessage[errId].c_str(), args);
|
||||
auto msg_iter = errorMessage.find(errId);
|
||||
assert(msg_iter!=errorMessage.end());
|
||||
vfprintf(stderr, msg_iter->second.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@ -9,18 +9,15 @@
|
||||
* *
|
||||
\* * * * * * * * * * * * */
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include <memory.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef PATLEN
|
||||
#define PATLEN 23
|
||||
#define WILD 0xF4
|
||||
#endif
|
||||
|
||||
#ifndef bool
|
||||
#define bool unsigned char
|
||||
#define uint8_t unsigned char
|
||||
#endif
|
||||
|
||||
static int pc; /* Indexes into pat[] */
|
||||
|
||||
/* prototypes */
|
||||
@ -410,7 +407,7 @@ void fixWildCards(uint8_t pat[])
|
||||
|
||||
case 0xCD: /* int nn */
|
||||
intArg = pat[pc++];
|
||||
if ((intArg >= 0x34) && (intArg <= 0x3B))
|
||||
if ((intArg >= 0x34) and (intArg <= 0x3B))
|
||||
{
|
||||
/* Borland/Microsoft FP emulations */
|
||||
if (ModRM(pat)) return;
|
||||
|
||||
@ -3,14 +3,15 @@
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "graph.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <boost/range/rbegin.hpp>
|
||||
#include <boost/range/rend.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "graph.h"
|
||||
#include "project.h"
|
||||
#include <string.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
@ -19,6 +20,14 @@ extern Project g_proj;
|
||||
//static void mergeFallThrough(Function * pProc, BB * pBB);
|
||||
//static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, int *first, int *last);
|
||||
|
||||
void Function::addOutEdgesForConditionalJump(BB * pBB,int next_ip, LLInst *ll)
|
||||
{
|
||||
pBB->addOutEdge(next_ip);
|
||||
/* This is checking for jumps off into nowhere */
|
||||
if ( not ll->testFlags(NO_LABEL) )
|
||||
pBB->addOutEdge(ll->src().getImm2());
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* createCFG - Create the basic control flow graph
|
||||
****************************************************************************/
|
||||
@ -68,16 +77,13 @@ void Function::createCFG()
|
||||
case iJO: case iJNO: case iJP: case iJNP:
|
||||
case iJCXZ:
|
||||
pBB = BB::Create(current_range, TWO_BRANCH, this);
|
||||
CondJumps:
|
||||
pBB->addOutEdge(nextIcode->loc_ip);
|
||||
/* This is checking for jumps off into nowhere */
|
||||
if ( not ll->testFlags(NO_LABEL) )
|
||||
pBB->addOutEdge(ll->src().getImm2());
|
||||
addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
|
||||
break;
|
||||
|
||||
case iLOOP: case iLOOPE: case iLOOPNE:
|
||||
pBB = BB::Create(current_range, LOOP_NODE, this);
|
||||
goto CondJumps;
|
||||
addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
|
||||
break;
|
||||
|
||||
case iJMPF: case iJMP:
|
||||
if (ll->testFlags(SWITCH))
|
||||
@ -100,7 +106,7 @@ CondJumps:
|
||||
{
|
||||
Function * p = ll->src().proc.proc;
|
||||
pBB = BB::Create(current_range, CALL_NODE, this);
|
||||
if (p && not ((p->flg) & TERMINATES) )
|
||||
if (p and not ((p->flg) & TERMINATES) )
|
||||
pBB->addOutEdge(nextIcode->loc_ip);
|
||||
break;
|
||||
}
|
||||
@ -132,6 +138,8 @@ CondJumps:
|
||||
// end iterator will be updated by expression in for statement
|
||||
current_range=make_iterator_range(nextIcode,nextIcode);
|
||||
}
|
||||
if (nextIcode == Icode.end())
|
||||
break;
|
||||
}
|
||||
for (auto pr : m_ip_to_bb)
|
||||
{
|
||||
@ -146,7 +154,7 @@ CondJumps:
|
||||
}
|
||||
auto iter2=m_ip_to_bb.find(ip);
|
||||
if(iter2==m_ip_to_bb.end())
|
||||
fatalError(NO_BB, ip, name.c_str());
|
||||
fatalError(NO_BB, ip, qPrintable(name));
|
||||
psBB = iter2->second;
|
||||
elem.BBptr = psBB;
|
||||
psBB->inEdges.push_back((BB *)nullptr);
|
||||
@ -203,7 +211,7 @@ void Function::compressCFG()
|
||||
* (Un)Conditional -> Unconditional jump */
|
||||
for (BB *pBB : m_actual_cfg) //m_cfg
|
||||
{
|
||||
if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH))
|
||||
if(pBB->inEdges.empty() or (pBB->nodeType != ONE_BRANCH and pBB->nodeType != TWO_BRANCH))
|
||||
continue;
|
||||
for (TYPEADR_TYPE &edgeRef : pBB->edges)
|
||||
{
|
||||
@ -265,7 +273,7 @@ BB *BB::rmJMP(int marker, BB * pBB)
|
||||
{
|
||||
marker += (int)DFS_JMP;
|
||||
|
||||
while (pBB->nodeType == ONE_BRANCH && pBB->size() == 1)
|
||||
while (pBB->nodeType == ONE_BRANCH and pBB->size() == 1)
|
||||
{
|
||||
if (pBB->traversed != marker)
|
||||
{
|
||||
@ -292,7 +300,7 @@ BB *BB::rmJMP(int marker, BB * pBB)
|
||||
do {
|
||||
pBB = pBB->edges[0].BBptr;
|
||||
pBB->inEdges.pop_back(); // was --numInedges
|
||||
if (! pBB->inEdges.empty())
|
||||
if (not pBB->inEdges.empty())
|
||||
{
|
||||
pBB->front().ll()->setFlags(NO_CODE);
|
||||
pBB->front().invalidate();
|
||||
@ -314,11 +322,11 @@ BB *BB::rmJMP(int marker, BB * pBB)
|
||||
void BB::mergeFallThrough( CIcodeRec &Icode)
|
||||
{
|
||||
BB * pChild;
|
||||
if (!this)
|
||||
if (nullptr==this)
|
||||
{
|
||||
printf("mergeFallThrough on empty BB!\n");
|
||||
}
|
||||
while (nodeType == FALL_NODE || nodeType == ONE_BRANCH)
|
||||
while (nodeType == FALL_NODE or nodeType == ONE_BRANCH)
|
||||
{
|
||||
pChild = edges[0].BBptr;
|
||||
/* Jump to next instruction can always be removed */
|
||||
|
||||
137
src/hlicode.cpp
137
src/hlicode.cpp
@ -4,17 +4,17 @@
|
||||
* Date: September-October 1993
|
||||
* (C) Cristina Cifuentes
|
||||
*/
|
||||
#include "dcc.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtCore/QString>
|
||||
|
||||
#include <cassert>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "dcc.h"
|
||||
using namespace std;
|
||||
|
||||
static char buf[lineSize]; /* Line buffer for hl icode output */
|
||||
|
||||
|
||||
|
||||
/* Places the new HLI_ASSIGN high-level operand in the high-level icode array */
|
||||
void HLTYPE::setAsgn(Expr *lhs, Expr *rhs)
|
||||
{
|
||||
@ -24,12 +24,12 @@ void HLTYPE::setAsgn(Expr *lhs, Expr *rhs)
|
||||
}
|
||||
void ICODE::checkHlCall()
|
||||
{
|
||||
//assert((ll()->immed.proc.cb != 0)||ll()->immed.proc.proc!=0);
|
||||
//assert((ll()->immed.proc.cb != 0) or ll()->immed.proc.proc!=0);
|
||||
}
|
||||
/* Places the new HLI_CALL high-level operand in the high-level icode array */
|
||||
void ICODE::newCallHl()
|
||||
{
|
||||
type = HIGH_LEVEL;
|
||||
type = HIGH_LEVEL_ICODE;
|
||||
hlU()->setCall(ll()->src().proc.proc);
|
||||
|
||||
if (ll()->src().proc.cb != 0)
|
||||
@ -48,7 +48,7 @@ void ICODE::newCallHl()
|
||||
* array */
|
||||
void ICODE::setUnary(hlIcode op, Expr *_exp)
|
||||
{
|
||||
type = HIGH_LEVEL;
|
||||
type = HIGH_LEVEL_ICODE;
|
||||
hlU()->set(op,_exp);
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ void ICODE::setUnary(hlIcode op, Expr *_exp)
|
||||
/* Places the new HLI_JCOND high-level operand in the high-level icode array */
|
||||
void ICODE::setJCond(Expr *cexp)
|
||||
{
|
||||
type = HIGH_LEVEL;
|
||||
type = HIGH_LEVEL_ICODE;
|
||||
hlU()->set(HLI_JCOND,cexp);
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ bool ICODE::removeDefRegi (eReg regi, int thisDefIdx, LOCAL_ID *locId)
|
||||
for ( ; numDefs > 0; numDefs--)
|
||||
{
|
||||
|
||||
if (du1.used(numDefs-1)||(du.lastDefRegi.testReg(regi)))
|
||||
if (du1.used(numDefs-1) or (du.lastDefRegi.testReg(regi)))
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -320,7 +320,7 @@ void Function::highLevelGen()
|
||||
LLOperand *src_ll = ll->get(SRC);
|
||||
if ( ll->testFlags(NOT_HLL) )
|
||||
pIcode->invalidate();
|
||||
if ((pIcode->type != LOW_LEVEL) or not pIcode->valid() )
|
||||
if ((pIcode->type != LOW_LEVEL_ICODE) or not pIcode->valid() )
|
||||
continue;
|
||||
_flg = ll->getFlag();
|
||||
if (not ll->testFlags(IM_OPS)) /* not processing IM_OPS yet */
|
||||
@ -328,7 +328,7 @@ void Function::highLevelGen()
|
||||
{
|
||||
if ( not ll->testFlags(NO_SRC) ) /* if there is src op */
|
||||
rhs = AstIdent::id (*pIcode->ll(), SRC, this, i, *pIcode, NONE);
|
||||
if(ll->m_dst.isSet() || (ll->getOpcode()==iMOD))
|
||||
if(ll->m_dst.isSet() or (ll->getOpcode()==iMOD))
|
||||
lhs = AstIdent::id (*pIcode->ll(), DST, this, i, *pIcode, NONE);
|
||||
}
|
||||
if(ll->getOpcode()==iPUSH) {
|
||||
@ -353,7 +353,7 @@ void Function::highLevelGen()
|
||||
|
||||
case iCALL:
|
||||
case iCALLF:
|
||||
pIcode->type = HIGH_LEVEL;
|
||||
pIcode->type = HIGH_LEVEL_ICODE;
|
||||
pIcode->hl( ll->createCall() );
|
||||
break;
|
||||
|
||||
@ -477,82 +477,68 @@ void Function::highLevelGen()
|
||||
|
||||
/* Returns the string that represents the procedure call of tproc (ie. with
|
||||
* actual parameters) */
|
||||
std::string Function::writeCall (Function * tproc, STKFRAME & args, int *numLoc)
|
||||
QString Function::writeCall (Function * tproc, STKFRAME & args, int *numLoc)
|
||||
{
|
||||
//string condExp;
|
||||
ostringstream ostr;
|
||||
ostr<<tproc->name<<" (";
|
||||
QString ostr;
|
||||
ostr+=tproc->name+" (";
|
||||
for(const STKSYM &sym : args)
|
||||
{
|
||||
if(sym.actual)
|
||||
ostr << sym.actual->walkCondExpr (this, numLoc);
|
||||
ostr += sym.actual->walkCondExpr(this, numLoc);
|
||||
else
|
||||
ostr << "";
|
||||
ostr += "";
|
||||
if((&sym)!=&(args.back()))
|
||||
ostr << ", ";
|
||||
ostr += ", ";
|
||||
}
|
||||
ostr << ")";
|
||||
return ostr.str();
|
||||
ostr += ")";
|
||||
return ostr;
|
||||
}
|
||||
|
||||
|
||||
/* Displays the output of a HLI_JCOND icode. */
|
||||
const char *writeJcond (const HLTYPE &h, Function * pProc, int *numLoc)
|
||||
QString writeJcond (const HLTYPE &h, Function * pProc, int *numLoc)
|
||||
{
|
||||
memset (buf, ' ', sizeof(buf));
|
||||
buf[0] = '\0';
|
||||
strcat (buf, "if ");
|
||||
if(h.opcode==HLI_INVALID)
|
||||
{
|
||||
return "if (*HLI_INVALID*) {\n";
|
||||
}
|
||||
|
||||
assert(h.expr());
|
||||
Expr *inverted=h.expr()->inverse();
|
||||
//inverseCondOp (&h.exp);
|
||||
std::string e = inverted->walkCondExpr (pProc, numLoc);
|
||||
QString inverted_form = inverted->walkCondExpr (pProc, numLoc);
|
||||
delete inverted;
|
||||
strcat (buf, e.c_str());
|
||||
strcat (buf, " {\n");
|
||||
return (buf);
|
||||
|
||||
return QString("if %1 {\n").arg(inverted_form);
|
||||
}
|
||||
|
||||
|
||||
/* Displays the inverse output of a HLI_JCOND icode. This is used in the case
|
||||
* when the THEN clause of an if..then..else is empty. The clause is
|
||||
* negated and the ELSE clause is used instead. */
|
||||
const char *writeJcondInv(HLTYPE h, Function * pProc, int *numLoc)
|
||||
QString writeJcondInv(HLTYPE h, Function * pProc, int *numLoc)
|
||||
{
|
||||
memset (buf, ' ', sizeof(buf));
|
||||
buf[0] = '\0';
|
||||
strcat (buf, "if ");
|
||||
std::string e;
|
||||
QString _form;
|
||||
|
||||
if(h.expr()==nullptr)
|
||||
e = "( *failed condition recovery* )";
|
||||
_form = "( *failed condition recovery* )";
|
||||
else
|
||||
e = h.expr()->walkCondExpr (pProc, numLoc);
|
||||
|
||||
strcat (buf, e.c_str());
|
||||
strcat (buf, " {\n");
|
||||
return (buf);
|
||||
_form = h.expr()->walkCondExpr (pProc, numLoc);
|
||||
return QString("if %1 {\n").arg(_form);
|
||||
}
|
||||
|
||||
string AssignType::writeOut(Function *pProc, int *numLoc) const
|
||||
QString AssignType::writeOut(Function *pProc, int *numLoc) const
|
||||
{
|
||||
ostringstream ostr;
|
||||
ostr << m_lhs->walkCondExpr (pProc, numLoc);
|
||||
ostr << " = ";
|
||||
ostr << rhs->walkCondExpr (pProc, numLoc);
|
||||
ostr << ";\n";
|
||||
return ostr.str();
|
||||
return QString("%1 = %2;\n")
|
||||
.arg(m_lhs->walkCondExpr (pProc, numLoc))
|
||||
.arg(m_rhs->walkCondExpr (pProc, numLoc));
|
||||
}
|
||||
string CallType::writeOut(Function *pProc, int *numLoc) const
|
||||
QString CallType::writeOut(Function *pProc, int *numLoc) const
|
||||
{
|
||||
ostringstream ostr;
|
||||
ostr << pProc->writeCall (proc, *args, numLoc);
|
||||
ostr << ";\n";
|
||||
return ostr.str();
|
||||
return pProc->writeCall (proc, *args, numLoc) + ";\n";
|
||||
}
|
||||
string ExpType::writeOut(Function *pProc, int *numLoc) const
|
||||
QString ExpType::writeOut(Function *pProc, int *numLoc) const
|
||||
{
|
||||
if(v==nullptr)
|
||||
return "";
|
||||
@ -567,16 +553,14 @@ void HLTYPE::set(Expr *l, Expr *r)
|
||||
//assert((asgn.lhs==0) and (asgn.rhs==0)); //prevent memory leaks
|
||||
assert(dynamic_cast<UnaryOperator *>(l));
|
||||
asgn.m_lhs=l;
|
||||
asgn.rhs=r;
|
||||
asgn.m_rhs=r;
|
||||
}
|
||||
/* Returns a string with the contents of the current high-level icode.
|
||||
* Note: this routine does not output the contens of HLI_JCOND icodes. This is
|
||||
* done in a separate routine to be able to support the removal of
|
||||
* empty THEN clauses on an if..then..else. */
|
||||
string HLTYPE::write1HlIcode (Function * pProc, int *numLoc) const
|
||||
QString HLTYPE::write1HlIcode (Function * pProc, int *numLoc) const
|
||||
{
|
||||
string e;
|
||||
ostringstream ostr;
|
||||
const HlTypeSupport *p = get();
|
||||
switch (opcode)
|
||||
{
|
||||
@ -585,26 +569,23 @@ string HLTYPE::write1HlIcode (Function * pProc, int *numLoc) const
|
||||
case HLI_CALL:
|
||||
return p->writeOut(pProc,numLoc);
|
||||
case HLI_RET:
|
||||
{
|
||||
QString e;
|
||||
e = p->writeOut(pProc,numLoc);
|
||||
if (! e.empty())
|
||||
ostr << "return (" << e << ");\n";
|
||||
if (not e.isEmpty())
|
||||
return QString("return (%1);\n").arg(e);
|
||||
break;
|
||||
}
|
||||
case HLI_POP:
|
||||
ostr << "HLI_POP ";
|
||||
ostr << p->writeOut(pProc,numLoc);
|
||||
ostr << "\n";
|
||||
break;
|
||||
return QString("HLI_POP %1\n").arg(p->writeOut(pProc,numLoc));
|
||||
case HLI_PUSH:
|
||||
ostr << "HLI_PUSH ";
|
||||
ostr << p->writeOut(pProc,numLoc);
|
||||
ostr << "\n";
|
||||
break;
|
||||
return QString("HLI_PUSH %1\n").arg(p->writeOut(pProc,numLoc));
|
||||
case HLI_JCOND: //Handled elsewhere
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr," HLTYPE::write1HlIcode - Unhandled opcode %d\n",opcode);
|
||||
qCritical() << " HLTYPE::write1HlIcode - Unhandled opcode" << opcode;
|
||||
}
|
||||
return ostr.str();
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@ -619,16 +600,22 @@ void ICODE::writeDU()
|
||||
{
|
||||
int my_idx = loc_ip;
|
||||
{
|
||||
ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
{
|
||||
QTextStream ostr(&ostr_contents);
|
||||
Machine_X86::writeRegVector(ostr,du.def);
|
||||
if (!ostr.str().empty())
|
||||
printf ("Def (reg) = %s\n", ostr.str().c_str());
|
||||
}
|
||||
if (not ostr_contents.isEmpty())
|
||||
qDebug() << QString("Def (reg) = %1\n").arg(ostr_contents);
|
||||
}
|
||||
{
|
||||
ostringstream ostr;
|
||||
QString ostr_contents;
|
||||
{
|
||||
QTextStream ostr(&ostr_contents);
|
||||
Machine_X86::writeRegVector(ostr,du.use);
|
||||
if (!ostr.str().empty())
|
||||
printf ("Use (reg) = %s\n", ostr.str().c_str());
|
||||
}
|
||||
if (not ostr_contents.isEmpty())
|
||||
qDebug() << QString("Use (reg) = %1\n").arg(ostr_contents);
|
||||
}
|
||||
|
||||
/* Print du1 chain */
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
// Object oriented icode code for dcc
|
||||
// (C) 1997 Mike Van Emmerik
|
||||
#include "icode.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "dcc.h"
|
||||
#include "types.h" // Common types like uint8_t, etc
|
||||
#include "ast.h" // Some icode types depend on these
|
||||
#include "icode.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
ICODE::TypeFilter<HIGH_LEVEL> ICODE::select_high_level;
|
||||
ICODE::TypeAndValidFilter<HIGH_LEVEL> ICODE::select_valid_high_level;
|
||||
ICODE::TypeFilter<HIGH_LEVEL_ICODE> ICODE::select_high_level;
|
||||
ICODE::TypeAndValidFilter<HIGH_LEVEL_ICODE> ICODE::select_valid_high_level;
|
||||
CIcodeRec::CIcodeRec()
|
||||
{
|
||||
}
|
||||
@ -87,7 +87,7 @@ void LLInst::emitGotoLabel (int indLevel)
|
||||
|
||||
bool LLOperand::isReg() const
|
||||
{
|
||||
return (regi>=rAX) && (regi<=rTMP);
|
||||
return (regi>=rAX) and (regi<=rTMP);
|
||||
}
|
||||
void LLOperand::addProcInformation(int param_count, CConv::Type call_conv)
|
||||
{
|
||||
|
||||
@ -4,14 +4,11 @@
|
||||
****************************************************************************/
|
||||
|
||||
//#include <llvm/Config/llvm-config.h>
|
||||
//#if( (LLVM_VERSION_MAJOR==3 ) && (LLVM_VERSION_MINOR>3) )
|
||||
//#if( (LLVM_VERSION_MAJOR==3 ) and (LLVM_VERSION_MINOR>3) )
|
||||
//#include <llvm/IR/PatternMatch.h>
|
||||
//#else
|
||||
//#include <llvm/Support/PatternMatch.h>
|
||||
//#endif
|
||||
#include <boost/iterator/filter_iterator.hpp>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include "idiom.h"
|
||||
#include "idiom1.h"
|
||||
#include "epilogue_idioms.h"
|
||||
@ -22,6 +19,11 @@
|
||||
#include "shift_idioms.h"
|
||||
#include "arith_idioms.h"
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <boost/iterator/filter_iterator.hpp>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
/*****************************************************************************
|
||||
* JmpInst - Returns true if opcode is a conditional or unconditional jump
|
||||
****************************************************************************/
|
||||
@ -122,11 +124,11 @@ void Function::findIdioms()
|
||||
/* Check for library functions that return a long register.
|
||||
* Propagate this result */
|
||||
if (pIcode->ll()->src().proc.proc != nullptr)
|
||||
if ((pIcode->ll()->src().proc.proc->flg & PROC_ISLIB) &&
|
||||
if ((pIcode->ll()->src().proc.proc->flg & PROC_ISLIB) and
|
||||
(pIcode->ll()->src().proc.proc->flg & PROC_IS_FUNC))
|
||||
{
|
||||
if ((pIcode->ll()->src().proc.proc->retVal.type==TYPE_LONG_SIGN)
|
||||
|| (pIcode->ll()->src().proc.proc->retVal.type == TYPE_LONG_UNSIGN))
|
||||
or (pIcode->ll()->src().proc.proc->retVal.type == TYPE_LONG_UNSIGN))
|
||||
localId.newLongReg(TYPE_LONG_SIGN, LONGID_TYPE(rDX,rAX), pIcode/*ip*/);
|
||||
}
|
||||
|
||||
@ -209,14 +211,14 @@ void Function::findIdioms()
|
||||
}
|
||||
|
||||
/* Check if number of parameter bytes match their calling convention */
|
||||
if ((flg & PROC_HLL) && (!args.empty()))
|
||||
if ((flg & PROC_HLL) and (not args.empty()))
|
||||
{
|
||||
args.m_minOff += (flg & PROC_FAR ? 4 : 2);
|
||||
args.m_minOff += ((flg & PROC_FAR)!=0 ? 4 : 2);
|
||||
delta = args.maxOff - args.m_minOff;
|
||||
if (cbParam != delta)
|
||||
{
|
||||
cbParam = delta;
|
||||
callingConv(CConv::UNKNOWN);
|
||||
callingConv(CConv::eUnknown);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -236,7 +238,7 @@ void Function::bindIcodeOff()
|
||||
for(ICODE &c : Icode) // TODO: use filtered here
|
||||
{
|
||||
LLInst *ll=c.ll();
|
||||
if (ll->testFlags(I) && ll->isJmpInst())
|
||||
if (ll->testFlags(I) and ll->isJmpInst())
|
||||
{
|
||||
iICODE loc=Icode.labelSrch(ll->src().getImm2());
|
||||
if (loc!=Icode.end())
|
||||
@ -256,7 +258,7 @@ void Function::bindIcodeOff()
|
||||
if (ll->testFlags(I) )
|
||||
{
|
||||
uint32_t found;
|
||||
if (! Icode.labelSrch(ll->src().getImm2(), found))
|
||||
if (not Icode.labelSrch(ll->src().getImm2(), found))
|
||||
ll->setFlags( NO_LABEL );
|
||||
else
|
||||
ll->replaceSrc(LLOperand::CreateImm2(found));
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
#include "dcc.h"
|
||||
#include "arith_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
@ -105,7 +110,7 @@ bool Idiom18::match(iICODE picode)
|
||||
if(not m_icodes[0]->ll()->matchWithRegDst(iMOV) )
|
||||
return false;
|
||||
regi = m_icodes[0]->ll()->m_dst.regi;
|
||||
if( not ( m_icodes[2]->ll()->match(iCMP,regi) &&
|
||||
if( not ( m_icodes[2]->ll()->match(iCMP,regi) and
|
||||
m_icodes[3]->ll()->conditionalJump() ) )
|
||||
return false;
|
||||
// Simple matching finished, select apropriate matcher based on dst type
|
||||
@ -118,9 +123,9 @@ bool Idiom18::match(iICODE picode)
|
||||
else if ( m_icodes[1]->ll()->m_dst.isReg() ) /* register */
|
||||
{
|
||||
m_idiom_type = 1;
|
||||
// if ((m_icodes[1]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR))
|
||||
// if ((m_icodes[1]->ll()->dst.regi == rSI) and (m_func->flg & SI_REGVAR))
|
||||
// m_idiom_type = 1;
|
||||
// else if ((m_icodes[1]->ll()->dst.regi == rDI) && (m_func->flg & DI_REGVAR))
|
||||
// else if ((m_icodes[1]->ll()->dst.regi == rDI) and (m_func->flg & DI_REGVAR))
|
||||
// m_idiom_type = 1;
|
||||
}
|
||||
else if (m_icodes[1]->ll()->m_dst.off) /* local variable */
|
||||
@ -131,7 +136,8 @@ bool Idiom18::match(iICODE picode)
|
||||
/* not supported yet */
|
||||
ICODE &ic(*picode);
|
||||
const Function *my_proc(ic.getParent()->getParent());
|
||||
printf("Unsupported idiom18 type at %x in %s:%x : indexed\n",ic.loc_ip,my_proc->name.c_str(),my_proc->procEntry);
|
||||
qWarning() << "Unsupported idiom18 type at"<< QString::number(ic.loc_ip,16)
|
||||
<< "in"<< my_proc->name <<':'<< QString::number(my_proc->procEntry,16) << "- indexed";
|
||||
}
|
||||
|
||||
switch(m_idiom_type)
|
||||
@ -141,13 +147,13 @@ bool Idiom18::match(iICODE picode)
|
||||
break;
|
||||
case 1: /* register variable */
|
||||
/* Check previous instruction for a MOV */
|
||||
if ( (m_icodes[0]->ll()->src().regi == m_icodes[1]->ll()->m_dst.regi))
|
||||
if ( m_icodes[0]->ll()->src().regi == m_icodes[1]->ll()->m_dst.regi)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 2: /* local */
|
||||
if ((m_icodes[0]->ll()->src().off == m_icodes[1]->ll()->m_dst.off))
|
||||
if (m_icodes[0]->ll()->src().off == m_icodes[1]->ll()->m_dst.off)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -204,8 +210,8 @@ bool Idiom19::match(iICODE picode)
|
||||
/* not supported yet */ ;
|
||||
else if ( m_icodes[0]->ll()->m_dst.isReg() ) /* register */
|
||||
{
|
||||
// if (((picode->ll()->dst.regi == rSI) && (pproc->flg & SI_REGVAR)) ||
|
||||
// ((picode->ll()->dst.regi == rDI) && (pproc->flg & DI_REGVAR)))
|
||||
// if (((picode->ll()->dst.regi == rSI) and (pproc->flg & SI_REGVAR)) or
|
||||
// ((picode->ll()->dst.regi == rDI) and (pproc->flg & DI_REGVAR)))
|
||||
return true;
|
||||
}
|
||||
else if (m_icodes[0]->ll()->m_dst.off) /* stack variable */
|
||||
@ -271,9 +277,9 @@ bool Idiom20::match(iICODE picode)
|
||||
else if ( ll_dest.isReg() ) /* register */
|
||||
{
|
||||
type = 1;
|
||||
// if ((ll_dest.regi == rSI) && (m_func->flg & SI_REGVAR))
|
||||
// if ((ll_dest.regi == rSI) and (m_func->flg & SI_REGVAR))
|
||||
// type = 1;
|
||||
// else if ((ll_dest.regi == rDI) && (m_func->flg & DI_REGVAR))
|
||||
// else if ((ll_dest.regi == rDI) and (m_func->flg & DI_REGVAR))
|
||||
// type = 1;
|
||||
}
|
||||
else if (ll_dest.off) /* local variable */
|
||||
@ -286,7 +292,7 @@ bool Idiom20::match(iICODE picode)
|
||||
}
|
||||
regi = m_icodes[1]->ll()->m_dst.regi;
|
||||
const LLOperand &mov_src(m_icodes[1]->ll()->src());
|
||||
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) && m_icodes[3]->ll()->conditionalJump())
|
||||
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) and m_icodes[3]->ll()->conditionalJump())
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "call_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
using namespace std;
|
||||
/*****************************************************************************
|
||||
* idiom3 - C calling convention.
|
||||
@ -22,7 +25,7 @@ bool Idiom3::match(iICODE picode)
|
||||
/* Match ADD SP, immed */
|
||||
for(int i=0; i<2; ++i)
|
||||
m_icodes[i] = picode++;
|
||||
if ( m_icodes[1]->ll()->testFlags(I) && m_icodes[1]->ll()->match(iADD,rSP))
|
||||
if ( m_icodes[1]->ll()->testFlags(I) and m_icodes[1]->ll()->match(iADD,rSP))
|
||||
{
|
||||
m_param_count = m_icodes[1]->ll()->src().getImm2();
|
||||
return true;
|
||||
@ -38,7 +41,7 @@ int Idiom3::action()
|
||||
{
|
||||
if (m_icodes[0]->ll()->testFlags(I) )
|
||||
{
|
||||
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::C);
|
||||
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::eCdecl);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -78,10 +81,10 @@ bool Idiom17::match(iICODE picode)
|
||||
{
|
||||
int i=0;
|
||||
regi = m_icodes[1]->ll()->m_dst.regi;
|
||||
if ((regi >= rAX) && (regi <= rBX))
|
||||
if ((regi >= rAX) and (regi <= rBX))
|
||||
i++;
|
||||
|
||||
while (picode != m_end && picode->ll()->match(iPOP))
|
||||
while (picode != m_end and picode->ll()->match(iPOP))
|
||||
{
|
||||
if (picode->ll()->m_dst.regi != regi)
|
||||
break;
|
||||
@ -96,7 +99,7 @@ int Idiom17::action()
|
||||
{
|
||||
if (m_icodes[0]->ll()->testFlags(I))
|
||||
{
|
||||
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::C);
|
||||
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::eCdecl);
|
||||
for(size_t idx=1; idx<m_icodes.size(); ++idx)
|
||||
{
|
||||
m_icodes[idx]->invalidate();
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "epilogue_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* popStkVars - checks for
|
||||
* [POP DI]
|
||||
@ -14,9 +16,9 @@ void EpilogIdiom::popStkVars(iICODE pIcode)
|
||||
/* Match [POP DI] */
|
||||
if (pIcode->ll()->match(iPOP))
|
||||
{
|
||||
if ((m_func->flg & DI_REGVAR) && pIcode->ll()->match(rDI))
|
||||
if ((m_func->flg & DI_REGVAR) and pIcode->ll()->match(rDI))
|
||||
m_icodes.push_front(pIcode);
|
||||
else if ((m_func->flg & SI_REGVAR) && pIcode->ll()->match(rSI))
|
||||
else if ((m_func->flg & SI_REGVAR) and pIcode->ll()->match(rSI))
|
||||
m_icodes.push_front(pIcode);
|
||||
}
|
||||
++pIcode;
|
||||
@ -25,9 +27,9 @@ void EpilogIdiom::popStkVars(iICODE pIcode)
|
||||
/* Match [POP SI] */
|
||||
if (pIcode->ll()->match(iPOP))
|
||||
{
|
||||
if ((m_func->flg & SI_REGVAR) && pIcode->ll()->match(rSI))
|
||||
if ((m_func->flg & SI_REGVAR) and pIcode->ll()->match(rSI))
|
||||
m_icodes.push_front(pIcode);
|
||||
else if ((m_func->flg & DI_REGVAR) && pIcode->ll()->match(rDI))
|
||||
else if ((m_func->flg & DI_REGVAR) and pIcode->ll()->match(rDI))
|
||||
m_icodes.push_front(pIcode);
|
||||
}
|
||||
}
|
||||
@ -46,7 +48,7 @@ bool Idiom2::match(iICODE pIcode)
|
||||
iICODE nicode;
|
||||
if(pIcode==m_func->Icode.begin()) // pIcode->loc_ip == 0
|
||||
return false;
|
||||
if ( pIcode->ll()->testFlags(I) || (not pIcode->ll()->match(rSP,rBP)) )
|
||||
if ( pIcode->ll()->testFlags(I) or (not pIcode->ll()->match(rSP,rBP)) )
|
||||
return false;
|
||||
if(distance(pIcode,m_end)<3)
|
||||
return false;
|
||||
@ -55,21 +57,21 @@ bool Idiom2::match(iICODE pIcode)
|
||||
m_icodes.push_back(pIcode);
|
||||
/* Get next icode, skip over holes in the icode array */
|
||||
nicode = ++iICODE(pIcode);
|
||||
while (nicode->ll()->testFlags(NO_CODE) && (nicode != m_end))
|
||||
while (nicode->ll()->testFlags(NO_CODE) and (nicode != m_end))
|
||||
{
|
||||
nicode++;
|
||||
}
|
||||
if(nicode == m_end)
|
||||
return false;
|
||||
|
||||
if (nicode->ll()->match(iPOP,rBP) && ! (nicode->ll()->testFlags(I | TARGET | CASE)) )
|
||||
if (nicode->ll()->match(iPOP,rBP) and not (nicode->ll()->testFlags(I | TARGET | CASE)) )
|
||||
{
|
||||
m_icodes.push_back(nicode++); // Matched POP BP
|
||||
|
||||
/* Match RET(F) */
|
||||
if ( nicode != m_end &&
|
||||
!(nicode->ll()->testFlags(I | TARGET | CASE)) &&
|
||||
(nicode->ll()->match(iRET) || nicode->ll()->match(iRETF))
|
||||
if ( nicode != m_end and
|
||||
not (nicode->ll()->testFlags(I | TARGET | CASE)) and
|
||||
(nicode->ll()->match(iRET) or nicode->ll()->match(iRETF))
|
||||
)
|
||||
{
|
||||
m_icodes.push_back(nicode); // Matched RET
|
||||
@ -118,7 +120,7 @@ bool Idiom4::match(iICODE pIcode)
|
||||
{
|
||||
iICODE prev1 = --iICODE(pIcode);
|
||||
/* Check for POP BP */
|
||||
if (prev1->ll()->match(iPOP,rBP) && not prev1->ll()->testFlags(I) )
|
||||
if (prev1->ll()->match(iPOP,rBP) and not prev1->ll()->testFlags(I) )
|
||||
m_icodes.push_back(prev1);
|
||||
else if(prev1!=m_func->Icode.begin())
|
||||
{
|
||||
@ -138,7 +140,7 @@ bool Idiom4::match(iICODE pIcode)
|
||||
}
|
||||
int Idiom4::action()
|
||||
{
|
||||
if( ! m_icodes.empty()) // if not an empty RET[F] N
|
||||
if( not m_icodes.empty()) // if not an empty RET[F] N
|
||||
{
|
||||
for(size_t idx=0; idx<m_icodes.size()-1; ++idx) // don't invalidate last entry
|
||||
m_icodes[idx]->invalidate();
|
||||
@ -146,7 +148,7 @@ int Idiom4::action()
|
||||
if(m_param_count)
|
||||
{
|
||||
m_func->cbParam = (int16_t)m_param_count;
|
||||
m_func->callingConv(CConv::PASCAL);
|
||||
m_func->callingConv(CConv::ePascal);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "idiom1.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* checkStkVars - Checks for PUSH SI
|
||||
@ -19,14 +22,14 @@ int Idiom1::checkStkVars (iICODE pIcode)
|
||||
{
|
||||
si_matched = 1;
|
||||
++pIcode;
|
||||
if ((pIcode != m_end) && pIcode->ll()->match(iPUSH,rDI)) // Look for PUSH DI
|
||||
if ((pIcode != m_end) and pIcode->ll()->match(iPUSH,rDI)) // Look for PUSH DI
|
||||
di_matched = 1;
|
||||
}
|
||||
else if (pIcode->ll()->match(iPUSH,rDI))
|
||||
{
|
||||
di_matched = 1;
|
||||
++pIcode;
|
||||
if ((pIcode != m_end) && pIcode->ll()->match(iPUSH,rSI)) // Look for PUSH SI
|
||||
if ((pIcode != m_end) and pIcode->ll()->match(iPUSH,rSI)) // Look for PUSH SI
|
||||
si_matched = 1;
|
||||
}
|
||||
m_func->flg |= (si_matched ? SI_REGVAR : 0) | (di_matched ? DI_REGVAR : 0);
|
||||
@ -60,13 +63,13 @@ bool Idiom1::match(iICODE picode)
|
||||
m_icodes.clear();
|
||||
m_min_off = 0;
|
||||
/* PUSH BP as first instruction of procedure */
|
||||
if ( (not picode->ll()->testFlags(I)) && picode->ll()->src().regi == rBP)
|
||||
if ( (not picode->ll()->testFlags(I)) and picode->ll()->src().regi == rBP)
|
||||
{
|
||||
m_icodes.push_back( picode++ ); // insert iPUSH
|
||||
if(picode==m_end)
|
||||
return false;
|
||||
/* MOV BP, SP as next instruction */
|
||||
if ( !picode->ll()->testFlags(I | TARGET | CASE) && picode->ll()->match(iMOV ,rBP,rSP) )
|
||||
if ( not picode->ll()->testFlags(I | TARGET | CASE) and picode->ll()->match(iMOV ,rBP,rSP) )
|
||||
{
|
||||
m_icodes.push_back( picode++ ); // insert iMOV
|
||||
if(picode==m_end)
|
||||
@ -75,7 +78,7 @@ bool Idiom1::match(iICODE picode)
|
||||
|
||||
/* Look for SUB SP, immed */
|
||||
if (
|
||||
picode->ll()->testFlags(I | TARGET | CASE) && picode->ll()->match(iSUB,rSP)
|
||||
picode->ll()->testFlags(I | TARGET | CASE) and picode->ll()->match(iSUB,rSP)
|
||||
)
|
||||
{
|
||||
m_icodes.push_back( picode++ ); // insert iSUB
|
||||
@ -98,8 +101,8 @@ bool Idiom1::match(iICODE picode)
|
||||
if(picode == m_end)
|
||||
return false;
|
||||
/* Look for MOV BP, SP */
|
||||
if ( picode != m_end &&
|
||||
!picode->ll()->testFlags(I | TARGET | CASE) &&
|
||||
if ( picode != m_end and
|
||||
not picode->ll()->testFlags(I | TARGET | CASE) and
|
||||
picode->ll()->match(iMOV,rBP,rSP))
|
||||
{
|
||||
m_icodes.push_back(picode);
|
||||
@ -119,7 +122,7 @@ bool Idiom1::match(iICODE picode)
|
||||
m_icodes.push_back(picode++);
|
||||
|
||||
}
|
||||
return !m_icodes.empty();
|
||||
return not m_icodes.empty();
|
||||
}
|
||||
int Idiom1::action()
|
||||
{
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "mov_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
@ -30,17 +33,17 @@ bool Idiom14::match(iICODE pIcode)
|
||||
LLInst * matched [] {m_icodes[0]->ll(),m_icodes[1]->ll()};
|
||||
/* Check for regL */
|
||||
m_regL = matched[0]->m_dst.regi;
|
||||
if (not matched[0]->testFlags(I) && ((m_regL == rAX) || (m_regL ==rBX)))
|
||||
if (not matched[0]->testFlags(I) and ((m_regL == rAX) or (m_regL ==rBX)))
|
||||
{
|
||||
/* Check for XOR regH, regH */
|
||||
if (matched[1]->match(iXOR) && not matched[1]->testFlags(I))
|
||||
if (matched[1]->match(iXOR) and not matched[1]->testFlags(I))
|
||||
{
|
||||
m_regH = matched[1]->m_dst.regi;
|
||||
if (m_regH == matched[1]->src().getReg2())
|
||||
{
|
||||
if ((m_regL == rAX) && (m_regH == rDX))
|
||||
if ((m_regL == rAX) and (m_regH == rDX))
|
||||
return true;
|
||||
if ((m_regL == rBX) && (m_regH == rCX))
|
||||
if ((m_regL == rBX) and (m_regH == rCX))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -81,10 +84,10 @@ bool Idiom13::match(iICODE pIcode)
|
||||
|
||||
/* Check for regL */
|
||||
regi = m_icodes[0]->ll()->m_dst.regi;
|
||||
if (not m_icodes[0]->ll()->testFlags(I) && (regi >= rAL) && (regi <= rBH))
|
||||
if (not m_icodes[0]->ll()->testFlags(I) and (regi >= rAL) and (regi <= rBH))
|
||||
{
|
||||
/* Check for MOV regH, 0 */
|
||||
if (m_icodes[1]->ll()->match(iMOV,I) && (m_icodes[1]->ll()->src().getImm2() == 0))
|
||||
if (m_icodes[1]->ll()->match(iMOV,I) and (m_icodes[1]->ll()->src().getImm2() == 0))
|
||||
{
|
||||
if (m_icodes[1]->ll()->m_dst.regi == (regi + 4)) //WARNING: based on distance between AH-AL,BH-BL etc.
|
||||
{
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "neg_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@ -23,7 +26,7 @@ bool Idiom11::match (iICODE picode)
|
||||
for(int i=0; i<3; ++i)
|
||||
m_icodes[i]=picode++;
|
||||
type = m_icodes[0]->ll()->idType(DST);
|
||||
if(type==CONSTANT || type == OTHER)
|
||||
if(type==CONSTANT or type == OTHER)
|
||||
return false;
|
||||
/* Check NEG reg/mem
|
||||
* SBB reg/mem, 0*/
|
||||
@ -32,7 +35,7 @@ bool Idiom11::match (iICODE picode)
|
||||
switch (type)
|
||||
{
|
||||
case GLOB_VAR:
|
||||
if ((m_icodes[2]->ll()->m_dst.segValue == m_icodes[0]->ll()->m_dst.segValue) &&
|
||||
if ((m_icodes[2]->ll()->m_dst.segValue == m_icodes[0]->ll()->m_dst.segValue) and
|
||||
(m_icodes[2]->ll()->m_dst.off == m_icodes[0]->ll()->m_dst.off))
|
||||
return true;
|
||||
break;
|
||||
@ -83,11 +86,11 @@ bool Idiom16::match (iICODE picode)
|
||||
m_icodes[i]=picode++;
|
||||
|
||||
uint8_t regi = m_icodes[0]->ll()->m_dst.regi;
|
||||
if ((regi >= rAX) && (regi < INDEX_BX_SI))
|
||||
if ((regi >= rAX) and (regi < INDEX_BX_SI))
|
||||
{
|
||||
if (m_icodes[1]->ll()->match(iSBB) && m_icodes[2]->ll()->match(iINC))
|
||||
if ((m_icodes[1]->ll()->m_dst.regi == (m_icodes[1]->ll()->src().getReg2())) &&
|
||||
m_icodes[1]->ll()->match((eReg)regi) &&
|
||||
if (m_icodes[1]->ll()->match(iSBB) and m_icodes[2]->ll()->match(iINC))
|
||||
if ((m_icodes[1]->ll()->m_dst.regi == (m_icodes[1]->ll()->src().getReg2())) and
|
||||
m_icodes[1]->ll()->match((eReg)regi) and
|
||||
m_icodes[2]->ll()->match((eReg)regi))
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "shift_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@ -18,8 +21,8 @@ bool Idiom8::match(iICODE pIcode)
|
||||
return false;
|
||||
m_icodes[0]=pIcode++;
|
||||
m_icodes[1]=pIcode++;
|
||||
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if ( m_icodes[1]->ll()->match(iRCR,I) &&
|
||||
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if ( m_icodes[1]->ll()->match(iRCR,I) and
|
||||
(m_icodes[1]->ll()->src().getImm2() == 1))
|
||||
return true;
|
||||
return false;
|
||||
@ -107,8 +110,8 @@ bool Idiom12::match(iICODE pIcode)
|
||||
return false;
|
||||
m_icodes[0]=pIcode++;
|
||||
m_icodes[1]=pIcode++;
|
||||
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[1]->ll()->match(iRCL,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[1]->ll()->match(iRCL,I) and (m_icodes[1]->ll()->src().getImm2() == 1))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@ -147,8 +150,8 @@ bool Idiom9::match(iICODE pIcode)
|
||||
return false;
|
||||
m_icodes[0]=pIcode++;
|
||||
m_icodes[1]=pIcode++;
|
||||
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[1]->ll()->match(iRCR,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
|
||||
if (m_icodes[1]->ll()->match(iRCR,I) and (m_icodes[1]->ll()->src().getImm2() == 1))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
#include "dcc.h"
|
||||
#include "xor_idioms.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
@ -28,11 +31,11 @@ bool Idiom21::match (iICODE picode)
|
||||
|
||||
dst = &m_icodes[0]->ll()->m_dst;
|
||||
src = &m_icodes[0]->ll()->src();
|
||||
if ((dst->regi == src->getReg2()) && (dst->getReg2() > 0) && (dst->getReg2() < INDEX_BX_SI))
|
||||
if ((dst->regi == src->getReg2()) and (dst->getReg2() > 0) and (dst->getReg2() < INDEX_BX_SI))
|
||||
{
|
||||
if ((dst->getReg2() == rDX) && m_icodes[1]->ll()->match(rAX))
|
||||
if ((dst->getReg2() == rDX) and m_icodes[1]->ll()->match(rAX))
|
||||
return true;
|
||||
if ((dst->getReg2() == rCX) && m_icodes[1]->ll()->match(rBX))
|
||||
if ((dst->getReg2() == rCX) and m_icodes[1]->ll()->match(rBX))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -67,7 +70,7 @@ bool Idiom7::match(iICODE picode)
|
||||
src = &picode->ll()->src();
|
||||
if (dst->regi == 0) /* global variable */
|
||||
{
|
||||
if ((dst->segValue == src->segValue) && (dst->off == src->off))
|
||||
if ((dst->segValue == src->segValue) and (dst->off == src->off))
|
||||
return true;
|
||||
}
|
||||
else if (dst->regi < INDEX_BX_SI) /* register */
|
||||
@ -75,9 +78,9 @@ bool Idiom7::match(iICODE picode)
|
||||
if (dst->regi == src->regi)
|
||||
return true;
|
||||
}
|
||||
else if ((dst->off) && (dst->seg == rSS) && (dst->regi == INDEX_BP)) /* offset from BP */
|
||||
else if ((dst->off) and (dst->seg == rSS) and (dst->regi == INDEX_BP)) /* offset from BP */
|
||||
{
|
||||
if ((dst->off == src->off) && (dst->seg == src->seg) && (dst->regi == src->regi))
|
||||
if ((dst->off == src->off) and (dst->seg == src->seg) and (dst->regi == src->regi))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -114,8 +117,8 @@ bool Idiom10::match(iICODE pIcode)
|
||||
m_icodes[0]=pIcode++;
|
||||
m_icodes[1]=pIcode++;
|
||||
/* Check OR reg, reg */
|
||||
if (not m_icodes[0]->ll()->testFlags(I) &&
|
||||
m_icodes[0]->ll()->src().isReg() &&
|
||||
if (not m_icodes[0]->ll()->testFlags(I) and
|
||||
m_icodes[0]->ll()->src().isReg() and
|
||||
(m_icodes[0]->ll()->src().getReg2() == m_icodes[0]->ll()->m_dst.getReg2()))
|
||||
if (m_icodes[1]->ll()->match(iJNE)) //.conditionalJump()
|
||||
{
|
||||
|
||||
162
src/locident.cpp
162
src/locident.cpp
@ -4,10 +4,18 @@
|
||||
* Date: October 1993
|
||||
* (C) Cristina Cifuentes
|
||||
*/
|
||||
#include "locident.h"
|
||||
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <cstring>
|
||||
#include "locident.h"
|
||||
#include "dcc.h"
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
static const int LOCAL_ID_DELTA = 25;
|
||||
static const int IDX_ARRAY_DELTA = 5;
|
||||
|
||||
|
||||
bool LONGID_TYPE::srcDstRegMatch(iICODE a, iICODE b) const
|
||||
{
|
||||
return (a->ll()->src().getReg2()==m_l) and (b->ll()->m_dst.getReg2()==m_h);
|
||||
@ -23,7 +31,7 @@ ID::ID(hlType t, frameType f) : type(t),illegal(false),hasMacro(false)
|
||||
macro[0]=0;
|
||||
memset(&id,0,sizeof(id));
|
||||
loc=f;
|
||||
assert(not ((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN)));
|
||||
assert(not ((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN)));
|
||||
}
|
||||
ID::ID(hlType t,const LONGID_TYPE &s) : type(t),illegal(false),hasMacro(false)
|
||||
{
|
||||
@ -31,7 +39,7 @@ ID::ID(hlType t,const LONGID_TYPE &s) : type(t),illegal(false),hasMacro(false)
|
||||
memset(&id,0,sizeof(id));
|
||||
loc=REG_FRAME;
|
||||
m_longId = s;
|
||||
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
|
||||
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
|
||||
}
|
||||
ID::ID(hlType t,const LONG_STKID_TYPE &s) : type(t),illegal(false),hasMacro(false)
|
||||
{
|
||||
@ -39,7 +47,7 @@ ID::ID(hlType t,const LONG_STKID_TYPE &s) : type(t),illegal(false),hasMacro(fals
|
||||
memset(&id,0,sizeof(id));
|
||||
loc=STK_FRAME;
|
||||
id.longStkId = s;
|
||||
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
|
||||
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
|
||||
}
|
||||
|
||||
ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
|
||||
@ -48,12 +56,17 @@ ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
|
||||
memset(&id,0,sizeof(id));
|
||||
loc=GLB_FRAME;
|
||||
id.longGlb = s;
|
||||
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
|
||||
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
|
||||
}
|
||||
|
||||
eReg ID::getPairedRegister(eReg first) const {
|
||||
if (longId().h() == first)
|
||||
return (longId().l());
|
||||
else if (longId().l() == first)
|
||||
return (longId().h());
|
||||
return rUNDEF;
|
||||
}
|
||||
|
||||
#define LOCAL_ID_DELTA 25
|
||||
#define IDX_ARRAY_DELTA 5
|
||||
|
||||
/* Creates a new identifier node of type t and returns it.
|
||||
* Arguments: locSym : local long symbol table
|
||||
@ -62,8 +75,7 @@ ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
|
||||
* ix : index into icode array where this var is used */
|
||||
void LOCAL_ID::newIdent(hlType t, frameType f)
|
||||
{
|
||||
ID newid(t,f);
|
||||
id_arr.push_back(newid);
|
||||
id_arr.emplace_back(t,f);
|
||||
}
|
||||
|
||||
|
||||
@ -71,19 +83,16 @@ void LOCAL_ID::newIdent(hlType t, frameType f)
|
||||
* TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
|
||||
int LOCAL_ID::newByteWordReg(hlType t, eReg regi)
|
||||
{
|
||||
int idx;
|
||||
|
||||
/* Check for entry in the table */
|
||||
auto found=std::find_if(id_arr.begin(),id_arr.end(),[t,regi](ID &el)->bool {
|
||||
return ((el.type == t) && (el.id.regi == regi));
|
||||
return ((el.type == t) and (el.id.regi == regi));
|
||||
});
|
||||
if(found!=id_arr.end())
|
||||
return found-id_arr.begin();
|
||||
/* Not in table, create new identifier */
|
||||
newIdent (t, REG_FRAME);
|
||||
idx = id_arr.size() - 1;
|
||||
id_arr[idx].id.regi = regi;
|
||||
return (idx);
|
||||
id_arr.back().id.regi = regi;
|
||||
return id_arr.size() - 1;
|
||||
}
|
||||
|
||||
|
||||
@ -96,11 +105,8 @@ int LOCAL_ID::newByteWordReg(hlType t, eReg regi)
|
||||
void LOCAL_ID::flagByteWordId (int off)
|
||||
{
|
||||
auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool {
|
||||
//if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) &&
|
||||
if ((en.typeBitsize()<=16) &&
|
||||
(en.id.bwId.off == off) && (en.id.bwId.regOff == 0))
|
||||
return true;
|
||||
return false;
|
||||
//if (((en.type == TYPE_WORD_SIGN) or (en.type == TYPE_BYTE_SIGN)) and
|
||||
return ((en.typeBitsize()<=16) and (en.id.bwId.off == off) and (en.id.bwId.regOff == 0));
|
||||
});
|
||||
if(found==id_arr.end())
|
||||
{
|
||||
@ -114,11 +120,9 @@ void LOCAL_ID::flagByteWordId (int off)
|
||||
* TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
|
||||
int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
|
||||
{
|
||||
int idx;
|
||||
|
||||
/* Check for entry in the table */
|
||||
auto found=std::find_if(id_arr.begin(),id_arr.end(),[off,regOff](ID &el)->bool {
|
||||
if ((el.id.bwId.off == off) && (el.id.bwId.regOff == regOff))
|
||||
if ((el.id.bwId.off == off) and (el.id.bwId.regOff == regOff))
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
@ -127,10 +131,10 @@ int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
|
||||
|
||||
/* Not in table, create new identifier */
|
||||
newIdent (t, STK_FRAME);
|
||||
idx = id_arr.size() - 1;
|
||||
id_arr[idx].id.bwId.regOff = regOff;
|
||||
id_arr[idx].id.bwId.off = off;
|
||||
return (idx);
|
||||
ID &last_id(id_arr.back());
|
||||
last_id.id.bwId.regOff = regOff;
|
||||
last_id.id.bwId.off = off;
|
||||
return id_arr.size()-1;
|
||||
}
|
||||
|
||||
|
||||
@ -148,9 +152,9 @@ int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, eReg regi, hlType t)
|
||||
/* Check for entry in the table */
|
||||
for (size_t idx = 0; idx < id_arr.size(); idx++)
|
||||
{
|
||||
if (/*(locSym->id[idx].type == t) && Not checking type */
|
||||
(id_arr[idx].id.bwGlb.seg == seg) &&
|
||||
(id_arr[idx].id.bwGlb.off == off) &&
|
||||
if (/*(locSym->id[idx].type == t) and Not checking type */
|
||||
(id_arr[idx].id.bwGlb.seg == seg) and
|
||||
(id_arr[idx].id.bwGlb.off == off) and
|
||||
(id_arr[idx].id.bwGlb.regi == regi))
|
||||
return (idx);
|
||||
}
|
||||
@ -178,31 +182,30 @@ int LOCAL_ID::newLongReg(hlType t, const LONGID_TYPE &longT, iICODE ix_)
|
||||
for (idx = 0; idx < id_arr.size(); idx++)
|
||||
{
|
||||
ID &entry(id_arr[idx]);
|
||||
if(!entry.isLong() || (entry.loc != REG_FRAME))
|
||||
if(not entry.isLongRegisterPair())
|
||||
continue;
|
||||
if (/*(locSym->id[idx].type == t) && Not checking type */
|
||||
(entry.longId().h() == regH) &&
|
||||
if (/*(locSym->id[idx].type == t) and Not checking type */
|
||||
(entry.longId().h() == regH) and
|
||||
(entry.longId().l() == regL))
|
||||
{
|
||||
/* Check for occurrence in the list */
|
||||
if (entry.idx.inList(ix_))
|
||||
return (idx);
|
||||
return idx;
|
||||
else
|
||||
{
|
||||
/* Insert icode index in list */
|
||||
entry.idx.push_back(ix_);
|
||||
return (idx);
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Not in the table, create new identifier */
|
||||
id_arr.push_back(ID(t, LONGID_TYPE(regH,regL)));
|
||||
id_arr.emplace_back(t, LONGID_TYPE(regH,regL));
|
||||
id_arr.back().idx.push_back(ix_);
|
||||
return (id_arr.size() - 1);
|
||||
}
|
||||
/* Returns an identifier conditional expression node of type TYPE_LONG or
|
||||
* TYPE_WORD_SIGN */
|
||||
/** \returns an identifier conditional expression node of type TYPE_LONG or TYPE_WORD_SIGN */
|
||||
AstIdent * LOCAL_ID::createId(const ID *retVal, iICODE ix_)
|
||||
{
|
||||
return AstIdent::idID(retVal,this,ix_);
|
||||
@ -218,16 +221,16 @@ int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
|
||||
/* Check for entry in the table */
|
||||
for (idx = 0; idx < id_arr.size(); idx++)
|
||||
{
|
||||
if (/*(locSym->id[idx].type == t) && Not checking type */
|
||||
(id_arr[idx].id.longGlb.seg == seg) &&
|
||||
(id_arr[idx].id.longGlb.offH == offH) &&
|
||||
if (/*(locSym->id[idx].type == t) and Not checking type */
|
||||
(id_arr[idx].id.longGlb.seg == seg) and
|
||||
(id_arr[idx].id.longGlb.offH == offH) and
|
||||
(id_arr[idx].id.longGlb.offL == offL))
|
||||
return (idx);
|
||||
}
|
||||
printf("%d",t);
|
||||
/* Not in the table, create new identifier */
|
||||
id_arr.push_back(ID(t, LONGGLB_TYPE(seg,offH,offL)));
|
||||
return (id_arr.size() - 1);
|
||||
id_arr.emplace_back(t, LONGGLB_TYPE(seg,offH,offL));
|
||||
return id_arr.size() - 1;
|
||||
|
||||
}
|
||||
|
||||
@ -237,27 +240,20 @@ int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
|
||||
* TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
|
||||
int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
|
||||
{
|
||||
size_t idx;
|
||||
|
||||
/* Check for entry in the table */
|
||||
for (idx = 0; idx < id_arr.size(); idx++)
|
||||
for (size_t idx = 0; idx < id_arr.size(); idx++)
|
||||
{
|
||||
if (/*(locSym->id[idx].type == t) && Not checking type */
|
||||
(id_arr[idx].id.longGlb.seg == seg) &&
|
||||
(id_arr[idx].id.longGlb.offH == offH) &&
|
||||
(id_arr[idx].id.longGlb.offL == offL) &&
|
||||
if (/*(locSym->id[idx].type == t) and Not checking type */
|
||||
(id_arr[idx].id.longGlb.seg == seg) and
|
||||
(id_arr[idx].id.longGlb.offH == offH) and
|
||||
(id_arr[idx].id.longGlb.offL == offL) and
|
||||
(id_arr[idx].id.longGlb.regi == regi))
|
||||
return (idx);
|
||||
}
|
||||
|
||||
/* Not in the table, create new identifier */
|
||||
newIdent (t, GLB_FRAME);
|
||||
idx = id_arr.size() - 1;
|
||||
id_arr[idx].id.longGlb.seg = seg;
|
||||
id_arr[idx].id.longGlb.offH = offH;
|
||||
id_arr[idx].id.longGlb.offL = offL;
|
||||
id_arr[idx].id.longGlb.regi = regi;
|
||||
return (idx);
|
||||
id_arr.emplace_back(t,LONGGLB_TYPE(seg,offH,offL,regi));
|
||||
return id_arr.size() - 1;
|
||||
}
|
||||
|
||||
|
||||
@ -272,8 +268,8 @@ int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
|
||||
{
|
||||
if(id_arr[idx].loc!=STK_FRAME)
|
||||
continue;
|
||||
if ((id_arr[idx].type == t) &&
|
||||
(id_arr[idx].longStkId().offH == offH) &&
|
||||
if ((id_arr[idx].type == t) and
|
||||
(id_arr[idx].longStkId().offH == offH) and
|
||||
(id_arr[idx].longStkId().offL == offL))
|
||||
return (idx);
|
||||
}
|
||||
@ -283,7 +279,7 @@ int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
|
||||
flagByteWordId (offL);
|
||||
|
||||
/* Create new identifier */
|
||||
id_arr.push_back(ID(t,LONG_STKID_TYPE(offH,offL)));
|
||||
id_arr.emplace_back(t,LONG_STKID_TYPE(offH,offL));
|
||||
return (id_arr.size() - 1);
|
||||
}
|
||||
|
||||
@ -320,9 +316,9 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, L
|
||||
}
|
||||
|
||||
else if (pmL->off) { /* offset */
|
||||
if ((pmL->seg == rSS) && (pmL->regi == INDEX_BP)) /* idx on bp */
|
||||
if ((pmL->seg == rSS) and (pmL->regi == INDEX_BP)) /* idx on bp */
|
||||
idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
|
||||
else if ((pmL->seg == rDS) && (pmL->regi == INDEX_BX)) /* bx */
|
||||
else if ((pmL->seg == rDS) and (pmL->regi == INDEX_BX)) /* bx */
|
||||
{ /* glb var indexed on bx */
|
||||
printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
|
||||
idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
|
||||
@ -332,10 +328,10 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, L
|
||||
printf ("long not supported, idx <> bp\n");
|
||||
}
|
||||
|
||||
else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
|
||||
printf ("long not supported, idx && no off\n");
|
||||
else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
|
||||
printf ("long not supported, idx and no off\n");
|
||||
|
||||
return (idx);
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
@ -357,7 +353,7 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
|
||||
pmLdst = &atOffset.m_dst;
|
||||
pmHsrc = &pIcode->ll()->src();
|
||||
pmLsrc = &atOffset.src();
|
||||
// if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
|
||||
// if ((longId.offH == pmHsrc->off) and (longId.offL == pmLsrc->off))
|
||||
// {
|
||||
// asgn.lhs = AstIdent::LongIdx (i);
|
||||
|
||||
@ -367,14 +363,14 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
// else if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
|
||||
// else if ((longId.offH == pmHdst->off) and (longId.offL == pmLdst->off))
|
||||
// {
|
||||
// asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
|
||||
// asgn.rhs = AstIdent::LongIdx (i);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
|
||||
if ((longId.offH == pmHdst->off) and (longId.offL == pmLdst->off))
|
||||
{
|
||||
asgn.lhs = AstIdent::LongIdx (i);
|
||||
|
||||
@ -384,7 +380,7 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
|
||||
else if ((longId.offH == pmHsrc->off) and (longId.offL == pmLsrc->off))
|
||||
{
|
||||
asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
|
||||
asgn.rhs = AstIdent::LongIdx (i);
|
||||
@ -414,7 +410,7 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
|
||||
pmHsrc = &pIcode->ll()->src();
|
||||
pmLsrc = &atOffset.src();
|
||||
|
||||
if ((longId.h() == pmHdst->regi) && (longId.l() == pmLdst->regi))
|
||||
if ((longId.h() == pmHdst->regi) and (longId.l() == pmLdst->regi))
|
||||
{
|
||||
asgn.lhs = AstIdent::LongIdx (i);
|
||||
if ( not pIcode->ll()->testFlags(NO_SRC) )
|
||||
@ -423,7 +419,7 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if ((longId.h() == pmHsrc->regi) && (longId.l() == pmLsrc->regi))
|
||||
else if ((longId.h() == pmHsrc->regi) and (longId.l() == pmLsrc->regi))
|
||||
{
|
||||
asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, atOffset);
|
||||
asgn.rhs = AstIdent::LongIdx (i);
|
||||
@ -432,25 +428,19 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Given an index into the local identifier table for a long register
|
||||
* variable, determines whether regi is the high or low part, and returns
|
||||
* the other part */
|
||||
eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
|
||||
eReg LOCAL_ID::getPairedRegisterAt(int idx,eReg regi) const
|
||||
{
|
||||
ID *id;
|
||||
|
||||
id = &locTbl->id_arr[idx];
|
||||
if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
|
||||
(id->type == TYPE_LONG_UNSIGN)))
|
||||
eReg res=rUNDEF; // Cristina: please check this!
|
||||
const ID *id = &id_arr[idx];
|
||||
if (id->isLongRegisterPair())
|
||||
{
|
||||
if (id->longId().h() == regi)
|
||||
return (id->longId().l());
|
||||
else if (id->longId().l() == regi)
|
||||
return (id->longId().h());
|
||||
res = id->getPairedRegister(regi);
|
||||
}
|
||||
return rUNDEF; // Cristina: please check this!
|
||||
qWarning() << "Cannot find paired register";
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@ -458,7 +448,7 @@ eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
|
||||
* the local identifier table. If so, macros for these registers are
|
||||
* placed in the local identifier table, as these registers belong to a
|
||||
* long register identifier. */
|
||||
void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
|
||||
void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const QString &name)
|
||||
{
|
||||
for (ID &rid : id_arr)
|
||||
{
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
#include <cassert>
|
||||
#include "machine_x86.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "icode.h"
|
||||
|
||||
#include <QtCore/QTextStream>
|
||||
#include <cassert>
|
||||
|
||||
// Index registers **** temp solution
|
||||
static const std::string regNames[] = {
|
||||
static const QString regNames[] = {
|
||||
"undef",
|
||||
"ax", "cx", "dx", "bx",
|
||||
"sp", "bp", "si", "di",
|
||||
@ -17,17 +22,17 @@ static const std::string regNames[] = {
|
||||
/* uint8_t and uint16_t registers */
|
||||
Machine_X86::Machine_X86()
|
||||
{
|
||||
static_assert((sizeof(regNames)/sizeof(std::string))==LAST_REG,
|
||||
static_assert((sizeof(regNames)/sizeof(QString))==LAST_REG,
|
||||
"Reg count not equal number of strings");
|
||||
}
|
||||
|
||||
const std::string &Machine_X86::regName(eReg r)
|
||||
const QString &Machine_X86::regName(eReg r)
|
||||
{
|
||||
assert(r<(sizeof(regNames)/sizeof(std::string)));
|
||||
assert(r<(sizeof(regNames)/sizeof(QString)));
|
||||
return regNames[r];
|
||||
}
|
||||
|
||||
static const std::string szOps[] =
|
||||
static const QString szOps[] =
|
||||
{
|
||||
"CBW", "AAA", "AAD", "AAM", "AAS", "ADC", "ADD", "AND",
|
||||
"BOUND","CALL", "CALL", "CLC", "CLD", "CLI", "CMC", "CMP",
|
||||
@ -45,7 +50,7 @@ static const std::string szOps[] =
|
||||
"XLAT", "XOR", "INTO", "NOP", "REPNE", "REPE", "MOD"
|
||||
};
|
||||
/* The following opcodes are for mod != 3 */
|
||||
static std::string szFlops1[] =
|
||||
static const QString szFlops1[] =
|
||||
{
|
||||
/* 0 1 2 3 4 5 6 7 */
|
||||
"FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
|
||||
@ -58,7 +63,7 @@ static std::string szFlops1[] =
|
||||
"FILD", "???", "FIST", "FISTP", "FBLD", "???", "FBSTP", "FISTP" /* 38 */
|
||||
};
|
||||
/* The following opcodes are for mod == 3 */
|
||||
static std::string szFlops2[] =
|
||||
static const QString szFlops2[] =
|
||||
{
|
||||
/* 0 1 2 3 4 5 6 7 */
|
||||
"FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
|
||||
@ -71,17 +76,17 @@ static std::string szFlops2[] =
|
||||
"FILD", "???", "FIST", "FISTP", "", "???", "FBSTP", "FISTP" /* 38 */
|
||||
};
|
||||
|
||||
const std::string &Machine_X86::opcodeName(unsigned r)
|
||||
const QString &Machine_X86::opcodeName(unsigned r)
|
||||
{
|
||||
assert(r<(sizeof(szOps)/sizeof(std::string)));
|
||||
assert(r<(sizeof(szOps)/sizeof(QString)));
|
||||
return szOps[r];
|
||||
}
|
||||
const std::string &Machine_X86::floatOpName(unsigned r)
|
||||
const QString &Machine_X86::floatOpName(unsigned r)
|
||||
{
|
||||
if(r>=(sizeof(szFlops1)/sizeof(std::string)))
|
||||
if(r>=(sizeof(szFlops1)/sizeof(QString)))
|
||||
{
|
||||
r-= (sizeof(szFlops1)/sizeof(std::string));
|
||||
assert(r<(sizeof(szFlops2)/sizeof(std::string)));
|
||||
r-= (sizeof(szFlops1)/sizeof(QString));
|
||||
assert(r<(sizeof(szFlops2)/sizeof(QString)));
|
||||
return szFlops2[r];
|
||||
}
|
||||
return szFlops1[r];
|
||||
@ -89,11 +94,11 @@ const std::string &Machine_X86::floatOpName(unsigned r)
|
||||
|
||||
bool Machine_X86::physicalReg(eReg r)
|
||||
{
|
||||
return (r>=rAX) && (r<rTMP);
|
||||
return (r>=rAX) and (r<rTMP);
|
||||
}
|
||||
bool Machine_X86::isMemOff(eReg r)
|
||||
{
|
||||
return r == 0 || r >= INDEX_BX_SI;
|
||||
return r == 0 or r >= INDEX_BX_SI;
|
||||
}
|
||||
//TODO: Move these to Machine_X86
|
||||
eReg Machine_X86::subRegH(eReg reg)
|
||||
@ -106,18 +111,18 @@ eReg Machine_X86::subRegL(eReg reg)
|
||||
}
|
||||
bool Machine_X86::isSubRegisterOf(eReg reg,eReg parent)
|
||||
{
|
||||
if ((parent < rAX) || (parent > rBX))
|
||||
if ((parent < rAX) or (parent > rBX))
|
||||
return false; // only AX -> BX are coverede by subregisters
|
||||
return ((reg==subRegH(parent)) || (reg == subRegL(parent)));
|
||||
return ((reg==subRegH(parent)) or (reg == subRegL(parent)));
|
||||
}
|
||||
bool Machine_X86::hasSubregisters(eReg reg)
|
||||
{
|
||||
return ((reg >= rAX) && (reg <= rBX));
|
||||
return ((reg >= rAX) and (reg <= rBX));
|
||||
}
|
||||
|
||||
bool Machine_X86::isPartOfComposite(eReg reg)
|
||||
{
|
||||
return ((reg >= rAL) && (reg <= rBH));
|
||||
return ((reg >= rAL) and (reg <= rBH));
|
||||
}
|
||||
|
||||
eReg Machine_X86::compositeParent(eReg reg)
|
||||
@ -133,7 +138,7 @@ eReg Machine_X86::compositeParent(eReg reg)
|
||||
}
|
||||
return rUNDEF;
|
||||
}
|
||||
void Machine_X86::writeRegVector (std::ostream &ostr,const LivenessSet ®i)
|
||||
void Machine_X86::writeRegVector (QTextStream &ostr,const LivenessSet ®i)
|
||||
{
|
||||
int j;
|
||||
for (j = rAX; j < INDEX_BX_SI; j++)
|
||||
|
||||
336
src/parser.cpp
336
src/parser.cpp
@ -2,17 +2,21 @@
|
||||
* dcc project procedure list builder
|
||||
* (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
|
||||
****************************************************************************/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h> /* For exit() */
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <QMap>
|
||||
#include <QtCore/QDebug>
|
||||
#include <inttypes.h>
|
||||
#include <cstring>
|
||||
#include <cstdlib> /* For exit() */
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate);
|
||||
@ -38,7 +42,7 @@ ICODE * Function::translate_DIV(LLInst *ll, ICODE &_Icode)
|
||||
|
||||
ICODE eIcode = ICODE();
|
||||
|
||||
eIcode.type = LOW_LEVEL;
|
||||
eIcode.type = LOW_LEVEL_ICODE;
|
||||
eIcode.ll()->set(iMOV,0,rTMP);
|
||||
if (ll->testFlags(B) )
|
||||
{
|
||||
@ -62,7 +66,7 @@ ICODE * Function::translate_DIV(LLInst *ll, ICODE &_Icode)
|
||||
|
||||
/* iMOD */
|
||||
eIcode = ICODE();
|
||||
eIcode.type = LOW_LEVEL;
|
||||
eIcode.type = LOW_LEVEL_ICODE;
|
||||
eIcode.ll()->set(iMOD,ll->getFlag() | SYNTHETIC | IM_TMP_DST);
|
||||
eIcode.ll()->replaceSrc(_Icode.ll()->src());
|
||||
eIcode.du = _Icode.du;
|
||||
@ -73,14 +77,14 @@ ICODE *Function::translate_XCHG(LLInst *ll,ICODE &_Icode)
|
||||
{
|
||||
/* MOV rTMP, regDst */
|
||||
ICODE eIcode;
|
||||
eIcode.type = LOW_LEVEL;
|
||||
eIcode.type = LOW_LEVEL_ICODE;
|
||||
eIcode.ll()->set(iMOV,SYNTHETIC,rTMP,ll->m_dst);
|
||||
eIcode.setRegDU( rTMP, eDEF);
|
||||
if(eIcode.ll()->src().getReg2())
|
||||
{
|
||||
eReg srcreg=eIcode.ll()->src().getReg2();
|
||||
eIcode.setRegDU( srcreg, eUSE);
|
||||
if((srcreg>=rAL) && (srcreg<=rBH))
|
||||
if((srcreg>=rAL) and (srcreg<=rBH))
|
||||
eIcode.ll()->setFlags( B );
|
||||
}
|
||||
eIcode.ll()->label = ll->label;
|
||||
@ -93,12 +97,12 @@ ICODE *Function::translate_XCHG(LLInst *ll,ICODE &_Icode)
|
||||
|
||||
/* MOV regSrc, rTMP */
|
||||
eIcode = ICODE();
|
||||
eIcode.type = LOW_LEVEL;
|
||||
eIcode.type = LOW_LEVEL_ICODE;
|
||||
eIcode.ll()->set(iMOV,SYNTHETIC);
|
||||
eIcode.ll()->replaceDst(ll->src());
|
||||
if(eIcode.ll()->m_dst.regi)
|
||||
{
|
||||
if((eIcode.ll()->m_dst.regi>=rAL) && (eIcode.ll()->m_dst.regi<=rBH))
|
||||
if((eIcode.ll()->m_dst.regi>=rAL) and (eIcode.ll()->m_dst.regi<=rBH))
|
||||
eIcode.ll()->setFlags( B );
|
||||
eIcode.setRegDU( eIcode.ll()->m_dst.regi, eDEF);
|
||||
}
|
||||
@ -120,28 +124,28 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
eErrorId err;
|
||||
bool done = false;
|
||||
SYMTAB &global_symbol_table(Project::get()->symtab);
|
||||
if (name.find("chkstk") != string::npos)
|
||||
if (name.contains("chkstk"))
|
||||
{
|
||||
// Danger! Dcc will likely fall over in this code.
|
||||
// So we act as though we have done with this proc
|
||||
// pProc->flg &= ~TERMINATES; // Not sure about this
|
||||
done = true;
|
||||
// And mark it as a library function, so structure() won't choke on it
|
||||
flg |= PROC_ISLIB;
|
||||
return;
|
||||
}
|
||||
if (option.VeryVerbose)
|
||||
{
|
||||
printf("Parsing proc %s at %X\n", name.c_str(), pstate->IP);
|
||||
qDebug() << "Parsing proc" << name << "at" << QString::number(pstate->IP,16).toUpper();
|
||||
}
|
||||
|
||||
while (! done )
|
||||
while (not done )
|
||||
{
|
||||
err = scan(pstate->IP, _Icode);
|
||||
if(err)
|
||||
break;
|
||||
LLInst *ll = _Icode.ll();
|
||||
pstate->IP += (uint32_t)ll->numBytes;
|
||||
|
||||
setBits(BM_CODE, ll->label, (uint32_t)ll->numBytes);
|
||||
|
||||
process_operands(_Icode,pstate);
|
||||
@ -153,14 +157,14 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
iICODE labLoc = Icode.labelSrch(ll->label);
|
||||
if (Icode.end()!=labLoc)
|
||||
{ /* Synthetic jump */
|
||||
_Icode.type = LOW_LEVEL;
|
||||
_Icode.type = LOW_LEVEL_ICODE;
|
||||
ll->set(iJMP,I | SYNTHETIC | NO_OPS);
|
||||
ll->replaceSrc(LLOperand::CreateImm2(labLoc->ll()->GetLlLabel()));
|
||||
ll->label = SynthLab++;
|
||||
}
|
||||
|
||||
/* Copy Icode to Proc */
|
||||
if ((ll->getOpcode() == iDIV) || (ll->getOpcode() == iIDIV))
|
||||
if ((ll->getOpcode() == iDIV) or (ll->getOpcode() == iIDIV))
|
||||
pIcode = translate_DIV(ll, _Icode);
|
||||
else if (_Icode.ll()->getOpcode() == iXCHG)
|
||||
pIcode = translate_XCHG(ll, _Icode);
|
||||
@ -177,6 +181,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
case iJCXZ:
|
||||
{
|
||||
STATE StCopy;
|
||||
uint32_t lastIp = pstate->IP - 2;
|
||||
int ip = Icode.size()-1; /* Index of this jump */
|
||||
ICODE &prev(*(++Icode.rbegin())); /* Previous icode */
|
||||
bool fBranch = false;
|
||||
@ -186,18 +191,18 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
/* This sets up range check for indexed JMPs hopefully
|
||||
* Handles JA/JAE for fall through and JB/JBE on branch
|
||||
*/
|
||||
if (ip > 0 && prev.ll()->getOpcode() == iCMP && (prev.ll()->testFlags(I)))
|
||||
if (ip > 0 and prev.ll()->getOpcode() == iCMP and (prev.ll()->testFlags(I)))
|
||||
{
|
||||
pstate->JCond.immed = (int16_t)prev.ll()->src().getImm2();
|
||||
if (ll->match(iJA) || ll->match(iJBE) )
|
||||
if (ll->match(iJA) or ll->match(iJBE) )
|
||||
pstate->JCond.immed++;
|
||||
if (ll->getOpcode() == iJAE || ll->getOpcode() == iJA)
|
||||
if (ll->getOpcode() == iJAE or ll->getOpcode() == iJA)
|
||||
pstate->JCond.regi = prev.ll()->m_dst.regi;
|
||||
fBranch = (bool)
|
||||
(ll->getOpcode() == iJB || ll->getOpcode() == iJBE);
|
||||
fBranch = (bool) (ll->getOpcode() == iJB or ll->getOpcode() == iJBE);
|
||||
}
|
||||
StCopy = *pstate;
|
||||
//memcpy(&StCopy, pstate, sizeof(STATE));
|
||||
|
||||
//printf("From %X condJump to %X\n", lastIp, pstate->IP);
|
||||
|
||||
/* Straight line code */
|
||||
this->FollowCtrl (pcallGraph, &StCopy); // recurrent ?
|
||||
@ -236,7 +241,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
break;
|
||||
|
||||
case iINT:
|
||||
if (ll->src().getImm2() == 0x21 && pstate->f[rAH])
|
||||
if (ll->src().getImm2() == 0x21 and pstate->f[rAH])
|
||||
{
|
||||
int funcNum = pstate->r[rAH];
|
||||
int operand;
|
||||
@ -247,7 +252,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
//Icode.GetIcode(Icode.GetNumIcodes() - 1)->
|
||||
|
||||
/* Program termination: int21h, fn 00h, 31h, 4Ch */
|
||||
done = (bool)(funcNum == 0x00 || funcNum == 0x31 ||
|
||||
done = (bool)(funcNum == 0x00 or funcNum == 0x31 or
|
||||
funcNum == 0x4C);
|
||||
|
||||
/* String functions: int21h, fn 09h */
|
||||
@ -262,12 +267,12 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
global_symbol_table.updateSymType (operand, TypeContainer(TYPE_STR, size));
|
||||
}
|
||||
}
|
||||
else if ((ll->src().getImm2() == 0x2F) && (pstate->f[rAH]))
|
||||
else if ((ll->src().getImm2() == 0x2F) and (pstate->f[rAH]))
|
||||
{
|
||||
Icode.back().ll()->m_dst.off = pstate->r[rAH];
|
||||
}
|
||||
else /* Program termination: int20h, int27h */
|
||||
done = (ll->src().getImm2() == 0x20 || ll->src().getImm2() == 0x27);
|
||||
done = (ll->src().getImm2() == 0x20 or ll->src().getImm2() == 0x27);
|
||||
if (done)
|
||||
pIcode->ll()->setFlags(TERMINATES);
|
||||
break;
|
||||
@ -284,7 +289,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
case iSHL:
|
||||
if (pstate->JCond.regi == ll->m_dst.regi)
|
||||
{
|
||||
if ((ll->testFlags(I)) && ll->src().getImm2() == 1)
|
||||
if ((ll->testFlags(I)) and ll->src().getImm2() == 1)
|
||||
pstate->JCond.immed *= 2;
|
||||
else
|
||||
pstate->JCond.regi = 0;
|
||||
@ -298,7 +303,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
|
||||
case iLDS: case iLES:
|
||||
if ((psym = lookupAddr(&ll->src(), pstate, 4, eDuVal::USE))
|
||||
/* && (Icode.ll()->flg & SEG_IMMED) */ )
|
||||
/* and (Icode.ll()->flg & SEG_IMMED) */ )
|
||||
{
|
||||
offset = LH(&prog.image()[psym->label]);
|
||||
pstate->setState( (ll->getOpcode() == iLDS)? rDS: rES,
|
||||
@ -313,7 +318,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
|
||||
if (err) {
|
||||
this->flg &= ~TERMINATES;
|
||||
|
||||
if (err == INVALID_386OP || err == INVALID_OPCODE)
|
||||
if (err == INVALID_386OP or err == INVALID_OPCODE)
|
||||
{
|
||||
fatalError(err, prog.image()[_Icode.ll()->label], _Icode.ll()->label);
|
||||
this->flg |= PROC_BADINST;
|
||||
@ -367,12 +372,185 @@ bool Function::followAllTableEntries(JumpTable &table, uint32_t cs, ICODE& pIcod
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Function::decodeIndirectJMP(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
// mov cx,NUM_CASES
|
||||
// mov bx,JUMP_TABLE
|
||||
|
||||
// LAB1:
|
||||
// mov ax, [bx]
|
||||
// cmp ax,VAL
|
||||
// jz LAB2
|
||||
// add bx,2
|
||||
// loop LAB1
|
||||
// jmp DEFAULT_CASE
|
||||
// LAB2
|
||||
// jmp word ptr [bx+2*NUM_CASES]
|
||||
static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
|
||||
|
||||
if(Icode.size()<8)
|
||||
return false;
|
||||
if(&Icode.back()!=&pIcode) // not the last insn in the list skip it
|
||||
return false;
|
||||
if(pIcode.ll()->src().regi != INDEX_BX) {
|
||||
return false;
|
||||
}
|
||||
// find address-wise predecessors of the icode
|
||||
std::deque<ICODE *> matched;
|
||||
QMap<uint32_t,ICODE *> addrmap;
|
||||
for(ICODE & ic : Icode) {
|
||||
addrmap[ic.ll()->GetLlLabel()] = ⁣
|
||||
}
|
||||
auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
|
||||
while(matched.size()<9) {
|
||||
matched.push_front(*iter);
|
||||
--iter;
|
||||
if(iter==addrmap.end())
|
||||
return false;
|
||||
}
|
||||
// pattern starts at the last jmp
|
||||
ICODE *load_num_cases = matched[0];
|
||||
ICODE *load_jump_table_addr = matched[1];
|
||||
ICODE *read_case_entry_insn = matched[2];
|
||||
ICODE *cmp_case_val_insn = matched[3];
|
||||
ICODE *exit_loop_insn = matched[4];
|
||||
ICODE *add_bx_insn = matched[5];
|
||||
ICODE *loop_insn = matched[6];
|
||||
ICODE *default_jmp = matched[7];
|
||||
ICODE *last_jmp = matched[8];
|
||||
for(int i=0; i<8; ++i) {
|
||||
if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
|
||||
qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(int i=0; i<9; ++i) {
|
||||
if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// verify that bx+offset == 2* case count
|
||||
uint32_t num_cases = load_num_cases->ll()->src().getImm2();
|
||||
if(last_jmp->ll()->src().off != 2*num_cases)
|
||||
return false;
|
||||
const LLOperand &op = last_jmp->ll()->src();
|
||||
if(op.regi != INDEX_BX)
|
||||
return false;
|
||||
if(not load_jump_table_addr->ll()->src().isImmediate())
|
||||
return false;
|
||||
uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
|
||||
uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
|
||||
uint32_t default_label = cs + default_jmp->ll()->src().getImm2();
|
||||
setBits(BM_DATA, table_addr, num_cases*2 + num_cases*2); // num_cases of short values + num cases short ptrs
|
||||
pIcode.ll()->setFlags(SWITCH);
|
||||
|
||||
for(int i=0; i<num_cases; ++i) {
|
||||
STATE StCopy = *pstate;
|
||||
uint32_t jump_target_location = table_addr + num_cases*2 + i*2;
|
||||
StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
|
||||
iICODE last_current_insn = (++Icode.rbegin()).base();
|
||||
FollowCtrl (pcallGraph, &StCopy);
|
||||
++last_current_insn;
|
||||
last_current_insn->ll()->caseEntry = i;
|
||||
last_current_insn->ll()->setFlags(CASE);
|
||||
pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool Function::decodeIndirectJMP2(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
// mov cx,NUM_CASES
|
||||
// mov bx,JUMP_TABLE
|
||||
|
||||
// LAB1:
|
||||
// mov ax, [bx]
|
||||
// cmp ax, LOW_WORD_OF_VAL
|
||||
// jnz LAB2
|
||||
// mov ax, [bx + 2 * NUM_CASES]
|
||||
// cmp ax, HIGH_WORD_OF_VAL
|
||||
// jz LAB3
|
||||
// LAB2
|
||||
// add bx,2
|
||||
// loop LAB1
|
||||
// jmp DEFAULT_CASE
|
||||
// LAB3
|
||||
// jmp word ptr [bx+2*NUM_CASES]
|
||||
static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJNE,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
|
||||
|
||||
if(Icode.size()<12)
|
||||
return false;
|
||||
if(&Icode.back() != &pIcode) // not the last insn in the list skip it
|
||||
return false;
|
||||
if(pIcode.ll()->src().regi != INDEX_BX) {
|
||||
return false;
|
||||
}
|
||||
// find address-wise predecessors of the icode
|
||||
std::deque<ICODE *> matched;
|
||||
QMap<uint32_t,ICODE *> addrmap;
|
||||
for(ICODE & ic : Icode) {
|
||||
addrmap[ic.ll()->GetLlLabel()] = ⁣
|
||||
}
|
||||
auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
|
||||
while(matched.size()<12) {
|
||||
matched.push_front(*iter);
|
||||
--iter;
|
||||
if(iter==addrmap.end())
|
||||
return false;
|
||||
}
|
||||
// pattern starts at the last jmp
|
||||
ICODE *load_num_cases = matched[0];
|
||||
ICODE *load_jump_table_addr = matched[1];
|
||||
ICODE *default_jmp = matched[10];
|
||||
ICODE *last_jmp = matched[11];
|
||||
|
||||
for(int i=0; i<11; ++i) {
|
||||
if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
|
||||
qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(int i=0; i<12; ++i) {
|
||||
if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// verify that bx+offset == 2* case count
|
||||
uint32_t num_cases = load_num_cases->ll()->src().getImm2();
|
||||
if(last_jmp->ll()->src().off != 4*num_cases)
|
||||
return false;
|
||||
const LLOperand &op = last_jmp->ll()->src();
|
||||
if(op.regi != INDEX_BX)
|
||||
return false;
|
||||
if(not load_jump_table_addr->ll()->src().isImmediate())
|
||||
return false;
|
||||
uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
|
||||
uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
|
||||
int default_label = cs + default_jmp->ll()->src().getImm2();
|
||||
setBits(BM_DATA, table_addr, num_cases*4 + num_cases*2); // num_cases of long values + num cases short ptrs
|
||||
pIcode.ll()->setFlags(SWITCH);
|
||||
|
||||
for(int i=0; i<num_cases; ++i) {
|
||||
STATE StCopy = *pstate;
|
||||
uint32_t jump_target_location = table_addr + num_cases*4 + i*2;
|
||||
StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
|
||||
iICODE last_current_insn = (++Icode.rbegin()).base();
|
||||
FollowCtrl (pcallGraph, &StCopy);
|
||||
++last_current_insn;
|
||||
last_current_insn->ll()->caseEntry = i;
|
||||
last_current_insn->ll()->setFlags(CASE);
|
||||
pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
|
||||
{
|
||||
PROG &prog(Project::get()->prog);
|
||||
static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
|
||||
ICODE _Icode;
|
||||
uint32_t lastIp = pstate->IP - 1;
|
||||
uint32_t cs, offTable, endTable;
|
||||
uint32_t i, k, seg, target;
|
||||
|
||||
@ -380,7 +558,19 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
|
||||
{
|
||||
if (pIcode.ll()->getOpcode() == iJMPF)
|
||||
pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
|
||||
|
||||
pstate->IP = pIcode.ll()->src().getImm2();
|
||||
|
||||
//printf("From seg:%04X JMP(F) to %X\n", lastIp, pstate->IP);
|
||||
|
||||
if (pstate->IP == 0xFFFF0)
|
||||
{
|
||||
/* Nasty (wrong) trick use to reset, consider it as terminating */
|
||||
pIcode.ll()->setFlags(TERMINATES);
|
||||
pstate->setState( rCS, 0);
|
||||
pstate->IP = 0;
|
||||
}
|
||||
|
||||
int64_t i = pIcode.ll()->src().getImm2();
|
||||
if (i < 0)
|
||||
{
|
||||
@ -390,16 +580,15 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
|
||||
/* Return true if jump target is already parsed */
|
||||
return Icode.alreadyDecoded(i);
|
||||
}
|
||||
|
||||
/* We've got an indirect JMP - look for switch() stmt. idiom of the form
|
||||
* JMP uint16_t ptr word_offset[rBX | rSI | rDI] */
|
||||
seg = (pIcode.ll()->src().seg)? pIcode.ll()->src().seg: rDS;
|
||||
|
||||
/* Ensure we have a uint16_t offset & valid seg */
|
||||
if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) &&
|
||||
pstate->f[seg] &&
|
||||
(pIcode.ll()->src().regi == INDEX_SI ||
|
||||
pIcode.ll()->src().regi == INDEX_DI || /* Idx reg. BX, SI, DI */
|
||||
if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) and
|
||||
pstate->f[seg] and
|
||||
(pIcode.ll()->src().regi == INDEX_SI or
|
||||
pIcode.ll()->src().regi == INDEX_DI or /* Idx reg. BX, SI, DI */
|
||||
pIcode.ll()->src().regi == INDEX_BX))
|
||||
{
|
||||
|
||||
@ -429,7 +618,7 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
|
||||
for (i = offTable; i < endTable; i += 2)
|
||||
{
|
||||
target = cs + LH(&prog.image()[i]);
|
||||
if (target < endTable && target >= offTable)
|
||||
if (target < endTable and target >= offTable)
|
||||
endTable = target;
|
||||
else if (target >= (uint32_t)prog.cbImage)
|
||||
endTable = i;
|
||||
@ -439,7 +628,7 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
|
||||
{
|
||||
target = cs + LH(&prog.image()[i]);
|
||||
/* Be wary of 00 00 as code - it's probably data */
|
||||
if (! (prog.image()[target] || prog.image()[target+1]) ||
|
||||
if (not (prog.image()[target] or prog.image()[target+1]) or
|
||||
scan(target, _Icode))
|
||||
endTable = i;
|
||||
}
|
||||
@ -475,6 +664,12 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(decodeIndirectJMP(pIcode,pstate,pcallGraph)) {
|
||||
return true;
|
||||
}
|
||||
if(decodeIndirectJMP2(pIcode,pstate,pcallGraph)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Can't do anything with this jump */
|
||||
|
||||
@ -499,6 +694,7 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
|
||||
PROG &prog(Project::get()->prog);
|
||||
ICODE &last_insn(Icode.back());
|
||||
STATE localState; /* Local copy of the machine state */
|
||||
uint32_t lastIp = pstate->IP - 2;
|
||||
uint32_t off;
|
||||
/* For Indirect Calls, find the function address */
|
||||
bool indirect = false;
|
||||
@ -507,13 +703,12 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
|
||||
{
|
||||
/* Not immediate, i.e. indirect call */
|
||||
|
||||
if (pIcode.ll()->m_dst.regi && (!option.Calls))
|
||||
if (pIcode.ll()->m_dst.regi and (not option.Calls))
|
||||
{
|
||||
/* We have not set the brave option to attempt to follow
|
||||
the execution path through register indirect calls.
|
||||
So we just exit this function, and ignore the call.
|
||||
We probably should not have parsed this deep, anyway.
|
||||
*/
|
||||
We probably should not have parsed this deep, anyway. */
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -581,11 +776,9 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
|
||||
if (indirect)
|
||||
x.flg |= PROC_ICALL;
|
||||
|
||||
if (x.name.empty()) /* Don't overwrite existing name */
|
||||
if (x.name.isEmpty()) /* Don't overwrite existing name */
|
||||
{
|
||||
ostringstream os;
|
||||
os<<"proc_"<< ++prog.cProcs;
|
||||
x.name = os.str();
|
||||
x.name = QString("proc_%1_%2").arg(x.procEntry ,6,16,QChar('0')).arg(++prog.cProcs);
|
||||
}
|
||||
x.depth = x.depth + 1;
|
||||
x.flg |= TERMINATES;
|
||||
@ -595,10 +788,13 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
|
||||
pstate->IP = pIcode.ll()->src().getImm2();
|
||||
if (pIcode.ll()->getOpcode() == iCALLF)
|
||||
pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
|
||||
|
||||
x.state = *pstate;
|
||||
|
||||
/* Insert new procedure in call graph */
|
||||
pcallGraph->insertCallGraph (this, iter);
|
||||
- pcallGraph->insertCallGraph (this, iter);
|
||||
|
||||
//printf("From %X CALL to %X\n", lastIp, pstate->IP);
|
||||
|
||||
/* Process new procedure */
|
||||
x.FollowCtrl (pcallGraph, pstate);
|
||||
@ -629,17 +825,17 @@ static void process_MOV(LLInst & ll, STATE * pstate)
|
||||
SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
|
||||
uint8_t dstReg = ll.m_dst.regi;
|
||||
uint8_t srcReg = ll.src().regi;
|
||||
if (dstReg > 0 && dstReg < INDEX_BX_SI)
|
||||
if (dstReg > 0 and dstReg < INDEX_BX_SI)
|
||||
{
|
||||
if (ll.testFlags(I))
|
||||
pstate->setState( dstReg, (int16_t)ll.src().getImm2());
|
||||
else if (srcReg == 0) /* direct memory offset */
|
||||
{
|
||||
psym = lookupAddr(&ll.src(), pstate, 2, eDuVal::USE);
|
||||
if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val))
|
||||
if (psym and ((psym->flg & SEG_IMMED) or psym->duVal.val))
|
||||
pstate->setState( dstReg, LH(&prog.image()[psym->label]));
|
||||
}
|
||||
else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
|
||||
else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
|
||||
{
|
||||
pstate->setState( dstReg, pstate->r[srcReg]);
|
||||
|
||||
@ -650,10 +846,10 @@ static void process_MOV(LLInst & ll, STATE * pstate)
|
||||
}
|
||||
else if (dstReg == 0) { /* direct memory offset */
|
||||
int size=2;
|
||||
if((ll.src().regi>=rAL)&&(ll.src().regi<=rBH))
|
||||
if((ll.src().regi>=rAL)and(ll.src().regi<=rBH))
|
||||
size=1;
|
||||
psym = lookupAddr (&ll.m_dst, pstate, size, eDEF);
|
||||
if (psym && ! (psym->duVal.val)) /* no initial value yet */
|
||||
if (psym and not (psym->duVal.val)) /* no initial value yet */
|
||||
{
|
||||
if (ll.testFlags(I)) /* immediate */
|
||||
{
|
||||
@ -669,7 +865,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
|
||||
else if (srcReg == 0) /* direct mem offset */
|
||||
{
|
||||
psym2 = lookupAddr (&ll.src(), pstate, 2, eDuVal::USE);
|
||||
if (psym2 && ((psym->flg & SEG_IMMED) || (psym->duVal.val)))
|
||||
if (psym2 and ((psym->flg & SEG_IMMED) or (psym->duVal.val)))
|
||||
{
|
||||
//prog.image()[psym->label] = (uint8_t)prog.image()[psym2->label];
|
||||
pstate->setMemoryByte(psym->label,(uint8_t)prog.image()[psym2->label]);
|
||||
@ -682,7 +878,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
|
||||
psym2->duVal.setFlags(eDuVal::USE);
|
||||
}
|
||||
}
|
||||
else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
|
||||
else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
|
||||
{
|
||||
//prog.image()[psym->label] = (uint8_t)pstate->r[srcReg];
|
||||
pstate->setMemoryByte(psym->label,(uint8_t)pstate->r[srcReg]);
|
||||
@ -864,13 +1060,13 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
|
||||
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
|
||||
}
|
||||
|
||||
else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
|
||||
else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
|
||||
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
|
||||
(uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
|
||||
|
||||
else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
|
||||
else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
|
||||
{
|
||||
if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
|
||||
if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
|
||||
{
|
||||
if (pm->off > 0) /* global indexed variable */
|
||||
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
|
||||
@ -892,7 +1088,7 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
|
||||
}
|
||||
|
||||
/* Use of register */
|
||||
else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
|
||||
else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
|
||||
pIcode.du.use.addReg(pm->regi);
|
||||
}
|
||||
|
||||
@ -924,15 +1120,15 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
|
||||
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
|
||||
}
|
||||
|
||||
else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
|
||||
else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
|
||||
{
|
||||
pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
|
||||
(uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
|
||||
}
|
||||
|
||||
else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
|
||||
else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
|
||||
{
|
||||
if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
|
||||
if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
|
||||
{
|
||||
if (pm->off > 0) /* global var */
|
||||
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
|
||||
@ -941,7 +1137,7 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
|
||||
}
|
||||
}
|
||||
/* Definition of register */
|
||||
else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
|
||||
else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
|
||||
{
|
||||
assert(not pIcode.ll()->match(iPUSH));
|
||||
pIcode.du1.addDef(pm->regi);
|
||||
@ -985,7 +1181,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
case iSAR: case iSHL: case iSHR:
|
||||
case iRCL: case iRCR: case iROL: case iROR:
|
||||
case iADD: case iADC: case iSUB: case iSBB:
|
||||
if (! Imm) {
|
||||
if (not Imm) {
|
||||
use(SRC, pIcode, this, pstate, cb);
|
||||
}
|
||||
case iINC: case iDEC: case iNEG: case iNOT:
|
||||
@ -1003,7 +1199,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
break;
|
||||
|
||||
case iTEST: case iCMP:
|
||||
if (! Imm)
|
||||
if (not Imm)
|
||||
use(SRC, pIcode, this, pstate, cb);
|
||||
use(DST, pIcode, this, pstate, cb);
|
||||
break;
|
||||
@ -1016,7 +1212,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
|
||||
case iMUL: case iIMUL:
|
||||
use(SRC, pIcode, this, pstate, cb);
|
||||
if (! Imm)
|
||||
if (not Imm)
|
||||
{
|
||||
use (DST, pIcode, this, pstate, cb);
|
||||
if (cb == 1)
|
||||
@ -1053,7 +1249,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
case iCALLF: /* Ignore def's on CS for now */
|
||||
cb = 4;
|
||||
case iCALL: case iPUSH: case iPOP:
|
||||
if (! Imm) {
|
||||
if (not Imm) {
|
||||
if (pIcode.ll()->getOpcode() == iPOP)
|
||||
def(DST, pIcode, this, pstate, cb);
|
||||
else
|
||||
@ -1091,7 +1287,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
case iJMPF:
|
||||
cb = 4;
|
||||
case iJMP:
|
||||
if (! Imm)
|
||||
if (not Imm)
|
||||
use(SRC, pIcode, this, pstate, cb);
|
||||
break;
|
||||
|
||||
@ -1118,7 +1314,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
case iSCAS: case iSTOS: case iINS:
|
||||
pIcode.du.def.addReg(rDI);
|
||||
pIcode.du1.addDef(rDI);
|
||||
if (pIcode.ll()->getOpcode() == iREP_INS || pIcode.ll()->getOpcode()== iINS)
|
||||
if (pIcode.ll()->getOpcode() == iREP_INS or pIcode.ll()->getOpcode()== iINS)
|
||||
{
|
||||
pIcode.du.use.addReg(rDI).addReg(rES).addReg(rDX);
|
||||
}
|
||||
@ -1152,7 +1348,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
|
||||
|
||||
case iIN: case iOUT:
|
||||
def(DST, pIcode, this, pstate, cb);
|
||||
if (! Imm)
|
||||
if (not Imm)
|
||||
{
|
||||
pIcode.du.use.addReg(rDX);
|
||||
}
|
||||
|
||||
@ -5,12 +5,15 @@
|
||||
* (C) Cristina Cifuentes
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
#include "project.h"
|
||||
#include "CallGraph.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
|
||||
extern Project g_proj;
|
||||
/* Static indentation buffer */
|
||||
static constexpr int indSize=81; /* size of indentation buffer; max 20 */
|
||||
@ -64,7 +67,7 @@ bool CALL_GRAPH::insertCallGraph(Function *caller, ilFunction callee)
|
||||
* the nodes the procedure invokes. */
|
||||
void CALL_GRAPH::writeNodeCallGraph(int indIdx)
|
||||
{
|
||||
printf ("%s%s\n", indentStr(indIdx), proc->name.c_str());
|
||||
qDebug() << indentStr(indIdx)+proc->name;
|
||||
for (CALL_GRAPH *cg : outEdges)
|
||||
cg->writeNodeCallGraph (indIdx + 1);
|
||||
}
|
||||
@ -85,7 +88,7 @@ void CALL_GRAPH::write()
|
||||
/* Updates the argument table by including the register(s) (ie. lhs of
|
||||
* picode) and the actual expression (ie. rhs of picode).
|
||||
* Note: register(s) are only included once in the table. */
|
||||
void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
|
||||
void LOCAL_ID::newRegArg(ICODE &picode, ICODE &ticode) const
|
||||
{
|
||||
AstIdent *lhs;
|
||||
STKFRAME * call_args_stackframe, *target_stackframe;
|
||||
@ -98,20 +101,20 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
|
||||
eReg regH; /* Registers involved in arguments */
|
||||
|
||||
/* Flag ticode as having register arguments */
|
||||
tproc = ticode->hl()->call.proc;
|
||||
tproc = ticode.hl()->call.proc;
|
||||
tproc->flg |= REG_ARGS;
|
||||
|
||||
/* Get registers and index into target procedure's local list */
|
||||
call_args_stackframe = ticode->hl()->call.args;
|
||||
call_args_stackframe = ticode.hl()->call.args;
|
||||
target_stackframe = &tproc->args;
|
||||
lhs = dynamic_cast<AstIdent *>(picode->hl()->asgn.lhs());
|
||||
lhs = dynamic_cast<AstIdent *>(picode.hl()->asgn.lhs());
|
||||
RegisterNode *lhs_reg = dynamic_cast<RegisterNode *>(lhs);
|
||||
assert(lhs);
|
||||
type = lhs->ident.type();
|
||||
if(type==REGISTER)
|
||||
assert(lhs_reg);
|
||||
if(type==LONG_VAR)
|
||||
assert(!lhs_reg);
|
||||
assert(nullptr==lhs_reg);
|
||||
if (lhs_reg)
|
||||
{
|
||||
regL = id_arr[lhs_reg->regiIdx].id.regi;
|
||||
@ -176,7 +179,7 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
|
||||
newsym.type = TYPE_LONG_SIGN;
|
||||
assert(regL!=rUNDEF);
|
||||
tproc->localId.id_arr[tidx].name = newsym.name;
|
||||
tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name.c_str());
|
||||
tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name);
|
||||
}
|
||||
target_stackframe->push_back(newsym);
|
||||
target_stackframe->numArgs++;
|
||||
@ -185,13 +188,13 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
|
||||
/* Do ps (actual arguments) */
|
||||
STKSYM newsym;
|
||||
newsym.setArgName(call_args_stackframe->size());
|
||||
newsym.actual = picode->hl()->asgn.rhs;
|
||||
newsym.actual = picode.hl()->asgn.m_rhs;
|
||||
newsym.regs = lhs;
|
||||
/* Mask off high and low register(s) in picode */
|
||||
switch (type) {
|
||||
case REGISTER:
|
||||
id = &id_arr[lhs_reg->regiIdx];
|
||||
picode->du.def.clrReg(id->id.regi);
|
||||
picode.du.def.clrReg(id->id.regi);
|
||||
if (id->id.regi < rAL)
|
||||
newsym.type = TYPE_WORD_SIGN;
|
||||
else
|
||||
@ -199,8 +202,8 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
|
||||
break;
|
||||
case LONG_VAR:
|
||||
id = &id_arr[lhs->ident.idNode.longIdx];
|
||||
picode->du.def.clrReg(id->longId().h());
|
||||
picode->du.def.clrReg(id->longId().l());
|
||||
picode.du.def.clrReg(id->longId().h());
|
||||
picode.du.def.clrReg(id->longId().l());
|
||||
newsym.type = TYPE_LONG_SIGN;
|
||||
break;
|
||||
default:
|
||||
@ -227,7 +230,7 @@ bool CallType::newStkArg(Expr *exp, llIcode opcode, Function * pproc)
|
||||
if (expr)
|
||||
{
|
||||
regi = pproc->localId.id_arr[expr->regiIdx].id.regi;
|
||||
if ((regi >= rES) && (regi <= rDS))
|
||||
if ((regi >= rES) and (regi <= rDS))
|
||||
{
|
||||
return (opcode == iCALLF) ? false : true;
|
||||
}
|
||||
@ -365,8 +368,8 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
|
||||
|
||||
case TYPE_LONG_UNSIGN:
|
||||
case TYPE_LONG_SIGN:
|
||||
if ((forType == TYPE_WORD_UNSIGN) ||
|
||||
(forType == TYPE_WORD_SIGN) ||
|
||||
if ((forType == TYPE_WORD_UNSIGN) or
|
||||
(forType == TYPE_WORD_SIGN) or
|
||||
(forType == TYPE_UNKNOWN))
|
||||
{
|
||||
/* Merge low and high */
|
||||
|
||||
@ -5,17 +5,15 @@
|
||||
#include "CallGraph.h"
|
||||
#include "project.h"
|
||||
#include "Procedure.h"
|
||||
|
||||
using namespace std;
|
||||
//Project g_proj;
|
||||
|
||||
QString asm1_name, asm2_name; /* Assembler output filenames */
|
||||
SYMTAB symtab; /* Global symbol table */
|
||||
STATS stats; /* cfg statistics */
|
||||
//PROG prog; /* programs fields */
|
||||
OPTION option; /* Command line options */
|
||||
Project *Project::s_instance = nullptr;
|
||||
Project::Project() : callGraph(nullptr)
|
||||
{
|
||||
memset(&prog,0,sizeof(prog));
|
||||
}
|
||||
void Project::initialize()
|
||||
{
|
||||
@ -54,13 +52,13 @@ ilFunction Project::findByEntry(uint32_t entry)
|
||||
return iter;
|
||||
}
|
||||
|
||||
ilFunction Project::createFunction(FunctionType *f,const std::string &name)
|
||||
ilFunction Project::createFunction(FunctionType *f,const QString &name)
|
||||
{
|
||||
pProcList.push_back(Function::Create(f,0,name,0));
|
||||
pProcList.push_back(*Function::Create(f,0,name,0));
|
||||
return (++pProcList.rbegin()).base();
|
||||
}
|
||||
|
||||
int Project::getSymIdxByAdd(uint32_t adr)
|
||||
int Project::getSymIdxByAddr(uint32_t adr)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < symtab.size(); i++)
|
||||
@ -87,7 +85,7 @@ hlType Project::symbolType(size_t idx)
|
||||
return symtab[idx].type;
|
||||
}
|
||||
|
||||
const std::string &Project::symbolName(size_t idx)
|
||||
const QString &Project::symbolName(size_t idx)
|
||||
{
|
||||
assert(validSymIdx(idx));
|
||||
return symtab[idx].name;
|
||||
@ -99,8 +97,6 @@ Project *Project::get()
|
||||
s_instance=new Project;
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
|
||||
SourceMachine *Project::machine()
|
||||
{
|
||||
return nullptr;
|
||||
|
||||
@ -4,18 +4,20 @@
|
||||
* registers) along the graph. Structure the graph in this way.
|
||||
* (C) Cristina Cifuentes
|
||||
**************************************************************************/
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <memory.h>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
|
||||
#include "dcc.h"
|
||||
|
||||
/* Returns whether the given icode opcode is within the range of valid
|
||||
* high-level conditional jump icodes (iJB..iJG) */
|
||||
static bool isJCond (llIcode opcode)
|
||||
{
|
||||
if ((opcode >= iJB) && (opcode <= iJG))
|
||||
if ((opcode >= iJB) and (opcode <= iJG))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@ -32,10 +34,10 @@ static bool isLong23 (BB * pbb, iICODE &off, int *arc)
|
||||
e = pbb->edges[ELSE].BBptr;
|
||||
|
||||
/* Check along the THEN path */
|
||||
if ((t->size() == 1) && (t->nodeType == TWO_BRANCH) && (t->inEdges.size() == 1))
|
||||
if ((t->size() == 1) and (t->nodeType == TWO_BRANCH) and (t->inEdges.size() == 1))
|
||||
{
|
||||
obb2 = t->edges[THEN].BBptr;
|
||||
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
|
||||
if ((obb2->size() == 2) and (obb2->nodeType == TWO_BRANCH) and (obb2->front().ll()->getOpcode() == iCMP))
|
||||
{
|
||||
off = obb2->begin();//std::distance(iter,obb2->begin2());
|
||||
*arc = THEN;
|
||||
@ -44,10 +46,10 @@ static bool isLong23 (BB * pbb, iICODE &off, int *arc)
|
||||
}
|
||||
|
||||
/* Check along the ELSE path */
|
||||
else if ((e->size() == 1) && (e->nodeType == TWO_BRANCH) && (e->inEdges.size() == 1))
|
||||
else if ((e->size() == 1) and (e->nodeType == TWO_BRANCH) and (e->inEdges.size() == 1))
|
||||
{
|
||||
obb2 = e->edges[THEN].BBptr;
|
||||
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
|
||||
if ((obb2->size() == 2) and (obb2->nodeType == TWO_BRANCH) and (obb2->front().ll()->getOpcode() == iCMP))
|
||||
{
|
||||
off = obb2->begin();//std::distance(iter,obb2->begin2());//obb2->front().loc_ip - i;
|
||||
*arc = ELSE;
|
||||
@ -66,9 +68,9 @@ static bool isLong22 (iICODE pIcode, iICODE pEnd, iICODE &off)
|
||||
return false;
|
||||
// preincrement because pIcode is not checked here
|
||||
iICODE icodes[] = { ++pIcode,++pIcode,++pIcode };
|
||||
if ( icodes[1]->ll()->match(iCMP) &&
|
||||
(isJCond ((llIcode)icodes[0]->ll()->getOpcode())) &&
|
||||
(isJCond ((llIcode)icodes[2]->ll()->getOpcode())))
|
||||
if ( icodes[1]->ll()->match(iCMP) and
|
||||
(isJCond (icodes[0]->ll()->getOpcode())) and
|
||||
(isJCond (icodes[2]->ll()->getOpcode())))
|
||||
{
|
||||
off = initial_icode;
|
||||
advance(off,2);
|
||||
@ -101,7 +103,7 @@ static int longJCond23 (Assignment &asgn, iICODE pIcode, int arc, iICODE atOffse
|
||||
/* Modify in edges of target basic block */
|
||||
auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
|
||||
[obb1,obb2](BB *b) -> bool {
|
||||
return (b==obb1) || (b==obb2); });
|
||||
return (b==obb1) or (b==obb2); });
|
||||
tbb->inEdges.erase(newlast,tbb->inEdges.end());
|
||||
tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
|
||||
|
||||
@ -129,7 +131,7 @@ static int longJCond23 (Assignment &asgn, iICODE pIcode, int arc, iICODE atOffse
|
||||
/* Modify in edges of the ELSE basic block */
|
||||
tbb = obb2->edges[ELSE].BBptr;
|
||||
auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
|
||||
[obb1,obb2](BB *b) -> bool { return (b==obb1) || (b==obb2); });
|
||||
[obb1,obb2](BB *b) -> bool { return (b==obb1) or (b==obb2); });
|
||||
tbb->inEdges.erase(newlast,tbb->inEdges.end());
|
||||
tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
|
||||
|
||||
@ -246,7 +248,7 @@ void Function::propLongStk (int i, const ID &pLocId)
|
||||
next1 = ++iICODE(pIcode);
|
||||
if(next1==pEnd)
|
||||
break;
|
||||
if ((pIcode->type == HIGH_LEVEL) || ( not pIcode->valid() ))
|
||||
if ((pIcode->type == HIGH_LEVEL_ICODE) or ( not pIcode->valid() ))
|
||||
continue;
|
||||
if (pIcode->ll()->getOpcode() == next1->ll()->getOpcode())
|
||||
{
|
||||
@ -288,7 +290,7 @@ void Function::propLongStk (int i, const ID &pLocId)
|
||||
}
|
||||
//TODO: Simplify this!
|
||||
/* Check long conditional (i.e. 2 CMPs and 3 branches */
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), l23, &arc)))
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) and (isLong23 (pIcode->getParent(), l23, &arc)))
|
||||
{
|
||||
if ( checkLongEq (pLocId.longStkId(), pIcode, i, this, asgn, *l23->ll()) )
|
||||
{
|
||||
@ -298,7 +300,7 @@ void Function::propLongStk (int i, const ID &pLocId)
|
||||
|
||||
/* Check for long conditional equality or inequality. This requires
|
||||
* 2 CMPs and 2 branches */
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) && isLong22 (pIcode, pEnd, l23))
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) and isLong22 (pIcode, pEnd, l23))
|
||||
{
|
||||
if ( checkLongEq (pLocId.longStkId(), pIcode, i, this,asgn, *l23->ll()) )
|
||||
{
|
||||
@ -321,7 +323,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
ICODE &icode(*pIcode);
|
||||
|
||||
|
||||
if ((icode.type == HIGH_LEVEL) || ( not icode.valid() ))
|
||||
if ((icode.type == HIGH_LEVEL_ICODE) or ( not icode.valid() ))
|
||||
continue;
|
||||
if (icode.ll()->getOpcode() != next1->ll()->getOpcode())
|
||||
continue;
|
||||
@ -331,7 +333,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
case iMOV:
|
||||
pmH = &icode.ll()->m_dst;
|
||||
pmL = &next1->ll()->m_dst;
|
||||
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
|
||||
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
|
||||
{
|
||||
localId.id_arr[loc_ident_idx].idx.push_back(pIcode);//idx-1//insert
|
||||
icode.setRegDU( pmL->regi, eDEF);
|
||||
@ -346,7 +348,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
case iPOP:
|
||||
pmH = &next1->ll()->m_dst;
|
||||
pmL = &icode.ll()->m_dst;
|
||||
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
|
||||
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
|
||||
{
|
||||
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
|
||||
icode.setRegDU( pmH->regi, eDEF);
|
||||
@ -362,7 +364,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
case iAND: case iOR: case iXOR:
|
||||
pmL = &icode.ll()->m_dst;
|
||||
pmH = &next1->ll()->m_dst;
|
||||
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
|
||||
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
|
||||
{
|
||||
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
|
||||
asgn.rhs = AstIdent::Long (&this->localId, SRC, pIcode, LOW_FIRST, pIcode, eUSE, *next1->ll());
|
||||
@ -399,7 +401,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
LLOperand * pmH,* pmL; /* Pointers to dst LOW_LEVEL icodes */
|
||||
int arc;
|
||||
|
||||
if ((pIcode->type == HIGH_LEVEL) || ( not pIcode->valid() ))
|
||||
if ((pIcode->type == HIGH_LEVEL_ICODE) or ( not pIcode->valid() ))
|
||||
continue;
|
||||
|
||||
if (pIcode->ll()->getOpcode() == next1->ll()->getOpcode())
|
||||
@ -412,7 +414,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
const LLOperand &src_op2(next1->ll()->src());
|
||||
eReg srcReg1=src_op1.getReg2();
|
||||
eReg nextReg2=src_op2.getReg2();
|
||||
if ((ref_long.h() == srcReg1) && (ref_long.l() == nextReg2))
|
||||
if ((ref_long.h() == srcReg1) and (ref_long.l() == nextReg2))
|
||||
{
|
||||
pIcode->setRegDU( nextReg2, eUSE);
|
||||
|
||||
@ -431,7 +433,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
const LONGID_TYPE &ref_long(pLocId.longId());
|
||||
const LLOperand &src_op1(pIcode->ll()->src());
|
||||
const LLOperand &src_op2(next1->ll()->src());
|
||||
if ((ref_long.h() == src_op1.getReg2()) && (ref_long.l() == src_op2.getReg2()))
|
||||
if ((ref_long.h() == src_op1.getReg2()) and (ref_long.l() == src_op2.getReg2()))
|
||||
{
|
||||
asgn.rhs = AstIdent::LongIdx (loc_ident_idx);
|
||||
pIcode->setRegDU( next1->ll()->src().getReg2(), eUSE);
|
||||
@ -447,7 +449,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
case iAND: case iOR: case iXOR:
|
||||
pmL = &pIcode->ll()->m_dst;
|
||||
pmH = &next1->ll()->m_dst;
|
||||
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
|
||||
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
|
||||
{
|
||||
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
|
||||
pIcode->setRegDU( pmH->regi, USE_DEF);
|
||||
@ -474,7 +476,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
} /* eos */
|
||||
|
||||
/* Check long conditional (i.e. 2 CMPs and 3 branches */
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), long_loc, &arc)))
|
||||
else if ((pIcode->ll()->getOpcode() == iCMP) and (isLong23 (pIcode->getParent(), long_loc, &arc)))
|
||||
{
|
||||
if (checkLongRegEq (pLocId.longId(), pIcode, loc_ident_idx, this, asgn, *long_loc->ll()))
|
||||
{
|
||||
@ -485,7 +487,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
|
||||
/* Check for long conditional equality or inequality. This requires
|
||||
* 2 CMPs and 2 branches */
|
||||
else if (pIcode->ll()->match(iCMP) && (isLong22 (pIcode, pEnd, long_loc)))
|
||||
else if (pIcode->ll()->match(iCMP) and (isLong22 (pIcode, pEnd, long_loc)))
|
||||
{
|
||||
if (checkLongRegEq (pLocId.longId(), pIcode, loc_ident_idx, this, asgn, *long_loc->ll()) )
|
||||
{
|
||||
@ -498,7 +500,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
|
||||
* JX lab
|
||||
* => HLI_JCOND (regH:regL X 0) lab
|
||||
* This is better code than HLI_JCOND (HI(regH:regL) | LO(regH:regL)) */
|
||||
else if (pIcode->ll()->match(iOR) && (next1 != pEnd) && (isJCond ((llIcode)next1->ll()->getOpcode())))
|
||||
else if (pIcode->ll()->match(iOR) and (next1 != pEnd) and (isJCond (next1->ll()->getOpcode())))
|
||||
{
|
||||
if (pLocId.longId().srcDstRegMatch(pIcode,pIcode))
|
||||
{
|
||||
@ -546,7 +548,7 @@ void Function::propLongReg (int loc_ident_idx, const ID &pLocId)
|
||||
* Transforms some LOW_LEVEL icodes into HIGH_LEVEL */
|
||||
void Function::propLongGlb (int /*i*/, const ID &/*pLocId*/)
|
||||
{
|
||||
printf("WARN: Function::propLongGlb not implemented");
|
||||
printf("WARN: Function::propLongGlb not implemented\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,12 +3,14 @@
|
||||
* constructs an equivalent reducible graph if one is not found.
|
||||
* (C) Cristina Cifuentes
|
||||
********************************************************************/
|
||||
#include "dcc.h"
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <stdint.h>
|
||||
#include "dcc.h"
|
||||
|
||||
static int numInt; /* Number of intervals */
|
||||
|
||||
@ -26,7 +28,7 @@ bool trivialGraph(BB *G)
|
||||
* from the list. Q is not an empty queue. */
|
||||
static BB *firstOfQueue (queue &Q)
|
||||
{
|
||||
assert(!Q.empty());
|
||||
assert(not Q.empty());
|
||||
BB *res=Q.front();
|
||||
Q.pop_front();
|
||||
return res;
|
||||
@ -79,7 +81,7 @@ void interval::appendNodeInt(queue &pqH, BB *node)
|
||||
|
||||
/* Check header list for occurrence of node, if found, remove it
|
||||
* and decrement number of out-edges from this interval. */
|
||||
if (node->beenOnH && !pqH.empty())
|
||||
if (node->beenOnH and not pqH.empty())
|
||||
{
|
||||
auto found_iter=std::find(pqH.begin(),pqH.end(),node);
|
||||
if(found_iter!=pqH.end())
|
||||
@ -111,7 +113,7 @@ void derSeq_Entry::findIntervals (Function *c)
|
||||
Gi->reachingInt = BB::Create(nullptr,"",c); /* ^ empty BB */
|
||||
|
||||
/* Process header nodes list H */
|
||||
while (!H.empty())
|
||||
while (not H.empty())
|
||||
{
|
||||
header = firstOfQueue (H);
|
||||
pI = new interval;
|
||||
@ -137,7 +139,7 @@ void derSeq_Entry::findIntervals (Function *c)
|
||||
succ->reachingInt = header;
|
||||
if (succ->inEdgeCount == 0)
|
||||
pI->appendNodeInt (H, succ);
|
||||
else if (! succ->beenOnH) /* out edge */
|
||||
else if (not succ->beenOnH) /* out edge */
|
||||
{
|
||||
appendQueue (H, succ);
|
||||
succ->beenOnH = true;
|
||||
@ -147,7 +149,7 @@ void derSeq_Entry::findIntervals (Function *c)
|
||||
else /* node has been visited before */
|
||||
if (succ->inEdgeCount == 0)
|
||||
{
|
||||
if (succ->reachingInt == header || succ->inInterval == pI) /* same interval */
|
||||
if (succ->reachingInt == header or succ->inInterval == pI) /* same interval */
|
||||
{
|
||||
if (succ != header)
|
||||
pI->appendNodeInt (H, succ);
|
||||
@ -155,13 +157,13 @@ void derSeq_Entry::findIntervals (Function *c)
|
||||
else /* out edge */
|
||||
pI->numOutEdges++;
|
||||
}
|
||||
else if (succ != header && succ->beenOnH)
|
||||
else if (succ != header and succ->beenOnH)
|
||||
pI->numOutEdges++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Link interval I to list of intervals */
|
||||
if (! first)
|
||||
if (not first)
|
||||
{
|
||||
m_intervals.push_back(pI);
|
||||
J->next = pI;
|
||||
@ -230,7 +232,7 @@ void freeDerivedSeq(derSeq &derivedG)
|
||||
derSeq_Entry::~derSeq_Entry()
|
||||
{
|
||||
freeInterval (&Ii);
|
||||
// if(Gi && Gi->nodeType == INTERVAL_NODE)
|
||||
// if(Gi and Gi->nodeType == INTERVAL_NODE)
|
||||
// freeCFG (Gi);
|
||||
}
|
||||
|
||||
@ -259,7 +261,7 @@ bool Function::nextOrderGraph (derSeq &derivedGi)
|
||||
const queue &listIi(Ii->nodes);
|
||||
|
||||
/* Check for more than 1 interval */
|
||||
if (sameGraph && (listIi.size()>1))
|
||||
if (sameGraph and (listIi.size()>1))
|
||||
sameGraph = false;
|
||||
|
||||
/* Find out edges */
|
||||
@ -309,20 +311,20 @@ bool Function::findDerivedSeq (derSeq &derivedGi)
|
||||
derSeq::iterator iter=derivedGi.begin();
|
||||
assert(iter!=derivedGi.end());
|
||||
BB *Gi = iter->Gi; /* Current derived sequence graph */
|
||||
while (! trivialGraph (Gi))
|
||||
while (not trivialGraph (Gi))
|
||||
{
|
||||
/* Find the intervals of Gi and place them in derivedGi->Ii */
|
||||
iter->findIntervals(this);
|
||||
|
||||
/* Create Gi+1 and check if it is equivalent to Gi */
|
||||
if (! nextOrderGraph (derivedGi))
|
||||
if (not nextOrderGraph (derivedGi))
|
||||
break;
|
||||
++iter;
|
||||
Gi = iter->Gi;
|
||||
stats.nOrder++;
|
||||
}
|
||||
|
||||
if (! trivialGraph (Gi))
|
||||
if (not trivialGraph (Gi))
|
||||
{
|
||||
++iter;
|
||||
derivedGi.erase(iter,derivedGi.end()); /* remove Gi+1 */
|
||||
@ -366,7 +368,7 @@ derSeq * Function::checkReducibility()
|
||||
der_seq->back().Gi = *m_actual_cfg.begin(); /*m_cfg.front()*/;
|
||||
reducible = findDerivedSeq(*der_seq);
|
||||
|
||||
if (! reducible)
|
||||
if (not reducible)
|
||||
{
|
||||
flg |= GRAPH_IRRED;
|
||||
m_actual_cfg.nodeSplitting();
|
||||
|
||||
123
src/scanner.cpp
123
src/scanner.cpp
@ -4,19 +4,21 @@
|
||||
* I-code
|
||||
* (C) Cristina Cifuentes, Jeff Ledermann
|
||||
****************************************************************************/
|
||||
#include "scanner.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
#include "dcc.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "dcc.h"
|
||||
#include "scanner.h"
|
||||
#include "project.h"
|
||||
|
||||
/* Parser flags */
|
||||
#define TO_REG 0x000100 /* rm is source */
|
||||
#define S_EXT 0x000200 /* sign extend */
|
||||
#define OP386 0x000400 /* 386 op-code */
|
||||
#define NSP 0x000800 /* NOT_HLL if SP is src or dst */
|
||||
// defined in Enums.h #define ICODEMASK 0xFF00FF /* Masks off parser flags */
|
||||
|
||||
static void rm(int i);
|
||||
static void modrm(int i);
|
||||
@ -48,15 +50,15 @@ static void none1(int i);
|
||||
static void none2(int i);
|
||||
static void checkInt(int i);
|
||||
|
||||
#define iZERO (llIcode)0 // For neatness
|
||||
#define IC llIcode
|
||||
|
||||
static struct {
|
||||
struct StateTabelEntry {
|
||||
void (*state1)(int);
|
||||
void (*state2)(int);
|
||||
uint32_t flg;
|
||||
llIcode opcode;
|
||||
} stateTable[] = {
|
||||
};
|
||||
static const StateTabelEntry stateTable[] = {
|
||||
{ modrm, none2, B , iADD }, /* 00 */
|
||||
{ modrm, none2, 0 , iADD }, /* 01 */
|
||||
{ modrm, none2, TO_REG | B , iADD }, /* 02 */
|
||||
@ -72,7 +74,7 @@ static struct {
|
||||
{ data1, axImp, B , iOR }, /* 0C */
|
||||
{ data2, axImp, 0 , iOR }, /* 0D */
|
||||
{ segop, none2, NO_SRC , iPUSH }, /* 0E */
|
||||
{ none1, none2, OP386 , iZERO }, /* 0F */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 0F */
|
||||
{ modrm, none2, B , iADC }, /* 10 */
|
||||
{ modrm, none2, NSP , iADC }, /* 11 */
|
||||
{ modrm, none2, TO_REG | B , iADC }, /* 12 */
|
||||
@ -156,11 +158,11 @@ static struct {
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iPUSHA}, /* 60 */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iPOPA }, /* 61 */
|
||||
{ memOnly, modrm, TO_REG | NSP , iBOUND}, /* 62 */
|
||||
{ none1, none2, OP386 , iZERO }, /* 63 */
|
||||
{ none1, none2, OP386 , iZERO }, /* 64 */
|
||||
{ none1, none2, OP386 , iZERO }, /* 65 */
|
||||
{ none1, none2, OP386 , iZERO }, /* 66 */
|
||||
{ none1, none2, OP386 , iZERO }, /* 67 */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 63 */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 64 */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 65 */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 66 */
|
||||
{ none1, none2, OP386 , iINVALID }, /* 67 */
|
||||
{ data2, none2, NO_SRC , iPUSH }, /* 68 */
|
||||
{ modrm, data2, TO_REG | NSP , iIMUL }, /* 69 */
|
||||
{ data1, none2, S_EXT | NO_SRC , iPUSH }, /* 6A */
|
||||
@ -185,10 +187,10 @@ static struct {
|
||||
{ dispS, none2, 0 , iJGE }, /* 7D */
|
||||
{ dispS, none2, 0 , iJLE }, /* 7E */
|
||||
{ dispS, none2, 0 , iJG }, /* 7F */
|
||||
{ immed, data1, B , iZERO }, /* 80 */
|
||||
{ immed, data2, NSP , iZERO }, /* 81 */
|
||||
{ immed, data1, B , iZERO }, /* 82 */ /* ?? */
|
||||
{ immed, data1, NSP | S_EXT , iZERO }, /* 83 */
|
||||
{ immed, data1, B , iINVALID }, /* 80 */
|
||||
{ immed, data2, NSP , iINVALID }, /* 81 */
|
||||
{ immed, data1, B , iINVALID }, /* 82 */ /* ?? */
|
||||
{ immed, data1, NSP | S_EXT , iINVALID }, /* 83 */
|
||||
{ modrm, none2, TO_REG | B , iTEST }, /* 84 */
|
||||
{ modrm, none2, TO_REG | NSP , iTEST }, /* 85 */
|
||||
{ modrm, none2, TO_REG | B , iXCHG }, /* 86 */
|
||||
@ -211,7 +213,7 @@ static struct {
|
||||
{ regop, axImp, 0 , iXCHG }, /* 97 */
|
||||
{ alImp, axImp, SRC_B | S_EXT , iSIGNEX}, /* 98 */
|
||||
{axSrcIm, axImp, IM_DST | S_EXT , iSIGNEX}, /* 99 */
|
||||
{ dispF, none2, 0 , iCALLF }, /* 9A */
|
||||
{ dispF, none2, TO_REG , iCALLF }, /* 9A */ // TO_REG set to use SRC when processing setAddress
|
||||
{ none1, none2, FLOAT_OP| NO_OPS , iWAIT }, /* 9B */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iPUSHF}, /* 9C */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iPOPF }, /* 9D */
|
||||
@ -249,8 +251,8 @@ static struct {
|
||||
{ regop, data2, 0 , iMOV }, /* BD */
|
||||
{ regop, data2, 0 , iMOV }, /* BE */
|
||||
{ regop, data2, 0 , iMOV }, /* BF */
|
||||
{ shift, data1, B , iZERO }, /* C0 */
|
||||
{ shift, data1, NSP | SRC_B , iZERO }, /* C1 */
|
||||
{ shift, data1, B , iINVALID }, /* C0 */
|
||||
{ shift, data1, NSP | SRC_B , iINVALID }, /* C1 */
|
||||
{ data2, none2, 0 , iRET }, /* C2 */
|
||||
{ none1, none2, NO_OPS , iRET }, /* C3 */
|
||||
{ memOnly, modrm, TO_REG | NSP , iLES }, /* C4 */
|
||||
@ -265,13 +267,13 @@ static struct {
|
||||
{ data1,checkInt, NOT_HLL , iINT }, /* CD */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iINTO }, /* CE */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iIRET }, /* Cf */
|
||||
{ shift, const1, B , iZERO }, /* D0 */
|
||||
{ shift, const1, SRC_B , iZERO }, /* D1 */
|
||||
{ shift, none1, B , iZERO }, /* D2 */
|
||||
{ shift, none1, SRC_B , iZERO }, /* D3 */
|
||||
{ shift, const1, B , iINVALID }, /* D0 */
|
||||
{ shift, const1, SRC_B , iINVALID }, /* D1 */
|
||||
{ shift, none1, B , iINVALID }, /* D2 */
|
||||
{ shift, none1, SRC_B , iINVALID }, /* D3 */
|
||||
{ data1, axImp, NOT_HLL , iAAM }, /* D4 */
|
||||
{ data1, axImp, NOT_HLL , iAAD }, /* D5 */
|
||||
{ none1, none2, 0 , iZERO }, /* D6 */
|
||||
{ none1, none2, 0 , iINVALID }, /* D6 */
|
||||
{ memImp, axImp, NOT_HLL | B| IM_OPS , iXLAT }, /* D7 */
|
||||
{ escop, none2, FLOAT_OP , iESC }, /* D8 */
|
||||
{ escop, none2, FLOAT_OP , iESC }, /* D9 */
|
||||
@ -298,21 +300,21 @@ static struct {
|
||||
{ none1, axImp, NOT_HLL | B|NO_SRC , iOUT }, /* EE */
|
||||
{ none1, axImp, NOT_HLL | NO_SRC , iOUT }, /* EF */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iLOCK }, /* F0 */
|
||||
{ none1, none2, 0 , iZERO }, /* F1 */
|
||||
{ none1, none2, 0 , iINVALID }, /* F1 */
|
||||
{ prefix, none2, 0 , iREPNE}, /* F2 */
|
||||
{ prefix, none2, 0 , iREPE }, /* F3 */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iHLT }, /* F4 */
|
||||
{ none1, none2, NO_OPS , iCMC }, /* F5 */
|
||||
{ arith, none1, B , iZERO }, /* F6 */
|
||||
{ arith, none1, NSP , iZERO }, /* F7 */
|
||||
{ arith, none1, B , iINVALID }, /* F6 */
|
||||
{ arith, none1, NSP , iINVALID }, /* F7 */
|
||||
{ none1, none2, NO_OPS , iCLC }, /* F8 */
|
||||
{ none1, none2, NO_OPS , iSTC }, /* F9 */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iCLI }, /* FA */
|
||||
{ none1, none2, NOT_HLL | NO_OPS , iSTI }, /* FB */
|
||||
{ none1, none2, NO_OPS , iCLD }, /* FC */
|
||||
{ none1, none2, NO_OPS , iSTD }, /* FD */
|
||||
{ trans, none1, B , iZERO }, /* FE */
|
||||
{ trans, none1, NSP , iZERO } /* FF */
|
||||
{ trans, none1, B , iINVALID }, /* FE */
|
||||
{ trans, none1, NSP , iINVALID } /* FF */
|
||||
} ;
|
||||
|
||||
static uint16_t SegPrefix, RepPrefix;
|
||||
@ -330,7 +332,7 @@ static void decodeBranchTgt(x86_insn_t &insn)
|
||||
int32_t addr = tgt_op->getAddress();
|
||||
if(tgt_op->is_relative())
|
||||
{
|
||||
addr += insn.addr+insn.size;
|
||||
addr = (uint16_t)(addr + insn.addr + insn.size);
|
||||
}
|
||||
pIcode->ll()->replaceSrc((uint32_t)addr);
|
||||
pIcode->ll()->setFlags(I);
|
||||
@ -380,7 +382,7 @@ static void fixFloatEmulation(x86_insn_t &insn)
|
||||
return;
|
||||
PROG &prog(Project::get()->prog);
|
||||
uint16_t wOp=insn.x86_get_imm()->data.word;
|
||||
if ((wOp < 0x34) || (wOp > 0x3B))
|
||||
if ((wOp < 0x34) or (wOp > 0x3B))
|
||||
return;
|
||||
uint8_t buf[16];
|
||||
/* This is a Borland/Microsoft floating point emulation instruction. Treat as if it is an ESC opcode */
|
||||
@ -403,7 +405,7 @@ int disassembleOneLibDisasm(uint32_t ip,x86_insn_t &l)
|
||||
PROG &prog(Project::get()->prog);
|
||||
X86_Disasm ds(opt_16_bit);
|
||||
int cnt=ds.x86_disasm(prog.image(),prog.cbImage,0,ip,&l);
|
||||
if(cnt && l.is_valid())
|
||||
if(cnt and l.is_valid())
|
||||
{
|
||||
fixFloatEmulation(l); //can change 'l'
|
||||
}
|
||||
@ -413,7 +415,7 @@ int disassembleOneLibDisasm(uint32_t ip,x86_insn_t &l)
|
||||
}
|
||||
eReg convertRegister(const x86_reg_t ®)
|
||||
{
|
||||
if( (reg_pc==reg.type) || (0==reg.id))
|
||||
if( (reg_pc==reg.type) or (0==reg.id))
|
||||
return rUNDEF;
|
||||
|
||||
eReg regmap[]={ rUNDEF,
|
||||
@ -505,7 +507,7 @@ LLOperand convertOperand(const x86_op_t &from)
|
||||
case op_register:
|
||||
res.regi = convertRegister(from.data.reg); break;
|
||||
case op_immediate:
|
||||
res.opz = from.data.sdword;
|
||||
res.opz = from.data.sdword; break;
|
||||
case op_expression:
|
||||
res = convertExpression(from.data.expression); break;
|
||||
case op_offset:
|
||||
@ -518,7 +520,7 @@ LLOperand convertOperand(const x86_op_t &from)
|
||||
default:
|
||||
fprintf(stderr,"convertOperand does not know how to convert %d\n",from.type);
|
||||
}
|
||||
if(res.isSet() && (res.seg == rUNDEF))
|
||||
if(res.isSet() and (res.seg == rUNDEF))
|
||||
{
|
||||
res.seg = rDS;
|
||||
}
|
||||
@ -534,7 +536,7 @@ eErrorId scan(uint32_t ip, ICODE &p)
|
||||
PROG &prog(Project::get()->prog);
|
||||
int op;
|
||||
p = ICODE();
|
||||
p.type = LOW_LEVEL;
|
||||
p.type = LOW_LEVEL_ICODE;
|
||||
p.ll()->label = ip; /* ip is absolute offset into image*/
|
||||
if (ip >= (uint32_t)prog.cbImage)
|
||||
{
|
||||
@ -568,7 +570,7 @@ eErrorId scan(uint32_t ip, ICODE &p)
|
||||
}
|
||||
// LLOperand conv = convertOperand(*p.insn.get_dest());
|
||||
// assert(conv==p.ll()->dst);
|
||||
if (p.ll()->getOpcode())
|
||||
if (p.ll()->getOpcode()!=iINVALID)
|
||||
{
|
||||
/* Save bytes of image used */
|
||||
p.ll()->numBytes = (uint8_t)((pInst - prog.image()) - ip);
|
||||
@ -638,7 +640,7 @@ static void setAddress(int i, bool fdst, uint16_t seg, int16_t reg, uint16_t off
|
||||
}
|
||||
else
|
||||
{ /* no override, check indexed register */
|
||||
if ((reg >= INDEX_BX_SI) && (reg == INDEX_BP_SI || reg == INDEX_BP_DI || reg == INDEX_BP))
|
||||
if ((reg >= INDEX_BX_SI) and (reg == INDEX_BP_SI or reg == INDEX_BP_DI or reg == INDEX_BP))
|
||||
{
|
||||
pm->seg = rSS; /* indexed on bp */
|
||||
}
|
||||
@ -650,7 +652,7 @@ static void setAddress(int i, bool fdst, uint16_t seg, int16_t reg, uint16_t off
|
||||
|
||||
pm->regi = (eReg)reg;
|
||||
pm->off = (int16_t)off;
|
||||
if (reg && reg < INDEX_BX_SI && (stateTable[i].flg & B))
|
||||
if (reg and (reg < INDEX_BX_SI) and (stateTable[i].flg & B))
|
||||
{
|
||||
pm->regi = Machine_X86::subRegL(pm->regi);
|
||||
}
|
||||
@ -694,7 +696,7 @@ static void rm(int i)
|
||||
break;
|
||||
}
|
||||
//pIcode->insn.get_dest()->
|
||||
if ((stateTable[i].flg & NSP) && (pIcode->ll()->src().getReg2()==rSP ||
|
||||
if ((stateTable[i].flg & NSP) and (pIcode->ll()->src().getReg2()==rSP or
|
||||
pIcode->ll()->m_dst.getReg2()==rSP))
|
||||
pIcode->ll()->setFlags(NOT_HLL);
|
||||
}
|
||||
@ -717,7 +719,7 @@ static void segrm(int i)
|
||||
{
|
||||
int reg = REG(*pInst) + rES;
|
||||
|
||||
if (reg > rDS || (reg == rCS && (stateTable[i].flg & TO_REG)))
|
||||
if (reg > rDS or (reg == rCS and (stateTable[i].flg & TO_REG)))
|
||||
pIcode->ll()->setOpcode((llIcode)0); // setCBW because it has that index
|
||||
else {
|
||||
setAddress(i, false, 0, (int16_t)reg, 0);
|
||||
@ -742,7 +744,7 @@ static void regop(int i)
|
||||
static void segop(int i)
|
||||
{
|
||||
if(i==0x1E) {
|
||||
printf("es");
|
||||
// printf("es");
|
||||
}
|
||||
setAddress(i, true, 0, (((int16_t)i & 0x18) >> 3) + rES, 0);
|
||||
}
|
||||
@ -784,7 +786,7 @@ static void memImp(int i)
|
||||
static void memOnly(int )
|
||||
{
|
||||
if ((*pInst & 0xC0) == 0xC0)
|
||||
pIcode->ll()->setOpcode((llIcode)0);
|
||||
pIcode->ll()->setOpcode(iINVALID);
|
||||
}
|
||||
|
||||
|
||||
@ -793,8 +795,8 @@ static void memOnly(int )
|
||||
****************************************************************************/
|
||||
static void memReg0(int i)
|
||||
{
|
||||
if (REG(*pInst) || (*pInst & 0xC0) == 0xC0)
|
||||
pIcode->ll()->setOpcode((llIcode)0);
|
||||
if (REG(*pInst) or (*pInst & 0xC0) == 0xC0)
|
||||
pIcode->ll()->setOpcode(iINVALID);
|
||||
else
|
||||
rm(i);
|
||||
}
|
||||
@ -810,7 +812,7 @@ static void immed(int i)
|
||||
pIcode->ll()->setOpcode(immedTable[REG(*pInst)]) ;
|
||||
rm(i);
|
||||
|
||||
if (pIcode->ll()->getOpcode() == iADD || pIcode->ll()->getOpcode() == iSUB)
|
||||
if (pIcode->ll()->getOpcode() == iADD or pIcode->ll()->getOpcode() == iSUB)
|
||||
pIcode->ll()->clrFlags(NOT_HLL); /* Allow ADD/SUB SP, immed */
|
||||
}
|
||||
|
||||
@ -838,20 +840,20 @@ static void trans(int i)
|
||||
{
|
||||
static llIcode transTable[8] =
|
||||
{
|
||||
(llIcode)iINC, iDEC, (llIcode)iCALL, (llIcode)iCALLF,
|
||||
(llIcode)iJMP, (llIcode)iJMPF,(llIcode)iPUSH, (llIcode)0
|
||||
iINC, iDEC, iCALL, iCALLF,
|
||||
iJMP, iJMPF,iPUSH, (llIcode)0
|
||||
};
|
||||
LLInst *ll = pIcode->ll();
|
||||
// if(transTable[REG(*pInst)]==iPUSH) {
|
||||
// printf("es");
|
||||
// }
|
||||
if ((uint8_t)REG(*pInst) < 2 || !(stateTable[i].flg & B)) { /* INC & DEC */
|
||||
if ((uint8_t)REG(*pInst) < 2 or not (stateTable[i].flg & B)) { /* INC & DEC */
|
||||
ll->setOpcode(transTable[REG(*pInst)]); /* valid on bytes */
|
||||
rm(i);
|
||||
ll->replaceSrc( pIcode->ll()->m_dst );
|
||||
if (ll->match(iJMP) || ll->match(iCALL) || ll->match(iCALLF))
|
||||
if (ll->match(iJMP) or ll->match(iCALL) or ll->match(iCALLF))
|
||||
ll->setFlags(NO_OPS);
|
||||
else if (ll->match(iINC) || ll->match(iPUSH) || ll->match(iDEC))
|
||||
else if (ll->match(iINC) or ll->match(iPUSH) or ll->match(iDEC))
|
||||
ll->setFlags(NO_SRC);
|
||||
}
|
||||
}
|
||||
@ -865,7 +867,7 @@ static void arith(int i)
|
||||
uint8_t opcode;
|
||||
static llIcode arithTable[8] =
|
||||
{
|
||||
iTEST, (llIcode)0, iNOT, iNEG,
|
||||
iTEST, iINVALID, iNOT, iNEG,
|
||||
iMUL , iIMUL, iDIV, iIDIV
|
||||
};
|
||||
opcode = arithTable[REG(*pInst)];
|
||||
@ -878,15 +880,15 @@ static void arith(int i)
|
||||
else
|
||||
data2(i);
|
||||
}
|
||||
else if (!(opcode == iNOT || opcode == iNEG))
|
||||
else if (not (opcode == iNOT or opcode == iNEG))
|
||||
{
|
||||
pIcode->ll()->replaceSrc( pIcode->ll()->m_dst );
|
||||
setAddress(i, true, 0, rAX, 0); /* dst = AX */
|
||||
}
|
||||
else if (opcode == iNEG || opcode == iNOT)
|
||||
else if (opcode == iNEG or opcode == iNOT)
|
||||
pIcode->ll()->setFlags(NO_SRC);
|
||||
|
||||
if ((opcode == iDIV) || (opcode == iIDIV))
|
||||
if ((opcode == iDIV) or (opcode == iIDIV))
|
||||
{
|
||||
if ( not pIcode->ll()->testFlags(B) )
|
||||
pIcode->ll()->setFlags(IM_TMP_DST);
|
||||
@ -970,6 +972,7 @@ static void dispF(int i)
|
||||
{
|
||||
uint16_t off = (unsigned)getWord();
|
||||
uint16_t seg = (unsigned)getWord();
|
||||
// FIXME: this is wrong since seg here is seg value, but setAddress treats it as register id
|
||||
setAddress(i, true, seg, 0, off);
|
||||
// decodeBranchTgt();
|
||||
}
|
||||
@ -981,7 +984,7 @@ static void dispF(int i)
|
||||
****************************************************************************/
|
||||
static void prefix(int )
|
||||
{
|
||||
if (pIcode->ll()->getOpcode() == iREPE || pIcode->ll()->getOpcode() == iREPNE)
|
||||
if ((pIcode->ll()->getOpcode() == iREPE) or (pIcode->ll()->getOpcode() == iREPNE))
|
||||
RepPrefix = pIcode->ll()->getOpcode();
|
||||
else
|
||||
SegPrefix = pIcode->ll()->getOpcode();
|
||||
@ -989,7 +992,7 @@ static void prefix(int )
|
||||
|
||||
inline void BumpOpcode(LLInst &ll)
|
||||
{
|
||||
llIcode ic((llIcode)ll.getOpcode());
|
||||
llIcode ic(ll.getOpcode());
|
||||
ic = (llIcode)(((int)ic)+1); // Bump this icode via the int type
|
||||
ll.setOpcode(ic);
|
||||
}
|
||||
@ -1001,7 +1004,7 @@ static void strop(int )
|
||||
{
|
||||
if (RepPrefix)
|
||||
{
|
||||
if ( pIcode->ll()->match(iCMPS) || pIcode->ll()->match(iSCAS) )
|
||||
if ( pIcode->ll()->match(iCMPS) or pIcode->ll()->match(iSCAS) )
|
||||
{
|
||||
if(pIcode->insn.prefix & insn_rep_zero)
|
||||
{
|
||||
@ -1075,7 +1078,7 @@ static void none2(int )
|
||||
static void checkInt(int )
|
||||
{
|
||||
uint16_t wOp = (uint16_t) pIcode->ll()->src().getImm2();
|
||||
if ((wOp >= 0x34) && (wOp <= 0x3B))
|
||||
if ((wOp >= 0x34) and (wOp <= 0x3B))
|
||||
{
|
||||
/* This is a Borland/Microsoft floating point emulation instruction.
|
||||
Treat as if it is an ESC opcode */
|
||||
|
||||
@ -133,7 +133,7 @@ void destroySymTables(void)
|
||||
}
|
||||
|
||||
/* Using the value, read the symbolic name */
|
||||
bool readVal(std::ostringstream &/*symName*/, uint32_t /*symOff*/, Function * /*symProc*/)
|
||||
bool readVal(QTextStream &/*symName*/, uint32_t /*symOff*/, Function * /*symProc*/)
|
||||
{
|
||||
return false; // no symbolic names for now
|
||||
}
|
||||
|
||||
38
src/udm.cpp
38
src/udm.cpp
@ -4,13 +4,15 @@
|
||||
* that just plays with abstract cfg's and intervals and such like.
|
||||
* (C) Cristina Cifuentes
|
||||
****************************************************************************/
|
||||
#include <list>
|
||||
#include <cassert>
|
||||
#include <stdio.h>
|
||||
#include "dcc.h"
|
||||
#include "disassem.h"
|
||||
#include "project.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <list>
|
||||
#include <cassert>
|
||||
#include <stdio.h>
|
||||
#include <CallGraph.h>
|
||||
extern Project g_proj;
|
||||
//static void displayCFG(Function * pProc);
|
||||
//static void displayDfs(BB * pBB);
|
||||
@ -60,7 +62,7 @@ void Function::controlFlowAnalysis()
|
||||
|
||||
if (option.verbose)
|
||||
{
|
||||
printf("\nDepth first traversal - Proc %s\n", name.c_str());
|
||||
qDebug() <<"\nDepth first traversal - Proc" <<name;
|
||||
(*m_actual_cfg.begin())->displayDfs();
|
||||
//m_cfg.front()->displayDfs();
|
||||
}
|
||||
@ -74,9 +76,16 @@ void udm(void)
|
||||
|
||||
/* Build the control flow graph, find idioms, and convert low-level
|
||||
* icodes to high-level ones */
|
||||
Project *proj = Project::get();
|
||||
Disassembler ds(2);
|
||||
for (auto iter = Project::get()->pProcList.rbegin(); iter!=Project::get()->pProcList.rend(); ++iter)
|
||||
for (auto iter = proj->pProcList.rbegin(); iter!=proj->pProcList.rend(); ++iter)
|
||||
{
|
||||
Function &f(*iter);
|
||||
if(option.CustomEntryPoint) {
|
||||
if(f.procEntry!=option.CustomEntryPoint) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
iter->buildCFG(ds);
|
||||
}
|
||||
if (option.asm2)
|
||||
@ -87,10 +96,23 @@ void udm(void)
|
||||
* and intermediate instructions. Find expressions by forward
|
||||
* substitution algorithm */
|
||||
LivenessSet live_regs;
|
||||
Project::get()->pProcList.front().dataFlow (live_regs);
|
||||
if(option.CustomEntryPoint) {
|
||||
ilFunction iter = proj->findByEntry(option.CustomEntryPoint);
|
||||
if(iter==proj->pProcList.end()) {
|
||||
qCritical()<< "No function found at entry point" << QString::number(option.CustomEntryPoint,16);
|
||||
return;
|
||||
}
|
||||
iter->dataFlow(live_regs);
|
||||
iter->controlFlowAnalysis();
|
||||
delete proj->callGraph;
|
||||
proj->callGraph = new CALL_GRAPH;
|
||||
proj->callGraph->proc = iter;
|
||||
return;
|
||||
}
|
||||
proj->pProcList.front().dataFlow (live_regs);
|
||||
|
||||
/* Control flow analysis - structuring algorithm */
|
||||
for (auto iter = Project::get()->pProcList.rbegin(); iter!=Project::get()->pProcList.rend(); ++iter)
|
||||
for (auto iter = proj->pProcList.rbegin(); iter!=proj->pProcList.rend(); ++iter)
|
||||
{
|
||||
iter->controlFlowAnalysis();
|
||||
}
|
||||
@ -101,7 +123,7 @@ void udm(void)
|
||||
***************************************************************************/
|
||||
void Function::displayCFG()
|
||||
{
|
||||
printf("\nBasic Block List - Proc %s", name.c_str());
|
||||
qDebug() << "\nBasic Block List - Proc"<<name;
|
||||
for (BB *pBB : /*m_cfg*/m_actual_cfg)
|
||||
{
|
||||
pBB->display();
|
||||
|
||||
@ -1 +1,4 @@
|
||||
add_subdirectory(dispsrch)
|
||||
add_subdirectory(makedsig)
|
||||
add_subdirectory(readsig)
|
||||
add_subdirectory(parsehdr)
|
||||
|
||||
9
tools/dispsrch/CMakeLists.txt
Normal file
9
tools/dispsrch/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
||||
add_executable(dispsig dispsig)
|
||||
|
||||
target_link_libraries(dispsig dcc_hash)
|
||||
qt5_use_modules(dispsig Core)
|
||||
|
||||
add_executable(srchsig srchsig)
|
||||
|
||||
target_link_libraries(srchsig dcc_hash dcc_lib)
|
||||
qt5_use_modules(srchsig Core)
|
||||
@ -1,13 +1,15 @@
|
||||
/* Quick program to copy a named signature to a small file */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include "perfhlib.h"
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* statics */
|
||||
byte buf[100];
|
||||
uint8_t buf[100];
|
||||
int numKeys; /* Number of hash table entries (keys) */
|
||||
int numVert; /* Number of vertices in the graph (also size of g[]) */
|
||||
int PatLen; /* Size of the keys (pattern length) */
|
||||
@ -15,57 +17,48 @@ int SymLen; /* Max size of the symbols, including null */
|
||||
FILE *f; /* File being read */
|
||||
FILE *f2; /* File being written */
|
||||
|
||||
static word *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static word *g; /* g[] */
|
||||
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static uint16_t *g; /* g[] */
|
||||
|
||||
/* prototypes */
|
||||
void grab(int n);
|
||||
word readFileShort(void);
|
||||
uint16_t readFileShort(void);
|
||||
void cleanup(void);
|
||||
|
||||
|
||||
#define SYMLEN 16
|
||||
#define PATLEN 23
|
||||
|
||||
/* Hash table structure */
|
||||
typedef struct HT_tag
|
||||
{
|
||||
typedef struct HT_tag {
|
||||
char htSym[SYMLEN];
|
||||
byte htPat[PATLEN];
|
||||
uint8_t htPat[PATLEN];
|
||||
} HT;
|
||||
|
||||
HT ht; /* One hash table entry */
|
||||
|
||||
void
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
word w, len;
|
||||
PerfectHash g_pattern_hasher;
|
||||
int main(int argc, char *argv[]) {
|
||||
uint16_t w, len;
|
||||
int i;
|
||||
|
||||
if (argc <= 3)
|
||||
{
|
||||
if (argc <= 3) {
|
||||
printf("Usage: dispsig <SigFilename> <FunctionName> <BinFileName>\n");
|
||||
printf("Example: dispsig dccm8s.sig printf printf.bin\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ((f = fopen(argv[1], "rb")) == NULL)
|
||||
{
|
||||
if ((f = fopen(argv[1], "rb")) == NULL) {
|
||||
printf("Cannot open %s\n", argv[1]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if ((f2 = fopen(argv[3], "wb")) == NULL)
|
||||
{
|
||||
if ((f2 = fopen(argv[3], "wb")) == NULL) {
|
||||
printf("Cannot write to %s\n", argv[3]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
|
||||
/* Read the parameters */
|
||||
grab(4);
|
||||
if (memcmp("dccs", buf, 4) != 0)
|
||||
{
|
||||
if (memcmp("dccs", buf, 4) != 0) {
|
||||
printf("Not a dccs file!\n");
|
||||
exit(3);
|
||||
}
|
||||
@ -75,117 +68,97 @@ main(int argc, char *argv[])
|
||||
SymLen = readFileShort();
|
||||
|
||||
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
|
||||
hashParams( /* Set the parameters for the hash table */
|
||||
g_pattern_hasher.setHashParams(
|
||||
numKeys, /* The number of symbols */
|
||||
PatLen, /* The length of the pattern to be hashed */
|
||||
256, /* The character set of the pattern (0-FF) */
|
||||
0, /* Minimum pattern character value */
|
||||
numVert); /* Specifies C, the sparseness of the graph.
|
||||
See Czech, Havas and Majewski for details
|
||||
*/
|
||||
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
|
||||
|
||||
T1base = g_pattern_hasher.readT1();
|
||||
T2base = g_pattern_hasher.readT2();
|
||||
g = g_pattern_hasher.readG();
|
||||
|
||||
T1base = readT1();
|
||||
T2base = readT2();
|
||||
g = readG();
|
||||
|
||||
/* Read T1 and T2 tables */
|
||||
grab(2);
|
||||
if (memcmp("T1", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("T1", buf, 2) != 0) {
|
||||
printf("Expected 'T1'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = PatLen * 256 * sizeof(word);
|
||||
len = PatLen * 256 * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of T1: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(T1base, 1, len, f) != len)
|
||||
{
|
||||
if (fread(T1base, 1, len, f) != len) {
|
||||
printf("Could not read T1\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
grab(2);
|
||||
if (memcmp("T2", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("T2", buf, 2) != 0) {
|
||||
printf("Expected 'T2'\n");
|
||||
exit(3);
|
||||
}
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of T2: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(T2base, 1, len, f) != len)
|
||||
{
|
||||
if (fread(T2base, 1, len, f) != len) {
|
||||
printf("Could not read T2\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
/* Now read the function g[] */
|
||||
grab(2);
|
||||
if (memcmp("gg", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("gg", buf, 2) != 0) {
|
||||
printf("Expected 'gg'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = numVert * sizeof(word);
|
||||
len = numVert * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(g, 1, len, f) != len)
|
||||
{
|
||||
if (fread(g, 1, len, f) != len) {
|
||||
printf("Could not read T2\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
|
||||
/* This is now the hash table */
|
||||
grab(2);
|
||||
if (memcmp("ht", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("ht", buf, 2) != 0) {
|
||||
printf("Expected 'ht'\n");
|
||||
exit(3);
|
||||
}
|
||||
w = readFileShort();
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(word)))
|
||||
{
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
|
||||
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
|
||||
exit(6);
|
||||
}
|
||||
|
||||
|
||||
for (i=0; i < numKeys; i++)
|
||||
{
|
||||
if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen))
|
||||
{
|
||||
QString argv2(argv[2]);
|
||||
for (i = 0; i < numKeys; i++) {
|
||||
if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen)) {
|
||||
printf("Could not read pattern %d from %s\n", i, argv[1]);
|
||||
exit(7);
|
||||
}
|
||||
if (stricmp(ht.htSym, argv[2]) == 0)
|
||||
{
|
||||
if (argv2.compare(ht.htSym, Qt::CaseInsensitive) == 0) {
|
||||
/* Found it! */
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
fclose(f);
|
||||
if (i == numKeys)
|
||||
{
|
||||
if (i == numKeys) {
|
||||
printf("Function %s not found!\n", argv[2]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
printf("Function %s index %d\n", ht.htSym, i);
|
||||
for (i=0; i < PatLen; i++)
|
||||
{
|
||||
for (i = 0; i < PatLen; i++) {
|
||||
printf("%02X ", ht.htPat[i]);
|
||||
}
|
||||
|
||||
@ -193,56 +166,36 @@ main(int argc, char *argv[])
|
||||
fclose(f2);
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cleanup(void)
|
||||
{
|
||||
void cleanup(void) {
|
||||
/* Free the storage for variable sized tables etc */
|
||||
if (T1base) free(T1base);
|
||||
if (T2base) free(T2base);
|
||||
if (g) free(g);
|
||||
if (T1base)
|
||||
free(T1base);
|
||||
if (T2base)
|
||||
free(T2base);
|
||||
if (g)
|
||||
free(g);
|
||||
}
|
||||
|
||||
void grab(int n)
|
||||
{
|
||||
if (fread(buf, 1, n, f) != (size_t)n)
|
||||
{
|
||||
void grab(int n) {
|
||||
if (fread(buf, 1, n, f) != (size_t)n) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
}
|
||||
|
||||
word
|
||||
readFileShort(void)
|
||||
{
|
||||
byte b1, b2;
|
||||
uint16_t readFileShort(void) {
|
||||
uint8_t b1, b2;
|
||||
|
||||
if (fread(&b1, 1, 1, f) != 1)
|
||||
{
|
||||
if (fread(&b1, 1, 1, f) != 1) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
if (fread(&b2, 1, 1, f) != 1)
|
||||
{
|
||||
if (fread(&b2, 1, 1, f) != 1) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
return (b2 << 8) + b1;
|
||||
}
|
||||
|
||||
/* Following two functions not needed unless creating tables */
|
||||
|
||||
void getKey(int i, byte **keys)
|
||||
{
|
||||
}
|
||||
|
||||
/* Display key i */
|
||||
void
|
||||
dispKey(int i)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -1,13 +1,14 @@
|
||||
/* Quick program to see if a pattern is in a sig file. Pattern is supplied
|
||||
in a small .bin or .com style file */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "perfhlib.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* statics */
|
||||
byte buf[100];
|
||||
uint8_t buf[100];
|
||||
int numKeys; /* Number of hash table entries (keys) */
|
||||
int numVert; /* Number of vertices in the graph (also size of g[]) */
|
||||
int PatLen; /* Size of the keys (pattern length) */
|
||||
@ -15,60 +16,53 @@ int SymLen; /* Max size of the symbols, including null */
|
||||
FILE *f; /* Sig file being read */
|
||||
FILE *fpat; /* Pattern file being read */
|
||||
|
||||
static word *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static word *g; /* g[] */
|
||||
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static uint16_t *g; /* g[] */
|
||||
|
||||
#define SYMLEN 16
|
||||
#define PATLEN 23
|
||||
|
||||
typedef struct HT_tag
|
||||
{
|
||||
typedef struct HT_tag {
|
||||
/* Hash table structure */
|
||||
char htSym[SYMLEN];
|
||||
byte htPat[PATLEN];
|
||||
uint8_t htPat[PATLEN];
|
||||
} HT;
|
||||
|
||||
HT *ht; /* Declare a pointer to a hash table */
|
||||
|
||||
/* prototypes */
|
||||
void grab(int n);
|
||||
word readFileShort(void);
|
||||
uint16_t readFileShort(void);
|
||||
void cleanup(void);
|
||||
void fixWildCards(char *buf); /* In fixwild.c */
|
||||
extern void fixWildCards(uint8_t pat[]); /* In fixwild.c */
|
||||
void pattSearch(void);
|
||||
PerfectHash g_pattern_hasher;
|
||||
|
||||
|
||||
void
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
word w, len;
|
||||
int main(int argc, char *argv[]) {
|
||||
uint16_t w, len;
|
||||
int h, i;
|
||||
int patlen;
|
||||
|
||||
if (argc <= 2)
|
||||
{
|
||||
if (argc <= 2) {
|
||||
printf("Usage: srchsig <SigFilename> <PattFilename>\n");
|
||||
printf("Searches the signature file for the given pattern\n");
|
||||
printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ((f = fopen(argv[1], "rb")) == NULL)
|
||||
{
|
||||
if ((f = fopen(argv[1], "rb")) == NULL) {
|
||||
printf("Cannot open signature file %s\n", argv[1]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if ((fpat = fopen(argv[2], "rb")) == NULL)
|
||||
{
|
||||
if ((fpat = fopen(argv[2], "rb")) == NULL) {
|
||||
printf("Cannot open pattern file %s\n", argv[2]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/* Read the parameters */
|
||||
grab(4);
|
||||
if (memcmp("dccs", buf, 4) != 0)
|
||||
{
|
||||
if (memcmp("dccs", buf, 4) != 0) {
|
||||
printf("Not a dccs file!\n");
|
||||
exit(3);
|
||||
}
|
||||
@ -78,115 +72,97 @@ main(int argc, char *argv[])
|
||||
SymLen = readFileShort();
|
||||
|
||||
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
|
||||
hashParams( /* Set the parameters for the hash table */
|
||||
g_pattern_hasher.setHashParams(
|
||||
numKeys, /* The number of symbols */
|
||||
PatLen, /* The length of the pattern to be hashed */
|
||||
256, /* The character set of the pattern (0-FF) */
|
||||
0, /* Minimum pattern character value */
|
||||
numVert); /* Specifies C, the sparseness of the graph.
|
||||
See Czech, Havas and Majewski for details
|
||||
*/
|
||||
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas
|
||||
and Majewski for details */
|
||||
|
||||
T1base = readT1();
|
||||
T2base = readT2();
|
||||
g = readG();
|
||||
T1base = g_pattern_hasher.readT1();
|
||||
T2base = g_pattern_hasher.readT2();
|
||||
g = g_pattern_hasher.readG();
|
||||
|
||||
/* Read T1 and T2 tables */
|
||||
grab(2);
|
||||
if (memcmp("T1", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("T1", buf, 2) != 0) {
|
||||
printf("Expected 'T1'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = PatLen * 256 * sizeof(word);
|
||||
len = PatLen * 256 * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of T1: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(T1base, 1, len, f) != len)
|
||||
{
|
||||
if (fread(T1base, 1, len, f) != len) {
|
||||
printf("Could not read T1\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
grab(2);
|
||||
if (memcmp("T2", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("T2", buf, 2) != 0) {
|
||||
printf("Expected 'T2'\n");
|
||||
exit(3);
|
||||
}
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of T2: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(T2base, 1, len, f) != len)
|
||||
{
|
||||
if (fread(T2base, 1, len, f) != len) {
|
||||
printf("Could not read T2\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
/* Now read the function g[] */
|
||||
grab(2);
|
||||
if (memcmp("gg", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("gg", buf, 2) != 0) {
|
||||
printf("Expected 'gg'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = numVert * sizeof(word);
|
||||
len = numVert * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
if (w != len) {
|
||||
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
|
||||
exit(4);
|
||||
}
|
||||
if (fread(g, 1, len, f) != len)
|
||||
{
|
||||
if (fread(g, 1, len, f) != len) {
|
||||
printf("Could not read T2\n");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
|
||||
/* This is now the hash table */
|
||||
/* First allocate space for the table */
|
||||
if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0)
|
||||
{
|
||||
if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0) {
|
||||
printf("Could not allocate hash table\n");
|
||||
exit(1);
|
||||
}
|
||||
grab(2);
|
||||
if (memcmp("ht", buf, 2) != 0)
|
||||
{
|
||||
if (memcmp("ht", buf, 2) != 0) {
|
||||
printf("Expected 'ht'\n");
|
||||
exit(3);
|
||||
}
|
||||
w = readFileShort();
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(word)))
|
||||
{
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
|
||||
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
|
||||
exit(6);
|
||||
}
|
||||
|
||||
for (i=0; i < numKeys; i++)
|
||||
{
|
||||
if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen)
|
||||
{
|
||||
for (i = 0; i < numKeys; i++) {
|
||||
if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
}
|
||||
|
||||
/* Read the pattern to buf */
|
||||
if ((patlen = fread(buf, 1, 100, fpat)) == 0)
|
||||
{
|
||||
if ((patlen = fread(buf, 1, 100, fpat)) == 0) {
|
||||
printf("Could not read pattern\n");
|
||||
exit(11);
|
||||
}
|
||||
if (patlen != PATLEN)
|
||||
{
|
||||
if (patlen != PATLEN) {
|
||||
printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN);
|
||||
exit(12);
|
||||
}
|
||||
@ -199,15 +175,11 @@ main(int argc, char *argv[])
|
||||
printf("%02X ", buf[i]);
|
||||
printf("\n");
|
||||
|
||||
|
||||
h = hash(buf);
|
||||
h = g_pattern_hasher.hash(buf);
|
||||
printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
|
||||
if (memcmp(ht[h].htPat, buf, PATLEN) == 0)
|
||||
{
|
||||
if (memcmp(ht[h].htPat, buf, PATLEN) == 0) {
|
||||
printf("Pattern matched");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("Pattern mismatch: found following pattern\n");
|
||||
for (i = 0; i < PATLEN; i++)
|
||||
printf("%02X ", ht[h].htPat[i]);
|
||||
@ -218,55 +190,47 @@ main(int argc, char *argv[])
|
||||
free(ht);
|
||||
fclose(f);
|
||||
fclose(fpat);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pattSearch(void)
|
||||
{
|
||||
void pattSearch(void) {
|
||||
int i;
|
||||
|
||||
for (i=0; i < numKeys; i++)
|
||||
{
|
||||
if ((i % 100) == 0) printf("\r%d ", i);
|
||||
if (memcmp(ht[i].htPat, buf, PATLEN) == 0)
|
||||
{
|
||||
for (i = 0; i < numKeys; i++) {
|
||||
if ((i % 100) == 0)
|
||||
printf("\r%d ", i);
|
||||
if (memcmp(ht[i].htPat, buf, PATLEN) == 0) {
|
||||
printf("\nPattern matched offset %d (0x%X)\n", i, i);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cleanup(void)
|
||||
{
|
||||
void cleanup(void) {
|
||||
/* Free the storage for variable sized tables etc */
|
||||
if (T1base) free(T1base);
|
||||
if (T2base) free(T2base);
|
||||
if (g) free(g);
|
||||
if (T1base)
|
||||
free(T1base);
|
||||
if (T2base)
|
||||
free(T2base);
|
||||
if (g)
|
||||
free(g);
|
||||
}
|
||||
|
||||
void grab(int n)
|
||||
{
|
||||
if (fread(buf, 1, n, f) != (size_t)n)
|
||||
{
|
||||
void grab(int n) {
|
||||
if (fread(buf, 1, n, f) != (size_t)n) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
}
|
||||
|
||||
word
|
||||
readFileShort(void)
|
||||
{
|
||||
byte b1, b2;
|
||||
uint16_t readFileShort(void) {
|
||||
uint8_t b1, b2;
|
||||
|
||||
if (fread(&b1, 1, 1, f) != 1)
|
||||
{
|
||||
if (fread(&b1, 1, 1, f) != 1) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
if (fread(&b2, 1, 1, f) != 1)
|
||||
{
|
||||
if (fread(&b2, 1, 1, f) != 1) {
|
||||
printf("Could not read\n");
|
||||
exit(11);
|
||||
}
|
||||
@ -275,13 +239,7 @@ readFileShort(void)
|
||||
|
||||
/* Following two functions not needed unless creating tables */
|
||||
|
||||
void getKey(int i, byte **keys)
|
||||
{
|
||||
}
|
||||
void getKey(int i, uint8_t **keys) {}
|
||||
|
||||
/* Display key i */
|
||||
void
|
||||
dispKey(int i)
|
||||
{
|
||||
}
|
||||
|
||||
void dispKey(int i) {}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
#include "LIB_PatternCollector.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
/** \note there is an untested assumption that the *first* segment definition
|
||||
with class CODE will be the one containing all useful functions in the
|
||||
LEDATA records. Functions such as _exit() have more than one segment
|
||||
@ -49,7 +52,7 @@ int LIB_PatternCollector::readSyms(FILE *fl)
|
||||
exit(10);
|
||||
}
|
||||
|
||||
while (!feof(fl))
|
||||
while (not feof(fl))
|
||||
{
|
||||
type = readByte(fl);
|
||||
len = readWord(fl);
|
||||
@ -88,7 +91,7 @@ int LIB_PatternCollector::readSyms(FILE *fl)
|
||||
++segnum;
|
||||
|
||||
b = readByte(fl); /* Class name index */
|
||||
if ((b == codeLNAMES) && (codeSEGDEF == NONE))
|
||||
if ((b == codeLNAMES) and (codeSEGDEF == NONE))
|
||||
{
|
||||
/* This is the segment defining the code class */
|
||||
codeSEGDEF = segnum;
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
#include "TPL_PatternCollector.h"
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
/** \note Fundamental problem: there seems to be no information linking the names
|
||||
@ -255,7 +258,7 @@ void TPL_PatternCollector::enterUnitProcs(FILE *f)
|
||||
readString(f);
|
||||
strcpy(name, (char *)buf);
|
||||
cat = readByte(f);
|
||||
if ((cat == charProc) || (cat == charFunc))
|
||||
if ((cat == charProc) or (cat == charFunc))
|
||||
{
|
||||
grab(f,skipPmap); /* Skip to the pmap */
|
||||
pmapOff = readShort(f); /* pmap offset */
|
||||
@ -278,7 +281,7 @@ void TPL_PatternCollector::enterUnitProcs(FILE *f)
|
||||
int TPL_PatternCollector::readSyms(FILE *f)
|
||||
{
|
||||
grab(f,4);
|
||||
if ((strncmp((char *)buf, "TPU0", 4) != 0) && ((strncmp((char *)buf, "TPU5", 4) != 0)))
|
||||
if ((strncmp((char *)buf, "TPU0", 4) != 0) and ((strncmp((char *)buf, "TPU5", 4) != 0)))
|
||||
{
|
||||
printf("Not a Turbo Pascal version 4 or 5 library file\n");
|
||||
fclose(f);
|
||||
|
||||
@ -25,8 +25,11 @@
|
||||
* *
|
||||
\* * * * * * * * * * * * */
|
||||
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef PATLEN
|
||||
#define PATLEN 23
|
||||
#define WILD 0xF4
|
||||
@ -417,7 +420,7 @@ void fixWildCards(uint8_t pat[])
|
||||
|
||||
case 0xCD: /* Int nn */
|
||||
intArg = pat[pc++];
|
||||
if ((intArg >= 0x34) && (intArg <= 0x3B))
|
||||
if ((intArg >= 0x34) and (intArg <= 0x3B))
|
||||
{
|
||||
/* Borland/Microsoft FP emulations */
|
||||
if (ModRM(pat)) return;
|
||||
|
||||
@ -3,13 +3,14 @@
|
||||
#include "LIB_PatternCollector.h"
|
||||
#include "TPL_PatternCollector.h"
|
||||
#include "perfhlib.h" /* Symbol table prototypes */
|
||||
#include "msvc_fixes.h"
|
||||
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
@ -21,7 +22,7 @@
|
||||
|
||||
void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll); /* Save the info */
|
||||
|
||||
int numKeys; /* Number of useful codeview symbols */
|
||||
static int numKeys; /* Number of useful codeview symbols */
|
||||
|
||||
|
||||
static void printUsage(bool longusage) {
|
||||
@ -48,7 +49,7 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
QString arg2 = app.arguments()[1];
|
||||
if (arg2.startsWith("-h") || arg2.startsWith("-?"))
|
||||
if (arg2.startsWith("-h") or arg2.startsWith("-?"))
|
||||
{
|
||||
printUsage(true);
|
||||
return 0;
|
||||
@ -58,6 +59,9 @@ int main(int argc, char *argv[])
|
||||
collector = new TPL_PatternCollector;
|
||||
} else if(arg2.endsWith(".lib")) {
|
||||
collector = new LIB_PatternCollector;
|
||||
} else {
|
||||
qCritical() << "Unsupported file type.";
|
||||
return -1;
|
||||
}
|
||||
if ((srcfile = fopen(argv[1], "rb")) == NULL)
|
||||
{
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
add_executable(parselib parsehdr.cpp)
|
||||
|
||||
target_link_libraries(parselib dcc_hash)
|
||||
qt5_use_modules(parselib Core)
|
||||
@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
/*$Log: locident.h,v $
|
||||
* Revision 1.6 94/02/22 15:20:23 cifuente
|
||||
* Code generation is done.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
#include "Enums.h"
|
||||
/*
|
||||
*$Log: parsehdr.h,v $
|
||||
*/
|
||||
@ -8,9 +10,6 @@ typedef unsigned char byte; /* 8 bits */
|
||||
typedef unsigned short word; /* 16 bits */
|
||||
typedef unsigned char boolT; /* 8 bits */
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
#define BUFF_SIZE 8192 /* Holds a declaration */
|
||||
#define FBUF_SIZE 32700 /* Holds part of a header file */
|
||||
|
||||
@ -38,11 +37,8 @@ void phWarning(char *errmsg);
|
||||
|
||||
#define SYMLEN 16 /* Including the null */
|
||||
#define Int long /* For locident.h */
|
||||
#define int16 short int /* For locident.h */
|
||||
#include "locident.h" /* For the hlType enum */
|
||||
#define bool unsigned char /* For internal use */
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
//#include "locident.h" /* For the hlType enum */
|
||||
//#define bool unsigned char /* For internal use */
|
||||
|
||||
typedef
|
||||
struct ph_func_tag
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
add_executable(readsig readsig.cpp)
|
||||
|
||||
target_link_libraries(readsig dcc_hash)
|
||||
qt5_use_modules(readsig Core)
|
||||
@ -1,34 +1,33 @@
|
||||
/* Quick program to read the output from makedsig */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <io.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include "perfhlib.h"
|
||||
|
||||
/* statics */
|
||||
byte buf[100];
|
||||
uint8_t buf[100];
|
||||
int numKeys; /* Number of hash table entries (keys) */
|
||||
int numVert; /* Number of vertices in the graph (also size of g[]) */
|
||||
int PatLen; /* Size of the keys (pattern length) */
|
||||
int SymLen; /* Max size of the symbols, including null */
|
||||
FILE *f; /* File being read */
|
||||
|
||||
static word *T1base, *T2base; /* Pointers to start of T1, T2 */
|
||||
static word *g; /* g[] */
|
||||
|
||||
/* prototypes */
|
||||
void grab(int n);
|
||||
word readFileShort(void);
|
||||
uint16_t readFileShort(void);
|
||||
void cleanup(void);
|
||||
|
||||
static bool bDispAll = FALSE;
|
||||
static bool bDispAll = false;
|
||||
static PerfectHash g_pattern_hasher;
|
||||
static uint16_t *T1base;
|
||||
static uint16_t *T2base;
|
||||
static uint16_t *g;
|
||||
|
||||
void
|
||||
main(int argc, char *argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
word w, len;
|
||||
uint16_t w, len;
|
||||
int h, i, j;
|
||||
long filePos;
|
||||
|
||||
@ -44,7 +43,7 @@ main(int argc, char *argv[])
|
||||
if (strcmp(argv[i], "-a") == 0)
|
||||
{
|
||||
i++;
|
||||
bDispAll = TRUE;
|
||||
bDispAll = true;
|
||||
}
|
||||
if ((f = fopen(argv[i], "rb")) == NULL)
|
||||
{
|
||||
@ -65,18 +64,16 @@ main(int argc, char *argv[])
|
||||
SymLen = readFileShort();
|
||||
|
||||
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
|
||||
hashParams( /* Set the parameters for the hash table */
|
||||
g_pattern_hasher.setHashParams(
|
||||
numKeys, /* The number of symbols */
|
||||
PatLen, /* The length of the pattern to be hashed */
|
||||
256, /* The character set of the pattern (0-FF) */
|
||||
0, /* Minimum pattern character value */
|
||||
numVert); /* Specifies C, the sparseness of the graph.
|
||||
See Czech, Havas and Majewski for details
|
||||
*/
|
||||
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
|
||||
|
||||
T1base = readT1();
|
||||
T2base = readT2();
|
||||
g = readG();
|
||||
T1base = g_pattern_hasher.readT1();
|
||||
T2base = g_pattern_hasher.readT2();
|
||||
g = g_pattern_hasher.readG();
|
||||
|
||||
/* Read T1 and T2 tables */
|
||||
grab(2);
|
||||
@ -85,7 +82,7 @@ main(int argc, char *argv[])
|
||||
printf("Expected 'T1'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = PatLen * 256 * sizeof(word);
|
||||
len = PatLen * 256 * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
@ -123,7 +120,7 @@ main(int argc, char *argv[])
|
||||
printf("Expected 'gg'\n");
|
||||
exit(3);
|
||||
}
|
||||
len = numVert * sizeof(word);
|
||||
len = numVert * sizeof(uint16_t);
|
||||
w = readFileShort();
|
||||
if (w != len)
|
||||
{
|
||||
@ -145,7 +142,7 @@ main(int argc, char *argv[])
|
||||
exit(3);
|
||||
}
|
||||
w = readFileShort();
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(word)))
|
||||
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
|
||||
{
|
||||
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
|
||||
exit(6);
|
||||
@ -153,8 +150,7 @@ main(int argc, char *argv[])
|
||||
|
||||
if (bDispAll)
|
||||
{
|
||||
fseek(f, 0, SEEK_CUR); /* Needed due to bug in MS fread()! */
|
||||
filePos = _lseek(fileno(f), 0, SEEK_CUR);
|
||||
filePos = ftell(f);
|
||||
for (i=0; i < numKeys; i++)
|
||||
{
|
||||
grab(SymLen + PatLen);
|
||||
@ -175,23 +171,23 @@ main(int argc, char *argv[])
|
||||
{
|
||||
grab(SymLen + PatLen);
|
||||
|
||||
h = hash(&buf[SymLen]);
|
||||
h = g_pattern_hasher.hash(&buf[SymLen]);
|
||||
if (h != i)
|
||||
{
|
||||
printf("Symbol %16s (index %3d) hashed to %d\n",
|
||||
buf, i, h);
|
||||
printf("Symbol %16s (index %3d) hashed to %d\n", buf, i, h);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Done!\n");
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cleanup(void)
|
||||
{
|
||||
// TODO: g_pattern_hasher.hashCleanup();
|
||||
/* Free the storage for variable sized tables etc */
|
||||
if (T1base) free(T1base);
|
||||
if (T2base) free(T2base);
|
||||
@ -207,10 +203,9 @@ void grab(int n)
|
||||
}
|
||||
}
|
||||
|
||||
word
|
||||
readFileShort(void)
|
||||
uint16_t readFileShort(void)
|
||||
{
|
||||
byte b1, b2;
|
||||
uint8_t b1, b2;
|
||||
|
||||
if (fread(&b1, 1, 1, f) != 1)
|
||||
{
|
||||
@ -224,16 +219,3 @@ readFileShort(void)
|
||||
}
|
||||
return (b2 << 8) + b1;
|
||||
}
|
||||
|
||||
/* Following two functions not needed unless creating tables */
|
||||
|
||||
void getKey(int i, byte **keys)
|
||||
{
|
||||
}
|
||||
|
||||
/* Display key i */
|
||||
void
|
||||
dispKey(int i)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user