Binary output to cout on Windows

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Binary output to cout on Windows

Michael Smith-31

I’m trying to resolve an issue that occurs in the native Windows implementation of LLVM (it probably doesn’t occur in Cygwin, but I haven’t checked). Namely, when calling

            llvm-as < input.ll > output.bc     OR     llvm-as < input.ll | opt

outputting to cout is adding \r (the CR character) to newlines. This specifically causes problems in WriterContext::write(…) in lib/Support/Compressor.cpp. Is anyone aware of a way to use a binary mode on cout in Windows (compiled with Visual Studio .NET 2003), or at least disable the inclusion of \r? It would be nice to allow the pipe functionality if it can be done cleanly.

 

~Michael


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Binary output to cout on Windows

Reid Spencer
Hi Michael,

LLVM uses the standard C++ library for this output. If LLVM opens the
file, it is opened in binary mode. However, LLVM is inheriting this file
descriptor from the shell in your example (std::cout) so we don't/can't
change the  open mode. You might want to try this instead:

llvm-as < input.ll -o output.bc OR llvm-as < input.ll -o - | opt

Reid.

On Mon, 2006-05-22 at 14:43 -0700, Michael Smith wrote:

> I’m trying to resolve an issue that occurs in the native Windows
> implementation of LLVM (it probably doesn’t occur in Cygwin, but I
> haven’t checked). Namely, when calling
>
>             llvm-as < input.ll > output.bc     OR     llvm-as <
> input.ll | opt
>
> outputting to cout is adding \r (the CR character) to newlines. This
> specifically causes problems in WriterContext::write(…) in
> lib/Support/Compressor.cpp. Is anyone aware of a way to use a binary
> mode on cout in Windows (compiled with Visual Studio .NET 2003), or at
> least disable the inclusion of \r? It would be nice to allow the pipe
> functionality if it can be done cleanly.
>
>  
>
> ~Michael
>
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Michael Smith-31
In reply to this post by Michael Smith-31
Hi Reid,

llvm-as interprets "-" as using standard output (cout), so llvm-as <
input.ll -o - | opt has the same behavior. You'll actually find a
comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope that
there's a good way to do it.

Michael

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Reid Spencer
Sent: Monday, May 22, 2006 3:34 PM
To: LLVM Developers Mailing List
Subject: Re: [LLVMdev] Binary output to cout on Windows

Hi Michael,

LLVM uses the standard C++ library for this output. If LLVM opens the
file, it is opened in binary mode. However, LLVM is inheriting this file
descriptor from the shell in your example (std::cout) so we don't/can't
change the  open mode. You might want to try this instead:

llvm-as < input.ll -o output.bc OR llvm-as < input.ll -o - | opt

Reid.

On Mon, 2006-05-22 at 14:43 -0700, Michael Smith wrote:

> I'm trying to resolve an issue that occurs in the native Windows
> implementation of LLVM (it probably doesn't occur in Cygwin, but I
> haven't checked). Namely, when calling
>
>             llvm-as < input.ll > output.bc     OR     llvm-as <
> input.ll | opt
>
> outputting to cout is adding \r (the CR character) to newlines. This
> specifically causes problems in WriterContext::write(...) in
> lib/Support/Compressor.cpp. Is anyone aware of a way to use a binary
> mode on cout in Windows (compiled with Visual Studio .NET 2003), or at
> least disable the inclusion of \r? It would be nice to allow the pipe
> functionality if it can be done cleanly.
>
>  
>
> ~Michael
>
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Chris Lattner
On Mon, 22 May 2006, Michael Smith wrote:
> llvm-as interprets "-" as using standard output (cout), so llvm-as <
> input.ll -o - | opt has the same behavior. You'll actually find a
> comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope that
> there's a good way to do it.

Please see:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13

We'd welcome patches to do this, and we can localize this in our system
support library, but you'd have to figure out the magic to make this
happen on win32.

-Chris

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Reid Spencer
> Sent: Monday, May 22, 2006 3:34 PM
> To: LLVM Developers Mailing List
> Subject: Re: [LLVMdev] Binary output to cout on Windows
>
> Hi Michael,
>
> LLVM uses the standard C++ library for this output. If LLVM opens the
> file, it is opened in binary mode. However, LLVM is inheriting this file
> descriptor from the shell in your example (std::cout) so we don't/can't
> change the  open mode. You might want to try this instead:
>
> llvm-as < input.ll -o output.bc OR llvm-as < input.ll -o - | opt
>
> Reid.
>
> On Mon, 2006-05-22 at 14:43 -0700, Michael Smith wrote:
>> I'm trying to resolve an issue that occurs in the native Windows
>> implementation of LLVM (it probably doesn't occur in Cygwin, but I
>> haven't checked). Namely, when calling
>>
>>             llvm-as < input.ll > output.bc     OR     llvm-as <
>> input.ll | opt
>>
>> outputting to cout is adding \r (the CR character) to newlines. This
>> specifically causes problems in WriterContext::write(...) in
>> lib/Support/Compressor.cpp. Is anyone aware of a way to use a binary
>> mode on cout in Windows (compiled with Visual Studio .NET 2003), or at
>> least disable the inclusion of \r? It would be nice to allow the pipe
>> functionality if it can be done cleanly.
>>
>>
>>
>> ~Michael
>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> [hidden email]         http://llvm.cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

-Chris

--
http://nondot.org/sabre/
http://llvm.org/

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Michael Smith-31
In reply to this post by Michael Smith-31
The solution (provided in Microsoft's documentation) is to add:
#include <cstdio>
#include <io.h>
#include <fcntl.h>
and run:
  int result = _setmode( _fileno(stdin), _O_BINARY );
  if( result == -1 )
  { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
}
  result = _setmode( _fileno(stdout), _O_BINARY );
  if( result == -1 )
  { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
}
before using cin or cout. I'm not sure where to add this however, since
it needs to be called before any reads or writes are done, but only
needs to be called once. Any suggestions? At the moment in my own code,
I'm adding them to the tools source files, since that's where it's
determined that cin/cout can be used for input/output.

~Michael

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Chris Lattner
Sent: Monday, May 22, 2006 4:36 PM
To: LLVM Developers Mailing List
Subject: RE: [LLVMdev] Binary output to cout on Windows

On Mon, 22 May 2006, Michael Smith wrote:
> llvm-as interprets "-" as using standard output (cout), so llvm-as <
> input.ll -o - | opt has the same behavior. You'll actually find a
> comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
that
> there's a good way to do it.

Please see:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13

We'd welcome patches to do this, and we can localize this in our system
support library, but you'd have to figure out the magic to make this
happen on win32.

-Chris

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Chris Lattner
On Tue, 23 May 2006, Michael Smith wrote:

> The solution (provided in Microsoft's documentation) is to add:
> #include <cstdio>
> #include <io.h>
> #include <fcntl.h>
> and run:
>  int result = _setmode( _fileno(stdin), _O_BINARY );
>  if( result == -1 )
>  { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
> }
>  result = _setmode( _fileno(stdout), _O_BINARY );
>  if( result == -1 )
>  { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
> }
> before using cin or cout. I'm not sure where to add this however, since
> it needs to be called before any reads or writes are done, but only
> needs to be called once. Any suggestions? At the moment in my own code,
> I'm adding them to the tools source files, since that's where it's
> determined that cin/cout can be used for input/output.

Reid is the guru here, but I'll inject my opinion.  I think that this
should be a method somewhere in the System library.  Given that, you
should change llvm-as.cpp (and all other tools with similar issues) from:

       } else {                      // Specified stdout
         // FIXME: cout is not binary!
         Out = &std::cout;
       }

To:

       } else {                      // Specified stdout
         sys::ChangeStandardStreamToBinary(std::cout);
         Out = &std::cout;
       }

... where "ChangeStandardStreamToBinary" is something that Reid likes.  :)

The implementation of ChangeStandardStreamToBinary would do the code above
in the win32 case, and would be a noop in the unix case.  This is just one
way to do it, I defer to Reid for what the "right" way to do it is :)

-Chris



> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Chris Lattner
> Sent: Monday, May 22, 2006 4:36 PM
> To: LLVM Developers Mailing List
> Subject: RE: [LLVMdev] Binary output to cout on Windows
>
> On Mon, 22 May 2006, Michael Smith wrote:
>> llvm-as interprets "-" as using standard output (cout), so llvm-as <
>> input.ll -o - | opt has the same behavior. You'll actually find a
>> comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
> that
>> there's a good way to do it.
>
> Please see:
> http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13
>
> We'd welcome patches to do this, and we can localize this in our system
> support library, but you'd have to figure out the magic to make this
> happen on win32.
>
> -Chris
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

-Chris

--
http://nondot.org/sabre/
http://llvm.org/

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Reid Spencer
In reply to this post by Michael Smith-31
Hi Michael,

The approach to setting binary mode varies from platform to platform.
Thanks for providing the Win32 version. We will also need to work up a
Unix version (with variants).

The correct way to do this is to add a function to
include/llvm/System/Program.h and then implement that function variantly
for the different platforms. The function can then be called from the
main function of whatever program wants its stdin/stdout to be binary.

We can do this.  Can you create a bug report with this code snippet in
it so we can keep track of it? That way, I won't forget that this needs
to be done.

http://llvm.org/bugs

Reid.

On Tue, 2006-05-23 at 10:27 -0700, Michael Smith wrote:

> The solution (provided in Microsoft's documentation) is to add:
> #include <cstdio>
> #include <io.h>
> #include <fcntl.h>
> and run:
>   int result = _setmode( _fileno(stdin), _O_BINARY );
>   if( result == -1 )
>   { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
> }
>   result = _setmode( _fileno(stdout), _O_BINARY );
>   if( result == -1 )
>   { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
> }
> before using cin or cout. I'm not sure where to add this however, since
> it needs to be called before any reads or writes are done, but only
> needs to be called once. Any suggestions? At the moment in my own code,
> I'm adding them to the tools source files, since that's where it's
> determined that cin/cout can be used for input/output.
>
> ~Michael
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Chris Lattner
> Sent: Monday, May 22, 2006 4:36 PM
> To: LLVM Developers Mailing List
> Subject: RE: [LLVMdev] Binary output to cout on Windows
>
> On Mon, 22 May 2006, Michael Smith wrote:
> > llvm-as interprets "-" as using standard output (cout), so llvm-as <
> > input.ll -o - | opt has the same behavior. You'll actually find a
> > comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
> that
> > there's a good way to do it.
>
> Please see:
> http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13
>
> We'd welcome patches to do this, and we can localize this in our system
> support library, but you'd have to figure out the magic to make this
> happen on win32.
>
> -Chris
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Reid Spencer
In reply to this post by Chris Lattner
On Tue, 2006-05-23 at 13:12 -0500, Chris Lattner wrote:

> Reid is the guru here, but I'll inject my opinion.  I think that this
> should be a method somewhere in the System library.  Given that, you
> should change llvm-as.cpp (and all other tools with similar issues) from:
>
>        } else {                      // Specified stdout
>          // FIXME: cout is not binary!
>          Out = &std::cout;
>        }
>
> To:
>
>        } else {                      // Specified stdout
>          sys::ChangeStandardStreamToBinary(std::cout);
>          Out = &std::cout;
>        }
>
> ... where "ChangeStandardStreamToBinary" is something that Reid likes.  :)
>
> The implementation of ChangeStandardStreamToBinary would do the code above
> in the win32 case, and would be a noop in the unix case.  This is just one
> way to do it, I defer to Reid for what the "right" way to do it is :)
>
Looks like we're on the same page here.

Reid.

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Michael Smith-31
In reply to this post by Michael Smith-31
Nothing really needs to be done on Unix, because to my knowledge Unix
doesn't differentiate between text and binary on stdin/stdout.

~Michael

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Reid Spencer
Sent: Tuesday, May 23, 2006 11:25 AM
To: LLVM Developers Mailing List
Subject: RE: [LLVMdev] Binary output to cout on Windows

Hi Michael,

The approach to setting binary mode varies from platform to platform.
Thanks for providing the Win32 version. We will also need to work up a
Unix version (with variants).

The correct way to do this is to add a function to
include/llvm/System/Program.h and then implement that function variantly
for the different platforms. The function can then be called from the
main function of whatever program wants its stdin/stdout to be binary.

We can do this.  Can you create a bug report with this code snippet in
it so we can keep track of it? That way, I won't forget that this needs
to be done.

http://llvm.org/bugs

Reid.

On Tue, 2006-05-23 at 10:27 -0700, Michael Smith wrote:
> The solution (provided in Microsoft's documentation) is to add:
> #include <cstdio>
> #include <io.h>
> #include <fcntl.h>
> and run:
>   int result = _setmode( _fileno(stdin), _O_BINARY );
>   if( result == -1 )
>   { std::cerr<<"Cannot set input mode to binary."<<std::endl; return
1;
> }
>   result = _setmode( _fileno(stdout), _O_BINARY );
>   if( result == -1 )
>   { std::cerr<<"Cannot set output mode to binary."<<std::endl; return
1;
> }
> before using cin or cout. I'm not sure where to add this however,
since
> it needs to be called before any reads or writes are done, but only
> needs to be called once. Any suggestions? At the moment in my own
code,

> I'm adding them to the tools source files, since that's where it's
> determined that cin/cout can be used for input/output.
>
> ~Michael
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Chris Lattner
> Sent: Monday, May 22, 2006 4:36 PM
> To: LLVM Developers Mailing List
> Subject: RE: [LLVMdev] Binary output to cout on Windows
>
> On Mon, 22 May 2006, Michael Smith wrote:
> > llvm-as interprets "-" as using standard output (cout), so llvm-as <
> > input.ll -o - | opt has the same behavior. You'll actually find a
> > comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
> that
> > there's a good way to do it.
>
> Please see:
> http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13
>
> We'd welcome patches to do this, and we can localize this in our
system
> support library, but you'd have to figure out the magic to make this
> happen on win32.
>
> -Chris
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

RE: Binary output to cout on Windows

Reid Spencer
On Tue, 2006-05-23 at 12:21 -0700, Michael Smith wrote:
> Unix, because to my knowledge Unix
> doesn't differentiate between text and binary on stdin/stdout.

True, but the function still needs to be implemented as a nullity in
order to maintain the illusion in lib/System that LLVM is "platform
neutral" :)

Reid.

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

signature.asc (196 bytes) Download Attachment